我有解密openssl aes加密文本的问题。
以下代码块显示了我如何加密文本。通过CLI,我发送一个托管字符串进行加密,并为此提供密钥。当我使用我用来加密同一个功能的密钥时,它工作得非常好。当我在同一函数中查看dec_out时,解密后的值与输入相同。
ID TS TB TC TSC
1 1.73602 -1 0 1.736020823
2 1.73023 -1 0.254 1.984238239
3 1.72445 0 0.624 2.348455656
4 1.72326 0 0.254 1.977269484
5 1.72089 0 1.25 2.970897142
6 1.72089 0 0 1.720897142
7 1.72089 0 0 1.720897142
8 1.70236 0 0 1.70236322
9 1.68145 0 0 1.681456955
10 1.67686 1 0 1.676860542
11 1.67463 0 6.25 4.575363528
12 1.65031 0 0.2547 1.395619965
13 1.62585 0 0.987 0.638855188
14 1.61903 0 0.3654 1.253634704
15 1.61547 0 0.6251 0.990376191
16 1.62585 0 0.369 1.256855188
17 1.63979 0 0 1.639792697
18 1.64438 0 0 1.64438911
19 1.64913 0 0 1.649133794
20 1.65031 0 0 1.650319965
但是当我将encryptedtext发送到另一个函数时,它不起作用。在这里你可以看到解密功能。我使用相同的托管密钥加密后,从第一个函数调用它。
const int keyLength = 256;
String^ cryptoLib::CLIWrapper::encrypt(String^ text, String^ key)
{
std::string unmanagedText = msclr::interop::marshal_as<std::string>(text);
std::string unmanagedKey = msclr::interop::marshal_as<std::string>(key);
const unsigned char* inputArray = (unsigned char*)(unmanagedText.c_str());
const unsigned char* keyArray = (unsigned char*)(unmanagedKey.c_str());
size_t inputLength = unmanagedText.length();
const size_t encslength = ((inputLength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
unsigned char *enc_out = (unsigned char*)malloc(sizeof(unsigned char) *encslength);
unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char) *encslength);
memset(enc_out, 0, sizeof(encslength));
memset(dec_out, 0, sizeof(dec_out));
AES_KEY dec_key, enc_key;
AES_set_encrypt_key(keyArray, keyLength, &enc_key);
AES_encrypt(inputArray, enc_out, &enc_key);
//Decrypt in same function works!
AES_set_decrypt_key(keyArray, keyLength, &dec_key);
AES_decrypt(enc_out, dec_out, &dec_key);
//test in other function
decrypt("", key, enc_out, encslength, inputLength);
std::string output = std::string(reinterpret_cast<const char*>(dec_out));
String^ returnValue = msclr::interop::marshal_as<String^>(output);
return returnValue;
}
任何人都可以帮我解决我的错误吗?