Encrypt with JAVA (jasypt) and Decrypt with PHP - Whats the less vulnerable Algorithm?
Im working on a legacy system that has the following tasks:
1) The Java Application saves some encrypted data on a mysql database. This happens very rarely. The data is saved once and rarely updated.
2) A PHP page loads that encrypted data from the mysql database and uses it for internal logic. This php page must be able to decrypt it internally, but not to encrypt it.
3) The java Application also loads the encrypted data from the mysql database and decrypts it for internal purposes.
In another words, I have a Java application that encrypts and decrypts data. And I have a php single page that must be able to decrypt the data.
Currently, I must re-do this with new crypto algorithms. I researched at stackoverflow and many are saying to stay away from MD5 and DES. As far as I understood, I must go with AES, So Ive came up with the following java CODE below. However:
a) Im unsure how to decrypt with php, I normally use openSSL but I dont know the equivalent algo name in php.
<?php
ini_set('display_errors', 1);
$salt = 'nXdHqFg74g22g4Vq';
$key = $salt; // ? not sure
$data = 'uVJ+m3FGkzFTCQXpZJysmo53rWh5+5L9dWzyyD8xues=';
$method = "AES-256-CFB"; //not sure which
//openssl_decrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string $tag = "" [, string $aad = "" ]]]] ) : string
echo openssl_decrypt($data, "AES-256-CFB", $key);
?>
b) Is this safe enough for general purposes? I dont need anything awesomely secure just enough since this data mostly travel through https.
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.iv.IvGenerator;
import org.jasypt.iv.StringFixedIvGenerator;
public class MyCryptoTest {
//private static final byte[] key = "nXdHqFg74g22g4Vq".getBytes();
private static final byte[] key = {110, 88, 100, 72, 113, 70, 103, 55, 52, 103, 50, 50, 103, 52, 86, 113};
private static PooledPBEStringEncryptor textCryptor = new PooledPBEStringEncryptor();
public static void main(String args[]) throws Exception {
//System.out.println(Arrays.toString(key));
//System.out.println(Arrays.toString("nXdHqFg74g22g4Vq".getBytes(Charset.forName("UTF-8"))));
String input = "stackoverflow";
String x = encrypt(input);
String y = decrypt(x);
System.out.println(x);
System.out.println(y);
System.out.println("Test Result: " + input.equals(y));
}
static {
IvGenerator ivGenerator = new StringFixedIvGenerator("some_random_word?");
textCryptor.setPoolSize(2);
textCryptor.setPassword(new String(key));
textCryptor.setAlgorithm("PBEWithHMACSHA512AndAES_256");
textCryptor.setIvGenerator(ivGenerator);
}
public static String encrypt(String strClearText) throws Exception {
String strData = "";
try {
strData = textCryptor.encrypt(strClearText);
} catch (Exception ex) {
ex.printStackTrace();
throw new Exception(ex);
}
return strData;
}
public static String decrypt(String strEncrypted) throws Exception {
String strData = "";
try {
//System.out.println(strEncrypted);
strData = textCryptor.decrypt(strEncrypted);
} catch (Exception ex) {
throw new Exception(ex);
}
return strData;
}
}
Important Quotes from the references:
Ideally you should move away from DES and since this padding is going to be a problem in PHP, why not see if you can change the encryption algorithm to something less troublesome and more secure?
To help you can show this page: http://www.ietf.org/rfc/rfc4772.txt, where it is succinctly expressed that DES is susceptible to brute force attacks, so has been deprecated and replaced with AES.
Community♦ 111 silver badge answered Dec 17 '13 at 17:15
James Black
Both MD5 and DES have known vulnerabilities and should not be used. – SLaks Apr 24 '12 at 14:45
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. – Maarten Bodewes Apr 25 '12 at 20:06
References:
Decrypt ( with PHP ) a Java encryption ( PBEWithMD5AndDES )