Qt crypto ++ aes加密/解密不能正确解密

时间:2014-05-13 19:10:16

标签: qt encryption crypto++

我有以下代码:

#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);

}

解密:

QString Encryption::AESDecrypt(const QString &data)
{
string plain;
string encrypted = data.toStdString();

// 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 );

try
{
    CBC_Mode<AES>::Decryption Decryptor( key, sizeof(key), iv );


    StringSource( encrypted, true,
                  new HexDecoder(new StreamTransformationFilter( Decryptor,
                                 new StringSink( plain ) ) ) );

}
catch (Exception &e) { // ...

}
catch (...) { // ...

}
return QString::fromStdString(plain);
}

如果我运行以下内容:

Encryption encrypt;
QString encdata = encrypt.AESEncrypt("This is my data");
qDebug() << "encrypt: " << encdata;
qDebug() << "decrypt" << encrypt.AESDecrypt(encdata);

我得到以下输出:

encrypt:  "4E712EFDE13DA42FF798C193D17BE5D2" 
decrypt "" 

所以我不确定为什么它没有正确解密。我从以下conversation中获取了代码。第二个StringSource上的代码在第一个Exception上解密和登陆时失败。关于我做错什么的任何想法?

1 个答案:

答案 0 :(得分:0)

我使用提供的代码得到了PKCS#7填充错误。我使用的代码被修改为删除Qt内容并删除函数调用,因此我不确定是否意外添加/删除了错误。

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

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

在上面的代码中,无法保证decodedKeyNULL终止。修复很简单 - 将其存储在string中,然后使用length()

您列出的密钥长度为35个字节(解码后)。

#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;

这三个cout的输出是:

Plain 1: Now is the time for all good men...
Cipher: 3073448F4A71BC26CF81441F1DEE69C5DE700DF86294181B5E72E19D260DDF1E725DB3EFC74415982FFF45F9F7E290AE
Plain 2: Now is the time for all good men...