openssl_encrypt 256 CBC raw_data em java
Eu tento fazer um PHP openssl_encrypt aes-256-cbc com OPENSSL_RAW_DATA em java 6 sem sucesso. Encontrei algum tópico sobre isso, mas consegui apenas fazê-lo no aes-128-cbc sem raw_data. O melhor tópico que eu fundei sobre isso é:AES-256 CBC criptografa em php e descriptografa em Java ou vice-versa Mas o raw_data não funciona e a chave de 256 bits é gerada aleatoriamente. De fato, a versão Php é:
<?php>
openssl(
"hello",
"aes-256-cbc",
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
OPENSSL_RAW_DATA,
"aaaaaaaaaaaaaaaa"
)
?>
E eu realmente tenho isso:
public static void main(String[] args) {
try {
// 128 bits key
openssl_encrypt("hello", "bbbbbbbbbbbbbbbb", "aaaaaaaaaaaaaaaa");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static String openssl_encrypt(String data, String strKey, String strIv) throws Exception {
Base64 base64 = new Base64();
Cipher ciper = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec key = new SecretKeySpec(strKey.getBytes("UTF-8"), "AES");
IvParameterSpec iv = new IvParameterSpec(strIv.getBytes("UTF-8"), 0, ciper.getBlockSize());
// Encrypt
ciper.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] encryptedCiperBytes = base64.encode((ciper.doFinal(data.getBytes())));
String s = new String(encryptedCiperBytes);
System.out.println("Ciper : " + s);
return s;
}