java - 256-bit AES decryption in PHP -
i have working example in java how decrypt (256-bit aes) string using key. need replicate decrypt algorithm in php , got work.
this decrypted message in java:
csm(mcl/kex rcv/10001031 org/comdata kyn/dsnax6hftkvfcet key/c062e276949d83554d7b5198c52c4c55ed1c65370fa71f8220538a0c3ba23172)
yet when call php method gives
10001031 org/comdata kyn/dsnax6hftkvfcet key/c062e276949d83554d7b5198c52c4c55ed1c65370fa71f8220538a0c3ba23172)
as can see it's not far off php method doesn't give first 17 characters (i.e. "csm(mcl/kex rcv/" bit).
am missing trivial here?
this php class:
class opensslaes { const method = 'aes-256-cbc'; public static function encrypt($message, $key) { if (mb_strlen($key, '8bit') !== 32) { throw new exception("needs 256-bit key!"); } $ivsize = openssl_cipher_iv_length(self::method); $iv = openssl_random_pseudo_bytes($ivsize); $ciphertext = openssl_encrypt( $message, self::method, $key, openssl_raw_data, $iv ); return $iv.$ciphertext; } public static function decrypt($message, $key) { if (mb_strlen($key, '8bit') !== 32) { throw new exception("needs 256-bit key!"); } $ivsize = openssl_cipher_iv_length(self::method); $iv = mb_substr($message, 0, $ivsize, '8bit'); $ciphertext = mb_substr($message, $ivsize, null, '8bit'); return openssl_decrypt( $ciphertext, self::method, $key, openssl_raw_data, $iv ); } } $class = new opensslaes(); var_dump($class->decrypt(base64_decode("cbvlmjbttr7dkw8fhhtqjolymbnrgxpijsgffpjka/4mwxmiudonyzs4wuxihujtgggk4czrkj1g60r4owbljntma9atpkh9pxe7wxawjfe9zc698bqv4ldkxrme+q4xcb3bk/ugq/bpvikmrydhcbvihxnzgd36nn40giigy/g="), hex2bin("secret_key_goes_here")));
this java class:
package javaapplication1; import org.apache.commons.codec.binary.base64; import org.apache.commons.codec.binary.stringutils; import org.apache.commons.codec.binary.hex; import java.security.invalidalgorithmparameterexception; import java.security.invalidkeyexception; import java.security.nosuchalgorithmexception; import java.security.securerandom; import javax.crypto.badpaddingexception; import javax.crypto.cipher; import javax.crypto.illegalblocksizeexception; import javax.crypto.keygenerator; import javax.crypto.nosuchpaddingexception; import javax.crypto.secretkey; import javax.crypto.spec.ivparameterspec; import javax.crypto.spec.secretkeyspec; import javax.xml.bind.datatypeconverter; public class javaapplication1 { public static void main(string[] args) { byte[] encryption_key = javaapplication1.tobytearray("secret_key_goes_here"); string input = new string("cbvlmjbttr7dkw8fhhtqjolymbnrgxpijsgffpjka/4mwxmiudonyzs4wuxihujtgggk4czrkj1g60r4owbljntma9atpkh9pxe7wxawjfe9zc698bqv4ldkxrme+q4xcb3bk/ugq/bpvikmrydhcbvihxnzgd36nn40giigy/g="); try { system.out.println(javaapplication1.decryptstringaes(input, encryption_key)); } catch (exception exception) { system.out.println("error occured: " + exception); } } public static byte[] tobytearray(string s) { return datatypeconverter.parsehexbinary(s); } public static string decryptstringaes(string input, byte[] key) throws exception { byte[] iv = javaapplication1.tobytearray("00000000000000000000000000000000"); byte[] inputbytes = base64.decodebase64(input.getbytes()); cipher decryptcipher = cipher.getinstance("aes/cbc/pkcs5padding"); decryptcipher.init(cipher.decrypt_mode, new secretkeyspec(key, "aes"), new ivparameterspec(iv)); byte[] decrypt = decryptcipher.dofinal(inputbytes); return new string(decrypt); } }
your java implementation expects iv zeros, whereas php implementation expects iv prepended message.
if want php implementation compatible java code, change this:
class opensslaes { const method = 'aes-256-cbc'; public static function encrypt($message, $key) { if (mb_strlen($key, '8bit') !== 32) { throw new exception("needs 256-bit key!"); } $ivsize = openssl_cipher_iv_length(self::method); $iv = hex2bin('00000000000000000000000000000000'); return openssl_encrypt( $message, self::method, $key, openssl_raw_data, $iv ); } public static function decrypt($message, $key) { if (mb_strlen($key, '8bit') !== 32) { throw new exception("needs 256-bit key!"); } $ivsize = openssl_cipher_iv_length(self::method); $iv = hex2bin('00000000000000000000000000000000'); return openssl_decrypt( $message, self::method, $key, openssl_raw_data, $iv ); } } $class = new opensslaes(); var_dump($class->decrypt(base64_decode("cbvlmjbttr7dkw8fhhtqjolymbnrgxpijsgffpjka/4mwxmiudonyzs4wuxihujtgggk4czrkj1g60r4owbljntma9atpkh9pxe7wxawjfe9zc698bqv4ldkxrme+q4xcb3bk/ugq/bpvikmrydhcbvihxnzgd36nn40giigy/g="), hex2bin("secret_key_goes_here")));
Comments
Post a Comment