尝试用另一种语言解密时 AES 解密错误
Wrong AES decryption when trying to decrypt in another language
当我尝试在C++中加密并在 C# 中解密时,它给了我一个错误
输入数据不是完整的块
但这对我来说没有任何意义,因为如果我尝试用C++解密消息,与我进行加密的语言相同,它可以正常工作。
因此,C++部分的一些代码:
int main(int argc, char* argv[]) {
std::string szEncryptionKey = "Sixteen byte key";
std::string szEncryptionIV = "Sixteen byte key";
std::string str = "I do not like green eggs and ham I do not like them Sam-I-Am.";
std::string str_encrypted = encrypt(str, szEncryptionKey, szEncryptionIV);
std::string str_decrypted = decrypt(str_encrypted, szEncryptionKey, szEncryptionIV);
std::cout << "str encrypted: " << str_encrypted << std::endl;
std::cout << "str decrypted: " << str_decrypted << std::endl;
return 0;
}
std::string encrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string str_out;
CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption encryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::StringSource encryptor(str_in, true,
new CryptoPP::StreamTransformationFilter(encryption,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(str_out),
BlockPaddingSchemeDef::NO_PADDING
)
)
);
return str_out;
}
我正在将加密和 base64 编码的字符串以及 KEY 和 IV 带到 C#:
static void Main(string[] args)
{
byte[] bytes = Encoding.UTF8.GetBytes("Sixteen byte key");
String msg2 = "cboiiqATFAU9KyK49BJMmkLLwiAyaP6yYjyM0oP09xbITLGaxRuLCsTYYzUKANydhO+JO7N00aVz0tmFTg==";
byte[] msg = Convert.FromBase64String(msg2);
DecryptStringFromBytes_Aes(msg, bytes, bytes);
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Mode = CipherMode.CFB;
aesAlg.Padding = PaddingMode.None;
aesAlg.Key = Key;
aesAlg.IV = IV;
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted = null;
// Create an Aes object
// with the specified key and IV.
using (ICryptoTransform decrypt = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV))
{
byte[] decryptedText = decrypt.TransformFinalBlock(cipherText, 0, cipherText.Length);
String s = Encoding.UTF8.GetString(decryptedText);
Console.Write(s);
return s;
}
}
}
错误消息出现在这一行:
byte[] decryptedText = decrypt.TransformFinalBlock(cipherText, 0, cipherText.Length);
这一行:
byte[] bytes = Encoding.UTF8.GetBytes("Sixteen byte key");
这只是 UTF8 的一个猜测,但我不知道它是否是正确的编码。
谁能帮我解决这个问题?
输入数据不是块大小的精确倍数,AES 为 16 字节。 解码时msg2
为 61 字节,这不是有效的 AES 加密长度,因此无效并产生错误消息:"输入数据不是完整的块".
AES是基于块的,因此如果要加密的数据不是块大小的精确倍数,则必须填充。与 AES 一起使用的一般填充方法是 PKCS#7,有时称为 PKCS#5。
CFB 模式需要填充,CFB8 模式不需要。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 从加密项目向量中解密任意选择的元素会导致无效的 PKCS #7 块错误
- 尝试从 Visual Studios C++ 中的加密字符串输出解密字符串时出现逻辑错误
- 压缩天然气.AES.错误解密(可能加密)的文件
- 异或字符串加密/解密输出错误
- 尝试用另一种语言解密时 AES 解密错误
- 解密文件AES_256_CBC返回"bad decrypt"错误
- 用反向_iterator stl错误解密upper_bound
- 使用C 中的OPENSL RSA函数解密消息错误
- C++ 密码解密错误NTE_BAD_DATA
- 无法从链接器解密错误
- 解密CMake错误
- 解密函数重载未找到错误消息
- 使用 AES 解密时错误的 16 个字节