AES-128 暗号化に次のコードを使用して 16 バイトの単一ブロックをエンコードしていますが、エンコードされた値の長さは 32 バイトの 2 ブロックになります。何か不足していますか?
plainEnc = AES.encrypt("これはパスワードです!");
java.security.* をインポートします。
java.security.spec.InvalidKeySpecException をインポートします。
import javax.crypto.*;
sun.misc.* をインポートします。
パブリック クラス AES {
private static final String ALGO = "AES";
プライベート静的最終バイト[] keyValue =
new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
public static String encrypt(String Data) throws Exception {
System.out.println("文字列の長さ: " + (Data.getBytes()).length); //長さ = 16
キー key = generateKey();
Cipher chiper = Cipher.getInstance(ALGO);
chiper.init(Cipher.ENCRYPT_MODE, キー);
byte[] encVal = chiper.doFinal(Data.getBytes());
System.out.println("出力長: " + encVal.length); // 長さ = 32
文字列 encryptedValue = new BASE64Encoder().encode(encVal);
暗号化された値を返します。
}
public static String decrypt(String encryptedData) throws Exception {
キー key = generateKey();
Cipher chiper = Cipher.getInstance(ALGO);
chiper.init(Cipher.DECRYPT_MODE, キー);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = chiper.doFinal(decordedValue);
文字列復号値 = 新しい文字列 (decValue);
復号化された値を返します。
}
private static Key generateKey() throws Exception {
キー key = new SecretKeySpec(keyValue, ALGO);
キーを返します。
}
}