I created a Helper function for encrypt data with OpenSSL in PHP.
private const METHOD = 'AES-256-CFB';
private const KEY = 'g5(Mt2-2x=wsS8^K';
public static function encrypt($data) {
$crypted = openssl_encrypt($data, self::METHOD, self::KEY, 1, substr(hash('sha256', self::KEY_1), 0, 16));
return base64_encode($crypted);
}
For decrypt the AES-CFB-256 data in Java, i've made a Helper class too.
private String decrypt(String sdata) throws Exception {
String sKey = "g5(Mt2-2x=wsS8^K";
byte[] key = sKey.getBytes();
byte[] iv = Arrays.copyOfRange(hash("SHA-256", key).getBytes(), 0, 16);
byte[] data = Base64.getDecoder().decode(sdata.getBytes());
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
SecretKey aesSecret = new SecretKeySpec(key, "AES");
IvParameterSpec ivps = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, aesSecret, ivps);
byte[] result = cipher.doFinal(data);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println(new String(result, StandardCharsets.UTF_8) + " : " + sb.toString());
return sb.toString();
}
private String hash(String type, byte[] data) throws Exception {
MessageDigest digest = MessageDigest.getInstance(type);
data = digest.digest(data);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.length; i++) {
sb.append(Integer.toString((data[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
I noticed that the hash function in JAVA works and gives me the same result as the hash function in PHP. The problem is that the result of the cipher is totally different and I don't see were is the mistake.
Thanks for your help