Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt crypto++aes加密/解密未正确解密_Qt_Encryption_Crypto++ - Fatal编程技术网

Qt crypto++aes加密/解密未正确解密

Qt crypto++aes加密/解密未正确解密,qt,encryption,crypto++,Qt,Encryption,Crypto++,我有以下代码: #define PRIVATE_KEY "729308A8E815F6A46EB3A8AE6D5463CA7B64A0E2E11BC26A68106FC7697E727E37011" 要加密: QString Encryption::AESEncrypt(const QString &data) { string plain = data.toStdString(); string ciphertext; // Hex decode symmetric key: H

我有以下代码:

#define PRIVATE_KEY "729308A8E815F6A46EB3A8AE6D5463CA7B64A0E2E11BC26A68106FC7697E727E37011"
要加密:

QString Encryption::AESEncrypt(const QString &data)
{
string plain = data.toStdString();
string ciphertext;


// Hex decode symmetric key:
HexDecoder decoder;
decoder.Put( (byte *)PRIVATE_KEY, 32*2 );
decoder.MessageEnd();

word64 size = decoder.MaxRetrievable();
char *decodedKey = new char[size];

decoder.Get((byte *)decodedKey, size);

// Generate Cipher, Key, and CBC
byte key[ AES::MAX_KEYLENGTH ];
byte iv[ AES::BLOCKSIZE ];

StringSource( reinterpret_cast<const char *>(decodedKey), true,
              new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );

memset( iv, 0x00, AES::BLOCKSIZE );

CBC_Mode<AES>::Encryption Encryptor( key, sizeof(key), iv );

StringSource( plain, true, new StreamTransformationFilter( Encryptor,
              new HexEncoder(new StringSink( ciphertext ) ) ) );

return QString::fromStdString(ciphertext);

}

所以我不知道为什么它不能正确解密。我从下面的代码中获取代码。代码在解密时在第二个StringSource上失败,并在第一个异常上着陆。你知道我做错了什么吗?

我在提供的代码中遇到了一个PKCS7填充错误。我使用的代码被修改以删除Qt内容和函数调用,因此我不确定是否意外添加/删除了错误

我能够通过修改密钥的编码/解码来修复填充问题。我认为问题就在这里:

StringSource( reinterpret_cast<const char *>(decodedKey), true,
          new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
这三个cout的输出为:


谢谢你提供的解决方案,我将尝试一下你在这里制作的StringSources,清理所有你用新配置的对象?@Boumbles-是的。当对象接受指针时,该对象将获得所有权。当对象被销毁时,指针将被删除。如果对象采用引用,则不采用所有权。请参阅README.txt中的重要用法说明。
Encryption encrypt;
QString encdata = encrypt.AESEncrypt("This is my data");
qDebug() << "encrypt: " << encdata;
qDebug() << "decrypt" << encrypt.AESDecrypt(encdata);
encrypt:  "4E712EFDE13DA42FF798C193D17BE5D2" 
decrypt "" 
StringSource( reinterpret_cast<const char *>(decodedKey), true,
          new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
#define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
#define PRIVATE_KEY "729308A8E815F6A46EB3A8AE6D5463CA7B64A0E2E11BC26A68106FC7697E727E37011"

string plain1 = "Now is the time for all good men...";
string decoded1, cipher1;

// Hex decode symmetric key:
StringSource ss1((const byte*)PRIVATE_KEY, COUNTOF(PRIVATE_KEY), true,
                 new HexDecoder(new StringSink(decoded1)));

// Generate Cipher, Key, and CBC
byte key1[ AES::MAX_KEYLENGTH ];
byte iv1[ AES::BLOCKSIZE ];

SHA256 sha1;
StringSource ss2( decoded1, true,
                 new HashFilter(sha1, new ArraySink(key1, sizeof(key1))) );

memset( iv1, 0x00, AES::BLOCKSIZE );

CBC_Mode<AES>::Encryption encryptor( key1, sizeof(key1), iv1 );

StringSource ss3( plain1, true, new StreamTransformationFilter( encryptor,
                                                               new HexEncoder(new StringSink( cipher1 ) ) ) );

string plain2, decoded2;
string cipher2 = cipher1;

// Hex decode symmetric key:
StringSource ss4((const byte*)PRIVATE_KEY, COUNTOF(PRIVATE_KEY), true,
                 new HexDecoder(new StringSink(decoded2)));

// Generate Cipher, Key, and CBC
byte key2[ AES::MAX_KEYLENGTH ];
byte iv2[ AES::BLOCKSIZE ];

SHA256 sha2;
StringSource ss5( decoded2, true,
                 new HashFilter(sha2, new ArraySink(key2, sizeof(key2))) );

memset( iv2, 0x00, AES::BLOCKSIZE );


CBC_Mode<AES>::Decryption decryptor( key2, sizeof(key2), iv2 );

StringSource ss6( cipher2, true,
                 new HexDecoder(new StreamTransformationFilter( decryptor,
                                                               new StringSink( plain2 ) ) ) );

cout << "Plain 1: " << plain1 << endl;
cout << "Cipher: " << cipher1 << endl;
cout << "Plain 2: " << plain2 << endl;
Plain 1: Now is the time for all good men...
Cipher: 3073448F4A71BC26CF81441F1DEE69C5DE700DF86294181B5E72E19D260DDF1E725DB3EFC74415982FFF45F9F7E290AE
Plain 2: Now is the time for all good men...