AES128 Encryption using PHP mcrypt is not outputting same as ASP.net -



AES128 Encryption using PHP mcrypt is not outputting same as ASP.net -

i have php application needs encrypt challenge string consumed asp.net web service output of php implementation not decrypted .net. in question relating ios , .net: aes128 bit encryption string not similar on .net

why outputs different , can php output same .net?

my php code looks this:

$key = '128bit-16bytekey'; $value = '128bit-16byteval'; function fnencrypt($value, $key) { $ivsize = mcrypt_get_iv_size(mcrypt_rijndael_128, mcrypt_mode_ecb); $iv = mcrypt_create_iv($ivsize); homecoming base64_encode( mcrypt_encrypt( mcrypt_rijndael_128, $key, $value, mcrypt_mode_ecb,$iv )); }

.net this

public static string encryptdata(string plaintext) { string encryptionkey = appconstants.aes_encryptdecrypt_key; // convert our plaintext byte array. // allow assume plaintext contains utf8-encoded characters. byte[] plaintextbytes = encoding.utf8.getbytes(plaintext); // utilize password generate pseudo-random bytes encryption // key. specify size of key in bytes (instead of bits). asciiencoding encoding = new asciiencoding(); byte[] keybytes = new byte[16]; byte[] tempkey = encoding.getbytes(encryptionkey); (int = 0; < keybytes.length; i++) { if (i < tempkey.length) { keybytes[i] = tempkey[i]; } else { keybytes[i] = 0; } } // create uninitialized rijndael encryption object. rijndaelmanaged symmetrickey = new rijndaelmanaged(); //aesmanaged symmetrickey = new aesmanaged(); //symmetrickey.padding = paddingmode.pkcs7; // reasonable set encryption mode cipher block chaining // (cbc). utilize default options other symmetric key parameters. symmetrickey.mode = ciphermode.ecb; // generate encryptor existing key bytes , initialization // vector. key size defined based on number of key // bytes. //icryptotransform encryptor = symmetrickey.createencryptor(keybytes,initvectorbytes); icryptotransform encryptor = symmetrickey.createencryptor(keybytes, null); // define memory stream used hold encrypted data. memorystream memorystream = new memorystream(); // define cryptographic stream (always utilize write mode encryption). cryptostream cryptostream = new cryptostream(memorystream, encryptor, cryptostreammode.write); // start encrypting. cryptostream.write(plaintextbytes, 0, plaintextbytes.length); // finish encrypting. cryptostream.flushfinalblock(); // convert our encrypted info memory stream byte array. byte[] ciphertextbytes = memorystream.toarray(); // close both streams. memorystream.close(); cryptostream.close(); // convert encrypted info base64-encoded string. string ciphertext = convert.tobase64string(ciphertextbytes); // homecoming encrypted string. homecoming ciphertext; }

sample outputs

php : q54np/txq2rdtuwww4ckkg==

.net : q54np/txq2rdtuwww4ckkpst9cqiizsg2xsqendcqc8=

php : dqzdab/labxvooocdnm6hq==

.net : dqzdab/labxvooocdnm6hzst9cqiizsg2xsqendcqc8=

as in question ref'd above right hand side of .net output same , left side consistent between 2 implementations. pretty sure iv irrelevant , how padding handled in mcrypt.

if decrypt either of outputs in php returns same right result.

can shed light? unable alter .net app. thanks!

this happens due padding applied in .net. can overcome adding next line .net code:

class="lang-c# prettyprint-override">symmetrickey.padding = paddingmode.none;

or can alter php code same encrypted string:

class="lang-php prettyprint-override">// add together lines below fnencrypt function $block = mcrypt_get_block_size(mcrypt_rijndael_128, mcrypt_mode_ecb); $len = strlen($value); $padding = $block - ($len % $block); $value .= str_repeat(chr($padding),$padding);

a similar issue described in 1 of comments php manual pages: http://www.php.net//manual/en/function.mcrypt-encrypt.php#47973

php asp.net encryption mcrypt

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -