c# 4.0 - PHP MCRYPT_RIJNDAEL_128 encryption in C# -
i'm trying rewrite function in c#. c# output mismatches php
php version
// encrypt data using aes128-cbc function encrypt($data, $key, $iv) { $cipher = mcrypt_module_open(mcrypt_rijndael_128, '', 'cbc', ''); mcrypt_generic_init($cipher, $key, $iv); $multipass = mcrypt_generic($cipher, $data); mcrypt_generic_deinit($cipher); return $multipass; }
c# version
public static string encrypt(string encryptionstring, string iv, string key) { byte[] cleartextbytes = encoding.utf8.getbytes(encryptionstring); var rijn = symmetricalgorithm.create(); rijn.keysize = 128; rijn.mode = ciphermode.cbc; var ms = new memorystream(); var cs = new cryptostream(ms, rijn.createencryptor(encoding.utf8.getbytes(key), encoding.utf8.getbytes(iv)), cryptostreammode.write); cs.write(cleartextbytes, 0, cleartextbytes.length); cs.close(); var tmp = encoding.utf8.getstring(ms.toarray()); return tmp; }
encrypt/decrypt using php:
class cipher { private $key, $iv; function __construct() { $this->key = "edrtjfjfjlldldld"; $this->iv = "56666852251557009888889955123458"; } function encrypt($text) { $block = mcrypt_get_block_size(mcrypt_rijndael_256, mcrypt_mode_cbc); $padding = $block - (strlen($text) % $block); $text .= str_repeat(chr($padding), $padding); $crypttext = mcrypt_encrypt(mcrypt_rijndael_256, $this->key, $text, mcrypt_mode_cbc, $this->iv); return base64_encode($crypttext); } function decrypt($input) { $dectext = mcrypt_decrypt(mcrypt_rijndael_256, $this->key, base64_decode($input), mcrypt_mode_cbc, $this->iv); return $dectext; } }
encrypt/decrypt using c#:
public class rijndaelsimple { const string iv = "56666852251557009888889955123458"; const string key = "edrtjfjfjlldldld"; static public string encryptrj256(string plaintext) { var encoding = new utf8encoding(); var key = encoding.getbytes(key); var iv = encoding.getbytes(iv); byte[] encrypted; using (var rj = new rijndaelmanaged()) { try { rj.padding = paddingmode.pkcs7; rj.mode = ciphermode.cbc; rj.keysize = 256; rj.blocksize = 256; rj.key = key; rj.iv = iv; var ms = new memorystream(); using (var cs = new cryptostream(ms, rj.createencryptor(key, iv), cryptostreammode.write)) { using (var sr = new streamwriter(cs)) { sr.write(plaintext); sr.flush(); cs.flushfinalblock(); } encrypted = ms.toarray(); } } { rj.clear(); } } return convert.tobase64string(encrypted); } static public string decryptrj256(string input) { byte[] cypher = convert.frombase64string(input); var sret = ""; var encoding = new utf8encoding(); var key = encoding.getbytes(key); var iv = encoding.getbytes(iv); using (var rj = new rijndaelmanaged()) { try { rj.padding = paddingmode.pkcs7; rj.mode = ciphermode.cbc; rj.keysize = 256; rj.blocksize = 256; rj.key = key; rj.iv = iv; var ms = new memorystream(cypher); using (var cs = new cryptostream(ms, rj.createdecryptor(key, iv), cryptostreammode.read)) { using (var sr = new streamreader(cs)) { sret = sr.readline(); } } } { rj.clear(); } } return sret; } }
Comments
Post a Comment