データの暗号化/復号化にライブラリCrypto++
を使用しています。公式ページはhttps://www.cryptopp.comです。私はthis tutorialに従っています。 Crypto++
でブロック暗号を使用する方法を示します。この部分はfind keyword "using block cipher"で見ることができます。Visual C++で関数に渡されるときに配列のサイズが自動的に変更される
デモをスムーズに実行できます。彼らは、キーを使用してデータを暗号化し、同じキーを使用してデータを復号化します。コードをencrypt()
とdecrypt()
に分割したいと思います。 下記の私のencrypt()
機能を見ることができます。
ザ・パートが含まれています
#include "E:\Working\Improve\CPP\cryptopp565\osrng.h"
using CryptoPP::AutoSeededRandomPool;
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include <cstdlib>
using std::exit;
#include "E:\Working\Improve\CPP\cryptopp565\cryptlib.h"
using CryptoPP::Exception;
#include "E:\Working\Improve\CPP\cryptopp565\hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;
#include "E:\Working\Improve\CPP\cryptopp565\filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::StreamTransformationFilter;
#include "E:\Working\Improve\CPP\cryptopp565\aes.h"
using CryptoPP::AES;
#include "E:\Working\Improve\CPP\cryptopp565\ccm.h"
#include "E:\Working\Improve\CPP\cryptopp565\modes.h"
using CryptoPP::ECB_Mode;
#include <fstream>
#include "assert.h"
コード本体:
// My encrypt function
void encrypt(byte cbCipherText[AES::BLOCKSIZE], byte *plainText,
byte key[AES::DEFAULT_KEYLENGTH], int sizeKey) {
int size = sizeof(key);
ECB_Mode<AES>::Encryption Encryptor(key, sizeKey);
Encryptor.ProcessData(cbCipherText, plainText, sizeof(plainText));
}
void main() {
byte PlainText[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o',
'r', 'l', 'd', 0x0, 0x0, 0x0, 0x0, 0x0};
byte key[AES::DEFAULT_KEYLENGTH];
::memset(key, 0x01, AES::DEFAULT_KEYLENGTH);
// Encrypt data
int size = sizeof(key);
int default = AES::DEFAULT_KEYLENGTH;
ECB_Mode<AES>::Encryption Encryptor(key, size);
// Next three lines are tutorial's code for encrypt
byte cbCipherText[AES::BLOCKSIZE];
Encryptor.ProcessData(cbCipherText, PlainText, sizeof(PlainText));
ECB_Mode<AES>::Decryption Decryptor(key, sizeof(key));
// Next two lines are my code to call the encrypt() function, I "cloned" the
// code
// from above three line!. Comment out them we will have the code like the
// demo.
byte myCipherText[AES::BLOCKSIZE];
encrypt(myCipherText, PlainText, key, size);
// Decrypt
byte cbRecoveredText[AES::BLOCKSIZE];
Decryptor.ProcessData(cbRecoveredText, cbCipherText, sizeof(cbCipherText));
// std::string PlainText ="Voltaire said, Prejudices are what fools use for
//reason";
cout << endl << "Recovered text: " << cbRecoveredText << endl;
getchar();
}
キーが値\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1
で作成されました。デモコードでは、キーの値は決して変更されず、そのサイズは常に16になります。関数を呼び出してkey
を渡すと、キーサイズ(sizeof(key)
)は作成時には16になりますが、長さは常に4(!)です。キー値はx1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1ĂŒĂŒĂŒĂŒĂŒĂŒĂŒĂŒHello World
(!!!)です。 したがって、関数にジャンプすると、私のコードは常に"AES: 4 is not valid key length"
というエラーになります。 なぜこれが起こったのか、それをどう修正するのか分かりません。どんな助けもありがとう!
"AES:4 is not valid key length"というエラーが表示されます。しかし、 'encrypt'関数のどこでも' size'( 'sizeof(key)'として計算されます)を渡すことは決してありません。あなたはどうしたらそのエラーを得ることができましたか? – AnT
最初の#includeを変更してください。それは私が読書を続けるのを止める。 – LeDYoM