我在想如何用AES解密一段密文。我正在使用crypto++库--或者至少尝试使用这个库。但我一点进展都没有。我假设只需要几行代码就可以运行这个解密算法,但是我搞不清楚。这就是我所写的。现在开始笑吧:
#include <stdio.h>
#include <cstdlib>
#include <rijndael.h>
#include <sha.h>
using namespace std;
int main()
{
// Decryption
CTR_Mode< AES >::Decryption decryptor;
decryptor.SetKeyWithIV( cbcKey, AES::DEFAULT_KEYLENGTH, cbcCipher );
}有没有人能给我一个简短的教程,告诉我如何使用crypto++在给定解密密钥的情况下“简单地”解密一个16字节的密文块?他们的文档比你在上面看到的密文更隐秘(至少对我来说是这样),我通过搜索找到了一点帮助。非常感谢。
发布于 2012-07-01 14:58:23
crypto++库网页上的常见问题解答包含指向“教程”的指针,请访问over there。
发布于 2013-10-04 12:49:58
谁能给我一个简短的教程,告诉我如何使用crypto++在给定解密密钥的情况下“简单地”解密一个16字节的密文块?
这是来自Crypto++维基的。它提供了一个CTR mode example。
AutoSeededRandomPool prng;
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
prng.GenerateBlock( key, key.size() );
byte ctr[ AES::BLOCKSIZE ];
prng.GenerateBlock( ctr, sizeof(ctr) );
string plain = "CTR Mode Test";
string cipher, encoded, recovered;
/*********************************\
\*********************************/
try
{
cout << "plain text: " << plain << endl;
CTR_Mode< AES >::Encryption e;
e.SetKeyWithIV( key, key.size(), ctr );
// The StreamTransformationFilter adds padding
// as required. ECB and CBC Mode must be padded
// to the block size of the cipher. CTR does not.
StringSource ss1( plain, true,
new StreamTransformationFilter( e,
new StringSink( cipher )
) // StreamTransformationFilter
); // StringSource
}
catch( CryptoPP::Exception& e )
{
cerr << e.what() << endl;
exit(1);
}
/*********************************\
\*********************************/
// Pretty print cipher text
StringSource ss2( cipher, true,
new HexEncoder(
new StringSink( encoded )
) // HexEncoder
); // StringSource
cout << "cipher text: " << encoded << endl;
/*********************************\
\*********************************/
try
{
CTR_Mode< AES >::Decryption d;
d.SetKeyWithIV( key, key.size(), ctr );
// The StreamTransformationFilter removes
// padding as required.
StringSource ss3( cipher, true,
new StreamTransformationFilter( d,
new StringSink( recovered )
) // StreamTransformationFilter
); // StringSource
cout << "recovered text: " << recovered << endl;
}
catch( CryptoPP::Exception& e )
{
cerr << e.what() << endl;
exit(1);
}下面是运行示例程序的结果:
$ ./crytpopp-test.exe
key: F534FC7F0565A8CF1629F01DB31AE3CA
counter: A4D16CBC010DACAA2E54FA676B57A345
plain text: CTR Mode Test
cipher text: 12455EDB41020E6D751F207EE6
recovered text: CTR Mode Test发布于 2014-10-02 22:49:16
实际上,Crypto++教程很难理解。一般来说,为了使用Crypto++应用程序接口,你至少需要有一些关于密码学的基础知识,我建议你使用this Cryptography course。
话虽如此,让我来回答你的问题。因为它有点模棱两可,我假设你想要解密一个AES密码,使用CTR模式。作为输入,假设您有一个密码、IV和密钥(在std::string中都以十六进制表示)。
#include <iostream>
#include <string>
#include "crypto++/modes.h" // For CTR_Mode
#include "crypto++/filters.h" //For StringSource
#include "crypto++/aes.h" // For AES
#include "crypto++/hex.h" // For HexDecoder
int main(int argc, char* argv[]) {
// Our input:
// Note: the input was previously generated by the same cipher
std::string iv_string = "37C6D22FADE22B2D924598BEE2455EFC";
std::string cipher_string = "221DF9130F0E05E7E87C89EE6A";
std::string key_string = "7D9BB722DA2DC8674E08C3D44AAE976F";
std::cout << "Cipher text: " << cipher_string << std::endl;
std::cout << "Key: " << key_string << std::endl;
std::cout << "IV: " << iv_string << std::endl;
// 1. Decode iv:
// At the moment our input is encoded in string format...
// we need it in raw hex:
byte iv[CryptoPP::AES::BLOCKSIZE] = {};
// this decoder would transform our std::string into raw hex:
CryptoPP::HexDecoder decoder;
decoder.Put((byte*)iv_string.data(), iv_string.size());
decoder.MessageEnd();
decoder.Get(iv, sizeof(iv));
// 2. Decode cipher:
// Next, we do a similar trick for cipher, only here we would leave raw hex
// in a std::string.data(), since it is convenient for us to pass this
// std::string to the decryptor mechanism:
std::string cipher_raw;
{
CryptoPP::HexDecoder decoder;
decoder.Put((byte*)cipher_string.data(), cipher_string.size());
decoder.MessageEnd();
long long size = decoder.MaxRetrievable();
cipher_raw.resize(size);
decoder.Get((byte*)cipher_raw.data(), cipher_raw.size());
// If we print this string it's completely rubbish:
// std::cout << "Raw cipher: " << cipher_raw << std::endl;
}
// 3. Decode the key:
// And finally the same for the key:
byte key[CryptoPP::AES::DEFAULT_KEYLENGTH];
{
CryptoPP::HexDecoder decoder;
decoder.Put((byte*)key_string.data(), key_string.size());
decoder.MessageEnd();
decoder.Get(key, sizeof(key));
}
// 4. Decrypt:
std::string decrypted_text;
try {
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption d;
d.SetKeyWithIV(key, sizeof(key), iv);
CryptoPP::StringSource ss(
cipher_raw,
true,
new CryptoPP::StreamTransformationFilter(
d,
new CryptoPP::StringSink(decrypted_text)
) // StreamTransformationFilter
); // StringSource
std::cout << "Decrypted text: " << decrypted_text << std::endl;
}
catch( CryptoPP::Exception& e ) {
std::cerr << e.what() << std::endl;
exit(1);
}
return 0;
}我使用Crypto++562在Ubutunu14.04上编译了它:
g++ -Wall -std=c++0x -o prog practicalAES.cpp -lcryptopp
如果我运行这个程序,我会得到这样的输出:
Cipher text: 221DF9130F0E05E7E87C89EE6A Key: 7D9BB722DA2DC8674E08C3D44AAE976F IV: 37C6D22FADE22B2D924598BEE2455EFC Decrypted text: CTR Mode Test
它在这里并不真正可见,但是密钥和IV都有相同的长度- 16字节(或128位)。这是块大小,所以这个密码是AES-128。因为它是CTR模式,所以不添加填充,并且密码和明文具有相同的字节数。
还要注意,60%的代码涉及将字符串解码为十六进制,而解密本身只是最后一步(因此,如果您的输入数据是原始十六进制,则不需要解码)。
https://stackoverflow.com/questions/11277324
复制相似问题