【发布时间】:2015-03-02 10:11:32
【问题描述】:
我有 c++ 代码,它使用 AES_CFB 将字符串加密为纯文本并生成相同大小的密文,但问题是输入和输出的数据类型,所以谁能帮我让它加密一个无符号整数并生成unsigned int number ciphertext withe 保持明文和 Chipertext 相同的长度(位长度)。
string ENCRYPTOR(const std::string& PlainText)
{
byte key[16]= "1234ff";// byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ];
byte iv[16]= "123456";//byte iv[ CryptoPP::AES::BLOCKSIZE ];
std::string CipherText;
// Encryptor
CryptoPP::CFB_Mode< CryptoPP::AES >::Encryption encryptor( key, sizeof(key), iv);
// Encryption
CryptoPP::StringSource( PlainText, true,
new CryptoPP::StreamTransformationFilter( encryptor,
new CryptoPP::StringSink( CipherText ) ) );
return (CipherText);
}
string DECRYPTOR(const string& CipherText)
{
byte key[16]= "1234ff";
byte iv[16]= "123456";
std::string RecoveredText;
// Decryptor
CryptoPP::CFB_Mode< CryptoPP::AES >::Decryption decryptor( key, sizeof(key), iv );
// Decryption
CryptoPP::StringSource( CipherText, true,
new CryptoPP::StreamTransformationFilter( decryptor,
new CryptoPP::StringSink( RecoveredText ) ) );
return (RecoveredText);
}
int main()
{
string ciphertext;
string plaintext = "3555";
ciphertext= ENCRYPTOR(plaintext);
string retrivdat = DECRYPTOR(ciphertext);
cout<<"The plaintext data is: "<<plaintext<<endl;
cout<<"The ciphertextdata is: "<<ciphertext<<endl;
Coot<<"The retrieved data is: "<<retrivdat<<end;
return 0;
}
输出是
The plaintext data is: 3555
The chepertext data is: ï¥R_
The retrieved data is: 3555
【问题讨论】:
-
CryptoPP::StringSource有一个构造函数将const byte*指针指向一个缓冲区,以及该缓冲区的长度。如果你有int x要加密,只需相应地传递(const byte*)&x和sizeof(x)。 -
@IgorTandetnik 备注 1)您只能使用大多数 API 加密字节 2)根据问题,使用
unsigned int和 3)请确保使用相同大小的int和每台机器上的相同字节序 4) 可能不是最小的编码(但我们不知道最小/最大尺寸) -
似乎您已经成功地做到了这一点(尽管首先转换为字符串对我来说似乎不是最佳选择),您的问题是什么?您需要最小位大小吗?以字节为单位的最小大小,某种最大值?
-
谢谢@Maarten 和伊戈尔。实际上,我需要将整数 PLAINTEXT 值加密为具有相同 SIZE 的整数 CHIPERTEXT 值。例如,明文值是 453(3 个字节),CHIPERTEXT 必须由具有相同大小(3 个字节)的任何整数值组成。换句话说,CHIPERTEXT SIZE 必须是 INTEGER 并且具有与 PLAINTEXT 值相同的 SIZE。
标签: c++ encryption encoding crypto++