我正在写一个基于crypto ++库的类。 此类是使用AES算法对数据进行编码和解码的简单方法。 这是我的代码:
class AESEncryption
{
public:
static std::string decrypt(const std::string& data, const std::string& password);
static std::string encrypt(const std::string& data, const std::string& password);
};
std::string AESEncryption::encrypt(const std::string& data, const std::string& password)
{
std::vector<CryptoPP::byte> key(password.begin(), password.end());
std::string result;
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption encryption;
encryption.SetKey(key.data(), key.size());
CryptoPP::StringSource(data, true, new CryptoPP::StreamTransformationFilter(encryption, new CryptoPP::StringSink(result)));
return result;
}
std::string AESEncryption::decrypt(const std::string& data, const std::string& password)
{
std::vector<CryptoPP::byte> key(password.begin(), password.end());
std::string result;
CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption decryption;
decryption.SetKey(key.data(), key.size());
CryptoPP::StringSource(data, true, new CryptoPP::StreamTransformationFilter(decryption, new CryptoPP::StringSink(result)));
return result;
}
我开始使用标准fips197(第46页)中的示例测试班级
这是我的测试代码:
namespace
{
QByteArray key(QByteArray::fromHex("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"));
QByteArray plainText(QByteArray::fromHex("00112233445566778899AABBCCDDEEFF"));
QByteArray result(QByteArray::fromHex("8EA2B7CA516745BFEAFC49904B496089"));
}
TEST(AESEncrypt, Valid)
{
std::string key_str(key.begin(), key.end());
std::string paintText_str(plainText.begin(), plainText.end());
std::string expected_result(result.begin(), result.end());
auto result = AESEncryption::encrypt(paintText_str, key_str);
EXPECT_EQ(result, expected_result);
auto decrypt = AESEncryption::decrypt(result, key_str);
EXPECT_EQ(decrypt, paintText_str);
}
我得到了下一个结果:
Expected equality of these values:
result
Which is: "\x8E\xA2\xB7\xCAQgE\xBF\xEA\xFCI\x90KI`\x89\x9F;u\x4\x92o\x8B\xD3n1\x18\xE9\x3\xA4\xCDJ"
expected_result
Which is: "\x8E\xA2\xB7\xCAQgE\xBF\xEA\xFCI\x90KI`\x89"
我得到了无用的数据:
\x9F;u\x4\x92o\x8B\xD3n1\x18\xE9\x3\xA4\xCDJ
如何解决?加密符合标准的我该怎么办?
答案 0 :(得分:3)
Expected equality of these values: result Which is: "\x8E\xA2\xB7\xCAQgE\xBF\xEA\xFCI\x90KI`\x89\x9F;u\x4\x92o\x8B\xD3n1\x18\xE9\x3\xA4\xCDJ" expected_result Which is: "\x8E\xA2\xB7\xCAQgE\xBF\xEA\xFCI\x90KI`\x89"
如何解决?加密符合标准的我该怎么办?
提供的FIPS 197 ECB模式测试不带填充。但是,下面的代码使用PKCS填充,由于填充,该填充会增加16个字节:
StringSource(data, true, new StreamTransformationFilter(encryption, new StringSink(result)));
更正确地讲,ECB模式默认使用DEFAULT_PADDING
,对于ECB模式和CBC模式则为PKCS_PADDING
。我认为您应该改用NO_PADDING
:
StringSource(data, true, new StreamTransformationFilter(encryption, new StringSink(result), NO_PADDING));
另请参阅StreamTransformationFilter
的文档。这就是您正在使用的过滤器所发生的情况:
StreamTransformationFilter (
StreamTransformation &c,
BufferedTransformation *attachment=NULL,
BlockPaddingScheme padding=DEFAULT_PADDING)
关于“ Crypto ++ AES与标准fips197不匹配” ,您可以使用以下代码通过FIPS 197测试向量(除了SP800-38)测试库:
$ ./cryptest.exe tv aes
Using seed: 1544068124
Testing SymmetricCipher algorithm AES/ECB.
....
Testing SymmetricCipher algorithm AES/CBC.
........
Testing SymmetricCipher algorithm AES/CFB.
.......
Testing SymmetricCipher algorithm AES/OFB.
....
Testing SymmetricCipher algorithm AES/CTR.
.............