데이터 암호화/해독에 라이브러리 Crypto++
을 사용하고 있습니다. 공식 페이지는 https://www.cryptopp.com입니다. 나는 this tutorial을 팔로우하고 있습니다. 그것은 Crypto++
으로 블록 암호를 사용하는 방법을 보여줍니다. 이 부분은 "블록 암호를 사용하여"라는 키워드로 찾을 수 있습니다.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입니다. 내 encrypt()
함수를 호출하고 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"오류가 발생했다고 주장합니다. 그러나,'size' ('sizeof (key) '로 계산)는'encrypt' 함수의 어느 곳에 나 전달하지 마십시오. 어떻게 그 오류를 가져올 수 있었습니까? – AnT
첫 번째 #include를 변경하십시오. 그것은 계속해서 읽는 것을 멈추게합니다. – LeDYoM