-1

In AES Encryption logic in Java I can see the following line

new SecretKeySpec(getMD5(key), "AES")

private static byte[] getMD5(String input) {
    try {
        byte[] bytesOfMessage = input.getBytes("UTF-8");
        MessageDigest md = MessageDigest.getInstance("MD5");
        return md.digest(bytesOfMessage);
    } catch (Exception e) {
    }
    return null;
}

In php when I use

$hash= md5($str, TRUE);
unpack('c*', $hash);

I get the getMD5() byte array equivalent in PHP. How can I proceed further with the following code?

public AesEncrypt(String key) {
        SecretKeySpec skey = new SecretKeySpec(getMD5(key), "AES");
        setupCrypto(skey);
    }

    private void setupCrypto(SecretKey key) {
        byte[] iv = {
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        System.out.println("key:  " + key);
        System.out.println("iv:  " + iv);

        // String iv = "fedcba9876543210";
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
        System.out.println("paramspec:  " + paramSpec);
        try {
            this.ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            this.dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            System.out.println("this.ecipher:  " + this.ecipher);
            this.ecipher.init(1, key, paramSpec);
            this.dcipher.init(2, key, paramSpec);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
genesis
  • 37
  • 9
  • There is no equivalent in PHP for `SecretKeySpec`. How do you want to use it? – Artjom B. Aug 20 '15 at 09:58
  • I want to implement AES encryption(CBC mode) in php.I have developed getMD5() equivqlent in php.how should I proceed furthur? – genesis Aug 20 '15 at 10:01
  • 1
    You've probably run into a comment stating that StackOverflow is not a code translation service, so now you're asking a question for each line, but that won't get you far, because the APIs are different enough that there isn't a single line that can be used as a replacement. Please try to solve it yourself before posting an answerable question. For starters see: http://stackoverflow.com/questions/4537099/problem-with-aes-256-between-java-and-php – Artjom B. Aug 20 '15 at 10:08
  • If I am passing byte[] iv = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; in Java how would I use the same iv in PHP,provided PHP allows IV in string format – genesis Aug 20 '15 at 10:15
  • If you look at my answer, you see you can generate an iv with builtin functions. – David Aug 21 '15 at 10:24

1 Answers1

1

This is what you seek:

http://blog.turret.io/the-missing-php-aes-encryption-example/

I copied the example code down below. You could also have a look at mcrypt on php.net.

<?php
// DEFINE our cipher
define('AES_256_CBC', 'aes-256-cbc');
// Generate a 256-bit encryption key
// This should be stored somewhere instead of recreating it each time
$encryption_key = openssl_random_pseudo_bytes(32);
// Generate an initialization vector
// This *MUST* be available for decryption as well
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(AES_256_CBC));
// Create some data to encrypt
$data = "Encrypt me, please!";
echo "Before encryption: $data\n";
// Encrypt $data using aes-256-cbc cipher with the given encryption key and 
// our initialization vector. The 0 gives us the default options, but can
// be changed to OPENSSL_RAW_DATA or OPENSSL_ZERO_PADDING
$encrypted = openssl_encrypt($data, AES_256_CBC, $encryption_key, 0, $iv);
echo "Encrypted: $encrypted\n";
// If we lose the $iv variable, we can't decrypt this, so append it to the 
// encrypted data with a separator that we know won't exist in base64-encoded 
// data
$encrypted = $encrypted . ':' . $iv;
// To decrypt, separate the encrypted data from the initialization vector ($iv)
$parts = explode(':', $encrypted);
// $parts[0] = encrypted data
// $parts[1] = initialization vector
$decrypted = openssl_decrypt($parts[0], AES_256_CBC, $encryption_key, 0, $parts[1]);
echo "Decrypted: $decrypted\n";
?>
David
  • 1,227
  • 12
  • 23