Я создаю приложение, которое вызывает сторонний API, они предоставили код PHP, а нам нужен код Java.
Код PHP приведен ниже:
function encryptIt( $string ) {
$key = 'qJB0rGtIn5UB1xG03efyCp';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = openssl_encrypt($string, 'aes-256-cbc', $key, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}
Теперь я изменил код abouve на java следующим образом:
private static String openssl_encrypt(String data, String strKey, String strIv) throws Exception {
SecureRandom randomSecureRandom = new SecureRandom();
Base64 base64 = new Base64();
Cipher ciper = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] ivv = new byte[ciper.getBlockSize()];
randomSecureRandom.nextBytes(ivv);
SecretKeySpec key = new SecretKeySpec(strKey.getBytes(), "AES");
IvParameterSpec iv = new IvParameterSpec(ivv, 0, ciper.getBlockSize());
// Encrypt
ciper.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] encryptedCiperBytes = ciper.doFinal(data.getBytes());
String s = new String(base64.encode(encryptedCiperBytes));
System.out.println("Ciper : " + s);
return s;
}
На самом деле я не очень хорош в методах шифрования, поэтому код PHP создает код IV, но после поиска в Интернете я попытался создать код IV, как указано выше,
Но я получил ошибку как:
java.security.InvalidKeyException: Invalid AES key length: 22 bytes
at com.sun.crypto.provider.AESCipher.engineGetKeySize(AESCipher.java:509)
at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1067)
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1038)
at javax.crypto.Cipher.init(Cipher.java:1393)
at javax.crypto.Cipher.init(Cipher.java:1327)
Пожалуйста, помогите мне решить эту проблему
ОБНОВЛЕНО:
Я обновил код до:
private static String openssl_encrypt(String data) throws Exception {
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
KeySpec spec = new PBEKeySpec("qJB0rGtIn5UB1xG03efyCp".toCharArray(), salt, 65536, 256); // AES-256
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] key = f.generateSecret(spec).getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
byte[] ivBytes = new byte[16];
random.nextBytes(ivBytes);
IvParameterSpec iv = new IvParameterSpec(ivBytes);
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, keySpec, iv);
byte[] encValue = c.doFinal(data.getBytes());
byte[] finalCiphertext = new byte[encValue.length+2*16];
System.arraycopy(ivBytes, 0, finalCiphertext, 0, 16);
System.arraycopy(salt, 0, finalCiphertext, 16, 16);
System.arraycopy(encValue, 0, finalCiphertext, 32, encValue.length);
String s = new String(Base64.getEncoder().encodeToString(finalCiphertext));
return s;
}
Тем не менее я получил сообщение об ошибке:
java.security.InvalidKeyException: недопустимый размер ключа
Мои провайдеры API говорят, что это ключ!