.net - OpenSSL AES cant decrypt -
i have e problem decrypt openssl aes encrypted text.
the following codeblock shows how encrypt text. on cli send managed string encrypt , key this. when use key use encrypt @ same funktion works wonderfully. when @ dec_out in same function decrypted value same input.
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; }
but when send encryptedtext function doesnt works. here can see decryption function. call first function after encryption same managed key.
string ^ cryptolib::cliwrapper::decrypt(string ^ enctext, string^ key, unsigned char* enc_out, size_t enclength, size_t inputlength) { std::string unmanagedkey = msclr::interop::marshal_as<std::string>(key); const unsigned char* keyarray = (unsigned char*)(unmanagedkey.c_str()); unsigned char *dec_out = (unsigned char*)malloc(sizeof(unsigned char) *enclength); memset(dec_out, 0, sizeof(dec_out)); aes_key dec_key, enc_key; aes_set_decrypt_key(keyarray, keylength, &dec_key); aes_decrypt(enc_out, dec_out, &enc_key); return ""; }
can me doing wrong ?
Comments
Post a Comment