Tuesday, 15 April 2014

Decrypt AES - C# .NET from OpenSSL -



Decrypt AES - C# .NET from OpenSSL -

i need know how encrypt message in aes-openssl , decrypt in .net (c# or vb) or know difference between aes-openssl , aes-.net

thank you!

code:

code in vb.net

public function aes_decrypt(byval prm_key string, byval prm_iv string, byval prm_text_to_decrypt string)

dim sencryptedstring string = prm_text_to_decrypt dim myrijndael new rijndaelmanaged myrijndael.padding = paddingmode.zeros myrijndael.mode = ciphermode.cbc myrijndael.keysize = 256 myrijndael.blocksize = 256 dim key() byte dim iv() byte key = system.text.encoding.ascii.getbytes(prm_key) iv = system.text.encoding.ascii.getbytes(prm_iv) dim decryptor icryptotransform = myrijndael.createdecryptor(key, iv) dim sencrypted byte() = convert.frombase64string(sencryptedstring) dim fromencrypt() byte = new byte(sencrypted.length) {} dim msdecrypt new memorystream(sencrypted) dim csdecrypt new cryptostream(msdecrypt, decryptor, cryptostreammode.read) csdecrypt.read(fromencrypt, 0, fromencrypt.length) homecoming (system.text.encoding.ascii.getstring(fromencrypt)) end function

in comment, inquire way encrypt in c# , decrypt in openssl. here's a implementation of evp_bytestokey in c#.

now have generate random byte array in c#, utilize these functions (evp on openssl side , sec 1 in c#) on both sides mutual random byte array.

beware tough, you have utilize same hash algorithm: in given link, md5 used. might have alter sha1 depending on 1 evp_bytestokey using (or other way round). same way, have adapt key , iv size in derive algorithm given in post depending on needs, here 32 , 32.

hope helped.

edit: forgot. owlstead said in comment, rijndael allows utilize block size of 256 bits. however, aes block size fixed 128 bits, block size must 128 bits , iv 16 bytes.

there grab when wish utilize salt. openssl prepends encrypted byte array base64 encryption of "salt__" , actual salt array. can find illustration in this post.

c# openssl aes

No comments:

Post a Comment