如何连续加密和解密多个值
How encrypt and decrypt several values in a row
在我的程序中,我使用 crypto++ 库。
我有这样的结构:
struct crypt_struct{
//consists of encrypted data
string name1;
string name2;
string name3;
}
struct decrypt_struct{
//consists of decrypted data
int name1;
string name2;
double name3;
}
我有加密功能:
crypt_struct encrypt(decrypt_struct struct_in) {
//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);
//output encrypt struct
crypt_struct struct_out;
//encrypt text
string ciphertext;
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));
//int name1
stfEncryptor.Put(reinterpret_cast<const unsigned char*> ((to_string(struct_in.name1)).c_str()), (to_string(struct_in.name1)).length() + 1);
stfEncryptor.MessageEnd();
struct_out.name1 = ciphertext;
cout << ciphertext << endl;
ciphertext.clear();
//string name2
stfEncryptor.Put(reinterpret_cast<const unsigned char*> (struct_in.name2.c_str()), struct_in.name2.length() + 1);
stfEncryptor.MessageEnd();
struct_out.name2 = ciphertext;
cout << ciphertext << endl;
ciphertext.clear();
//double name3
stfEncryptor.Put(reinterpret_cast<const unsigned char*> ((to_string(struct_in.name3)).c_str()), (to_string(struct_in.name3)).length() + 1);
stfEncryptor.MessageEnd();
struct_out.name3 = ciphertext;
cout << ciphertext << endl;
ciphertext.clear();
return struct_out;
}
和解密函数:
decrypt_struct decrypt(crypt_struct struct_in)
{
//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);
//
// Decrypt
//
decrypt_struct struct_out;
string decryptedtext;
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext));
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(struct_in.name1.c_str()), struct_in.name1.length());
struct_out.name1= atoi(decryptedtext.c_str());
stfDecryptor.MessageEnd();
decryptedtext.clear();
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(struct_in.name2.c_str()), struct_in.name2.length());
struct_out.name2= atoi(decryptedtext.c_str());
stfDecryptor.MessageEnd();
decryptedtext.clear();
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(struct_in.name3.c_str()), struct_in.name3.length());
struct_out.name3= atoi(decryptedtext.c_str());
stfDecryptor.MessageEnd();
decryptedtext.clear();
return struct_out;
}
如果我尝试只加密一个变量,那么所有内容都会被正确解密。但是如果我尝试同时加密多个变量,则会发生错误。而且,在对变量int类型进行加密时,也会出现同样的错误。可以连接什么,有没有人知道?
我觉得可能是这个函数一次只能加密一行。
问题是这样的:
问题是在这个库的源代码中,解密时使用了 c_str()
函数。那些:
stfDecryptor.Put(reinterpret_cast<const unsigned char*> (struct_in.name1.c_str()), struct_in.name1.size());
官方文档说:
The pointer obtained from c_str()
may only be treated as a pointer to a null-terminated character string if the string object does not contain other null characters.
在加密函数中,一个零字符被添加到字符串中,当我们尝试解密时,通过 c_str()
函数,我们 trim 我们的字符串具有第一个零字符。
但是,在 с++ 11 的标准中,添加了一个类似的函数,它执行与 data()
函数相同的操作,只是它不考虑空字符。
结果,我收到了:
stfDecryptor.Put(reinterpret_cast<const unsigned char*> (struct_in.name1.data()), struct_in.name1.size());
此外,重要的是,如果您先加密 name1
,然后 name2
,然后 name3
。那和解密你应该以相同的顺序。
在我的程序中,我使用 crypto++ 库。 我有这样的结构:
struct crypt_struct{
//consists of encrypted data
string name1;
string name2;
string name3;
}
struct decrypt_struct{
//consists of decrypted data
int name1;
string name2;
double name3;
}
我有加密功能:
crypt_struct encrypt(decrypt_struct struct_in) {
//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);
//output encrypt struct
crypt_struct struct_out;
//encrypt text
string ciphertext;
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));
//int name1
stfEncryptor.Put(reinterpret_cast<const unsigned char*> ((to_string(struct_in.name1)).c_str()), (to_string(struct_in.name1)).length() + 1);
stfEncryptor.MessageEnd();
struct_out.name1 = ciphertext;
cout << ciphertext << endl;
ciphertext.clear();
//string name2
stfEncryptor.Put(reinterpret_cast<const unsigned char*> (struct_in.name2.c_str()), struct_in.name2.length() + 1);
stfEncryptor.MessageEnd();
struct_out.name2 = ciphertext;
cout << ciphertext << endl;
ciphertext.clear();
//double name3
stfEncryptor.Put(reinterpret_cast<const unsigned char*> ((to_string(struct_in.name3)).c_str()), (to_string(struct_in.name3)).length() + 1);
stfEncryptor.MessageEnd();
struct_out.name3 = ciphertext;
cout << ciphertext << endl;
ciphertext.clear();
return struct_out;
}
和解密函数:
decrypt_struct decrypt(crypt_struct struct_in)
{
//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);
//
// Decrypt
//
decrypt_struct struct_out;
string decryptedtext;
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext));
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(struct_in.name1.c_str()), struct_in.name1.length());
struct_out.name1= atoi(decryptedtext.c_str());
stfDecryptor.MessageEnd();
decryptedtext.clear();
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(struct_in.name2.c_str()), struct_in.name2.length());
struct_out.name2= atoi(decryptedtext.c_str());
stfDecryptor.MessageEnd();
decryptedtext.clear();
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(struct_in.name3.c_str()), struct_in.name3.length());
struct_out.name3= atoi(decryptedtext.c_str());
stfDecryptor.MessageEnd();
decryptedtext.clear();
return struct_out;
}
如果我尝试只加密一个变量,那么所有内容都会被正确解密。但是如果我尝试同时加密多个变量,则会发生错误。而且,在对变量int类型进行加密时,也会出现同样的错误。可以连接什么,有没有人知道?
我觉得可能是这个函数一次只能加密一行。
问题是这样的:
问题是在这个库的源代码中,解密时使用了 c_str()
函数。那些:
stfDecryptor.Put(reinterpret_cast<const unsigned char*> (struct_in.name1.c_str()), struct_in.name1.size());
官方文档说:
The pointer obtained from
c_str()
may only be treated as a pointer to a null-terminated character string if the string object does not contain other null characters.
在加密函数中,一个零字符被添加到字符串中,当我们尝试解密时,通过 c_str()
函数,我们 trim 我们的字符串具有第一个零字符。
但是,在 с++ 11 的标准中,添加了一个类似的函数,它执行与 data()
函数相同的操作,只是它不考虑空字符。
结果,我收到了:
stfDecryptor.Put(reinterpret_cast<const unsigned char*> (struct_in.name1.data()), struct_in.name1.size());
此外,重要的是,如果您先加密 name1
,然后 name2
,然后 name3
。那和解密你应该以相同的顺序。