ユーザーの名前を暗号化された形式に変換してからphpで復号化する必要があるアプリケーションがあります。暗号化コードはうまく機能します。しかし、php側では、復号化された文字列には常に読み取り不可能な形式の2〜3個の追加文字が含まれています。これらの追加文字がどこから取得されているのかわかりません。パディングと関係がありますか?私はアンドロイドコードとPHPコードを投稿しています。
Androidのサイドコード:
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);