Monday, 15 February 2010

C# PHP AES 256 CBC Encryption -



C# PHP AES 256 CBC Encryption -

hello i'm trying encrypt files/strings on server using php , aes 256 cbc mode, because of strings ending '\0' can easly remove padding them aes adds, files cannot because of them contain null bytes. before send info encode base64 string.

here c# decrypt function

internal static byte[] __aes_decrypt(byte[] input, string _key, string _iv) { var myrijndael = new rijndaelmanaged() { padding = paddingmode.zeros, mode = ciphermode.cbc, keysize = 256, blocksize = 256 }; byte[] key = encoding.ascii.getbytes(_key); byte[] iv = encoding.ascii.getbytes(_iv); var decryptor = myrijndael.createdecryptor(key, iv); var sencrypted = input; var fromencrypt = new byte[sencrypted.length]; var msdecrypt = new memorystream(sencrypted); var csdecrypt = new cryptostream(msdecrypt, decryptor, cryptostreammode.read); csdecrypt.read(fromencrypt, 0, fromencrypt.length); homecoming fromencrypt; }

this function works fine strings , bytes too. believe php encrypt function wrong files works strings.

function encrypt($str) { $key = 'keygoeshere'; $iv = "ivgoeshere"; $str =mcrypt_encrypt(mcrypt_rijndael_256, $key, $str, mcrypt_mode_cbc, $iv); //$str = str_replace("\0","",$str); works strings not files. homecoming base64_encode($str); }

if don't want alter form of padding, append single 1 byte end of file contents, , on decryption, after removing trailing nulls, remove appended 1 byte. won't accidently remove 0 bytes belong file.

c# php encryption aes

No comments:

Post a Comment