gpt4 book ai didi

c++ - 流转换过滤器 : ciphertext length is not a multiple of block size?

转载 作者:行者123 更新时间:2023-11-28 05:18:28 26 4
gpt4 key购买 nike

我正在尝试使用 crypto++ 中的 aes 算法加密和解密纯文本

这是我的加密方式

/*
* Encrypt the given text
*/

template<typename T>
T encryptText(T plainText) {
/* Key and IV setup
* AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-
* bit). This key is secretly exchanged between two parties before communication
* begins. DEFAULT_KEYLENGTH= 16 bytes
*/
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
T cipherText;
cout << "\n\nPlain Text size is (" << plainText.size() << " bytes)" << "\n\n";
cout << plainText << "\n\n";
/*
* Create Cipher Text
*/
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( cipherText ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plainText.c_str() ), plainText.length() + 1 );
stfEncryptor.MessageEnd();
cout << "\n\nCipher Text size is (" << cipherText.size() << " bytes)" << std::endl;
for( int i = 0; i < cipherText.size(); i++ ) {
cout << "0x" << std::hex << (0xFF & static_cast<byte>(cipherText[i])) << " ";
}
return cipherText;
}

这是我的解密方法

/*
* Decrypt the given text
*/

template<typename T>
T decryptText(T encryptedText) {
/* Key and IV setup
* AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-
* bit). This key is secretly exchanged between two parties before communication
* begins. DEFAULT_KEYLENGTH= 16 bytes
*/
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
T decryptedText;
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedText ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( encryptedText.c_str() ), encryptedText.size() );
stfDecryptor.MessageEnd();
std::cout << "\n\nDecrypted Text is " << "\n\n";
std::cout << "\n\n" << decryptedText << "\n\n";
return decryptedText;
}

encryptText 函数正常执行,我将它的输出保存在 mysql 数据库中。调用 decryptText 函数时发生错误,提示

An uncaught exception occurred: StreamTransformationFilter: ciphertext length is not a multiple of block size

我知道这个错误是因为加密文本也可能包含一些 null(0) 值。如何解密实际文本。

编辑:

正如我要求在评论部分显示我的 sql 查询。

这是我的 sql 查询,用于获取 mysql 数据库中保存的加密数据。

template<typename T1, typename T2>
void getUserSms(T1 userId,vector<T1>& smsIds,vector<T2>& text){
try{
mysql::connection db_cs(config_cs_db());
auto sms = db_cs.run(select(all_of(us)).from(us).where(us.usersUserId == userId));
while(!sms.empty()){
const auto& row = sms.front();
auto smsId = row.id.value();
smsIds.push_back(smsId);
auto encryptedText = row.text.value();
auto plainText = decryptText(encryptedText);
text.push_back(plainText);
sms.pop_front();
}
return;
}
catch (const sqlpp::exception& e) {
std::cerr << "Could not get user sms due to some reason ...!!\n";
std:cerr << e.what() << "\n";
return;
}
}

我正在使用 sqlpp11用于 sql 查询。而表的描述是

CREATE TABLE `user_sms`(
`id` BIGINT(50) NOT NULL AUTO_INCREMENT,
`sender_number` VARCHAR(20),
`text` VARCHAR(2000),
`incoming_time` TIMESTAMP,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`users_user_id` BIGINT(30),
PRIMARY KEY(`id`),
FOREIGN KEY(`users_user_id`) REFERENCES `users`(`user_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

最佳答案

  • 将数组key和'iv的数据类型从byte更改为unsigned char`
  • mysql 数据库表 user_sms 中的 text 类型从 VARCHAR 更改为 BLOB.

创建表`user_sms`(
//下面的更改是已经进行的更改。
`文本` BLOB,
)ENGINE=InnoDB 默认字符集=utf8;

谢谢 jww求助。

关于c++ - 流转换过滤器 : ciphertext length is not a multiple of block size?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42057412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com