我使用以下代码对16字节的单个块进行AES-128加密,但编码值的长度为2个32字节的块。我有什么遗漏吗?
plainEnc = AES.encrypt("thisisapassword!");
import java.security.*; import java.security.spec.InvalidKeySpecException; import javax.crypto.*; import sun.misc.*;
public class AES {
private static final String ALGO = "AES"; private static final byte[] 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 key = generateKey(); Cipher chiper = Cipher.getInstance(ALGO); chiper.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = chiper.doFinal(Data.getBytes()); System.out.println("输出长度:" + encVal.length); //长度= 32 String encryptedValue = new BASE64Encoder().encode(encVal); return encryptedValue; }
public static String decrypt(String encryptedData) throws Exception { Key key = generateKey(); Cipher chiper = Cipher.getInstance(ALGO); chiper.init(Cipher.DECRYPT_MODE, key); byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); byte[] decValue = chiper.doFinal(decordedValue); String decryptedValue = new String(decValue); return decryptedValue; } private static Key generateKey() throws Exception { Key key = new SecretKeySpec(keyValue, ALGO); return key; }
}