c# - Compare AES encryption OpenSSL versus .NET(RijndaelManaged) -


i need encrypt url. according specs of url should (rijndael) encoded using cbc encryption mode, using iv of 0., pkcs7 padding , key length of 128-bit.

the decryption of url done in .net environment using rijndaelmanaged class. encrypt using openssl 1.0.2a (c++ unmanaged) , use following code (from internet):

// ctx holds state of encryption algorithm doesn't // reset initial state while encrypting more 1 block. evp_cipher_ctx ctx; evp_cipher_ctx_init(&ctx);  unsigned char key[] = {0x41, 0x41, 0x45, 0x43, 0x41, 0x77, 0x51, 0x46,                                      0x43, 0x67, 0x63, 0x49, 0x43, 0x5a, 0x6f, 0x4c }; unsigned char iv[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; assert(sizeof(key) == 16);  // aes128 key size assert(sizeof(iv) == 16);   // iv aes block size  // if data isn't multiple of 16, default behavior pad // n bytes of value n, n number of padding bytes required // make data multiple of block size.  pkcs7 padding. // output multiple of block size. std::string plain("someid=007&accountno=119955244351&user=admin&"); std::vector<unsigned char> encrypted; size_t max_output_len = plain.length() + 16 - (plain.length() % 16); encrypted.resize(max_output_len);  // enc 1 encrypt, 0 decrypt, or -1 (see documentation). evp_cipherinit_ex(&ctx, evp_aes_128_cbc(), null, key, iv, 1);  // evp_cipherupdate can encrypt data @ once, or can // small chunks @ time. int actual_size = 0; if( !evp_cipherupdate(&ctx,                 &encrypted[0], &actual_size,                 reinterpret_cast<unsigned char *>(&plain[0]), plain.size())) {     evp_cipher_ctx_cleanup(&ctx); }  // evp_cipherfinal_ex applies padding.  if data // multiple of block size, you'll aes block filled // nothing padding. int final_size; evp_cipherfinal_ex(&ctx, &encrypted[actual_size], &final_size); actual_size += final_size;  encrypted.resize(actual_size);  for( size_t index = 0; index < encrypted.size(); ++index ) {     std::cout << std::hex << std::setw(2) << std::setfill('0') <<                     static_cast<unsigned int>(encrypted[index]); } std::cout << "\n";  evp_cipher_ctx_cleanup(&ctx);  aeswrapper aeswrapper; std::string encryptedbase64(aeswrapper.base64encode( &encrypted[0], encrypted.size())); 

i've checked (obviously) key, iv , algorithm aes-cbc-128 , afaik openssl uses pkcs7 padding default, result doesn't match!

striking padding doesn't seem happen in evp_cipherfinal_ex.the padding should consist of required number of bytes correct blocksize, filling each byte number. appears filled random data (or maybe more encryption?)

is there issue code, can explain why encryption not 'correct'? should focus on incorrect padding , how debug in case?

any pointers?

nevermind. got working. c# application decrypts url uses sha1 hash on key. after hashed key before encrypting url started working.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -