php - En-/decrypt "AES/ECB/PKCS5Padding" in Java -
could tell me how decrypt data (using java) has been encrypted php function?
php code
public function pad($data, $blocksize = 16) { $pad = $blocksize - (strlen($data) % $blocksize); return $data . str_repeat(chr($pad), $pad); } public function decryptecb($data) { return mcrypt_decrypt(mcrypt_rijndael_128, self::blob_encryption_key, self::pad($data), mcrypt_mode_ecb); } public function encryptecb($data) { return mcrypt_encrypt(mcrypt_rijndael_128, self::blob_encryption_key, self::pad($data), mcrypt_mode_ecb); }
i have tried of things here of them without padding , when add padding don't work.
edit 1:
(from php)
the input looks this: http://pastebin.com/2cyig9nh
key this:
m02cnq51ji97vwt4
and output this: http://pastebin.com/xca50ugh
(the java code)
public class mcrypt { private secretkeyspec keyspec; private cipher cipher; private string secretkey = "m02cnq51ji97vwt4"; public mcrypt() { keyspec = new secretkeyspec(secretkey.getbytes(), "aes"); try { cipher = cipher.getinstance("aes/ecb/pkcs5padding"); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (nosuchpaddingexception e) { e.printstacktrace(); } } public string encrypt(string text) throws exception { if (text == null || text.length() == 0) throw new exception("empty string"); byte[] encrypted = null; try { cipher.init(cipher.encrypt_mode, keyspec ); encrypted = cipher.dofinal(padstring(text).getbytes()); } catch (exception e) { throw new exception("[encrypt] " + e.getmessage()); } return base64.encodebase64string(encrypted); } public byte[] decrypt(string code) throws exception { if (code == null || code.length() == 0) throw new exception("empty string"); byte[] decrypted = null; try { cipher.init(cipher.decrypt_mode, keyspec ); decrypted = cipher.dofinal(new base64().decode(code.getbytes())); } catch (exception e) { throw new exception("[decrypt] " + e.getmessage()); } return decrypted; } private static string padstring(string source) { char paddingchar = ' '; int size = 16; int x = source.length() % size; int padlength = size - x; (int = 0; < padlength; i++) { source += paddingchar; } return source; } }
you encoding , decoding base64 in java code, php code not seem perform encoding/decoding whatsoever. seems confirmed posted on pastebin. if want use strings instead of bytes - bytes input accepted modern ciphers - should make sure (character) encoding correct on both sides. if want use bytes, don't decode binary in java - input in bytes, not text.
Comments
Post a Comment