我有一个应用程序,我需要将用户名转换为加密格式,然后在 php 中解密。我的加密代码运行良好。但是在 php 端解密的字符串总是包含两到三个不可读格式的附加字符。我只是不明白它从哪里获取这些附加字符?它与填充有关吗?我正在发布 android 代码和 php 代码。
安卓端代码:
public String encryptStringWithAES(String Message) throws Exception {
String key = "123456789abcdefg";
String iv = "1234567890123456";
String padding = "ZeroBytePadding";
SecretKeySpec spec = new SecretKeySpec(key.getBytes("UTF8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/" + padding);
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, spec, ivspec);
byte[] array = cipher.doFinal(Message.getBytes());
System.out.println("encrypted ARRAY LENGHT:" + array.length);
String encoded_string = android.util.Base64.encodeToString(array, 0,
array.length, Base64.NO_PADDING);
System.out.println("Requested encoded string: " + encoded_string);
return encoded_string;
}
PHP端代码:
$key_for_AES='123456789abcdefg';
$iv='1234567890123456';
$message=$_POST['msg'];
$decoded_string=base64_decode($message);
$decrypted_string=mcrypt_decrypt('rijndael-128',$key_for_AES, $decoded_string,'cbc',$iv);