【问题标题】:c++ mcrypt error at mcrypt_genericc++ mcrypt 错误在 mcrypt_generic
【发布时间】:2011-11-13 14:06:01
【问题描述】:

我有以下代码:

[test.cpp]

#include <mcrypt.h>
#include <string>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

#include <string>
using namespace std;
int main()
{
  char algo[] = "rijndael-256";
  char mode[] = "cbc";
   char *block_buffer=(char*)"HELLO!! MY NAME IS: ";
cout<<"here"<<endl;
string s;
char key="1234-5678-9654-7512-7895-2543-12";


  char iv[]  = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};;

  MCRYPT td = mcrypt_module_open(algo, NULL, mode, NULL);
  if (td == MCRYPT_FAILED) { cout<<"error"<<endl;}

int keysize=32;
  int r =  mcrypt_generic_init(td, key, keysize, iv);
if (r<0)
{
    cout<<"error2"<<endl;
    mcrypt_perror(r);
            return 1;

}

 //while ( fread (&block_buffer, 1, 1, stdin) == 1 ) {
   int j=          mcrypt_generic (td, &block_buffer, sizeof(block_buffer));
if (j!=0){std::cout<<"error encrypting"<<std::endl;} // I HAVE ERROR HERE J==0


 //how to print the encrypted string??

 cout<<"buffer "<<block_buffer<<endl; //this is not the encriperd string. why?

    mcrypt_generic_deinit(td);

  mcrypt_module_close(td);
}

我正在测试代码:

$: g++ test.cpp -o tst -lmcrypt  
                      $: ./tst

我应该在哪里添加PKCS 7?

我有以下方法:

std::string add_pkcs7_padding(std::string s, std::size_t n)
{
  const std::size_t fill = n - (s.length() % n);
  s.append(fill, static_cast<char>(fill));
  return s;
}

std::string strip_pkcs7_padding(std::string s, std::size_t n)
{
  const std::size_t pad = static_cast<unsigned char>(*s.rbegin());
  return s.substr(0, s.length() - pad);
}

我不知道什么时候应该运行它以及在我的代码中的什么位置。

需要一些帮助。非常感谢!

编辑:

我有错误:mcrypt_generic (td, &block_buffer, sizeof(block_buffer));编译器打印 j=0;

【问题讨论】:

    标签: c++ ubuntu mcrypt


    【解决方案1】:

    您应该使用char* 调用mcrypt_generic(),而不是像您那样使用char**:

    mcrypt_generic(td, block_buffer, sizeof(block_buffer));
                       ^^^           ^^^^^^^^^^^^^^^^^^^^
                                          ouch!
    

    另外,长度是错误的,因为sizeof(block_buffer) 只是指针的大小,而不是字符串;如果你需要什么strlen(block_buffer).

    但这通常仍然是错误的,因为您需要您的消息是块大小的倍数。使用填充功能:

    std::string s = add_pkcs7_padding(block_buffer, mcrypt_enc_get_block_size(td));
    std::vector<char> v(s.begin(), s.end());  // this will hold the encrypted data
    mcrypt_generic(td, v.data(), v.size());
    

    顺便说一句,你的明文应该这样声明:

    const char * block_buffer = "HELLO!! MY NAME IS: ";
    ^^^^^                       ^^^^
      constness!                  no explicit cast!
    

    但是为什么这么笨拙,还是用字符串比较好:

    std::string plaintext = "HELLO!! MY NAME IS: ";
    


    我认为您可能会从一本好的 C++ 书籍中受益,并稍微熟悉一下该语言的基础知识 - 有一个项目可以开展是件好事,但您的大多数问题与加密或加密无关mcrypt,但只是一般的 C++ 编程。

    【讨论】:

    • 你能告诉我加密是在哪里添加的吗?在block_buffer中?如果我想打印密文怎么办?需要一些帮助。多谢!!你能写代码吗?
    • 请认真地阅读手册。都在里面。 mcrypt_generic 将数据加密到位,因此您传递到其中的数据缓冲区将包含加密后的密文。请注意我是如何使用vector&lt;char&gt; 来保存可变数据的,因为您不能直接修改string。
    • add_pkcs7_padding - 方法有两个参数。你只放了一个参数,第二个参数是什么?
    • 哦,抱歉,忘记了 - 这是块大小(您可能已经通过查看代码猜到了)。我会编辑它。
    • 我知道他会在一段时间内打印出来。问题是,就我而言,当我编译时..没有打印任何内容..我有一个空白页。该程序保留在一段时间内并且不打印任何内容。我不知道该怎么办
    猜你喜欢
    • 2011-02-15
    • 2017-04-25
    • 2017-10-28
    • 1970-01-01
    • 2016-07-22
    • 2012-02-07
    • 2018-07-25
    • 2019-01-19
    相关资源
    最近更新 更多