Decrypt ( with PHP ) a Java encryption ( PBEWithMD5AndDES )
Asked Answered
I

2

0

Someone asked me to decrypt with PHP a string encrypted with the following Java Class.

public class CryptoLibrary {

private Cipher encryptCipher;
private sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

public CryptoLibrary() throws SecurityException{

    java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());

    char[] pass = "NNSHHETJKKSNKH".toCharArray();
    byte[] salt = {
    (byte) 0xa3, (byte) 0x21, (byte) 0x24, (byte) 0x2c,
    (byte) 0xf2, (byte) 0xd2, (byte) 0x3e, (byte) 0x19 };

    init(pass, salt, iterations);

}

public void init(char[] pass, byte[] salt, int iterations)throws SecurityException{

        PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
        SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(pass));

        encryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        encryptCipher.init(Cipher.ENCRYPT_MODE, k, ps);
    }
}

public synchronized String encrypt(String str)  throws SecurityException{
    if(str!=null){
        byte[] utf8 = str.getBytes("UTF8");
        byte[] enc = encryptCipher.doFinal(utf8);
        return encoder.encode(enc);
    }
    else {
        return null;
    }
}
}

I don't know any Java so I need some help to understand this encryption.

1) what is the meaning of this line? PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt,20);

2) what value should I use for the first parameter of string mcrypt_encrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] )

3) When should I use MD5 in my php script?

Indogermanic answered 24/4, 2012 at 14:38 Comment(2)
Both MD5 and DES have known vulnerabilities and should not be used.Aguila
MD5 is actually fine for key derivation, single DES is only fine for real time, short lived encryption purposes (which is basically never). Both should be avoided of course, especially if you don't know what you are doing.Lenwood
L
0

1) It creates the parameters for Password Based Encryption, the salt, which is included in the hash calculations, and the number of iterations that the hash method is executed (on it's own output). It is used to defeat rainbow table attacks, basically an attacker has to go through the same number of iterations to check if the password is correct, and he cannot use a precalculated table because the salt will be different for each password (so you cannot see if somebody has the same password as another user).

2) MCRYPT_DES, and you will need MCRYPT_MODE_CBC for the mode, and PKCS#5 padding of course.

3) Only when you are absolutely sure that its weaknesses are not exposed or when absolutely required for compatibility. Fortunately, it is relatively secure for key derivation functions. Download a pbkdf1 method for PHP and put it in there - if not already included.

Lenwood answered 25/4, 2012 at 20:22 Comment(0)
F
2

I had to do the same thing for a customer of mine and wrote a few lines of code to help with issue: https://github.com/kevinsandow/PBEWithMD5AndDES

Folksy answered 28/1, 2014 at 13:18 Comment(3)
Hi Kevin, I tried using your algorithm to decrypt a string econded by java. But I dont understand what salt to use. In Java Salt is : byte[] salt = { (byte) 0xa3, (byte) 0x21, (byte) 0x24, (byte) 0x2c, (byte) 0xf2, (byte) 0xd2, (byte) 0x3e, (byte) 0x19 }; So as per you application what sholud i use as saltHereby
If my java team is using the above byte array as salt how can i get the equivalent salt to use in phpHereby
$salt = 'a321242cf2d23e19'; should do the trick, in PHP the salt is simply written without the "(byte) 0x"Folksy
L
0

1) It creates the parameters for Password Based Encryption, the salt, which is included in the hash calculations, and the number of iterations that the hash method is executed (on it's own output). It is used to defeat rainbow table attacks, basically an attacker has to go through the same number of iterations to check if the password is correct, and he cannot use a precalculated table because the salt will be different for each password (so you cannot see if somebody has the same password as another user).

2) MCRYPT_DES, and you will need MCRYPT_MODE_CBC for the mode, and PKCS#5 padding of course.

3) Only when you are absolutely sure that its weaknesses are not exposed or when absolutely required for compatibility. Fortunately, it is relatively secure for key derivation functions. Download a pbkdf1 method for PHP and put it in there - if not already included.

Lenwood answered 25/4, 2012 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.