我在Java中有以下代码,我想将其转换为PHP。 有人可以告诉我该如何处理吗?
private static Cipher initCipher(final int mode, final String initialVectorString, final String secretKey)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException {
final SecretKeySpec skeySpec = new SecretKeySpec(secretKey.getBytes(), "AES");
final IvParameterSpec initialVector = new IvParameterSpec(initialVectorString.getBytes());
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(mode, skeySpec, initialVector);
return cipher;
}
public static String encrypt(final String dataToEncrypt) {
String encryptedData = null;
try {
// Initialize the cipher
final Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, iv, secretKey);
// Encrypt the data
final byte[] encryptedByteArray = cipher.doFinal(dataToEncrypt.getBytes());
// Encode using Base64
encryptedData = (new BASE64Encoder()).encode(encryptedByteArray);
} catch (Exception e) {
System.err.println("Problem encrypting the data");
e.printStackTrace();
}
return encryptedData;
}