ASE加密和DES加密的区别在于AES密匙传入必须是16位字符长度而DES密匙必须是8位字符长度,同样最后8个byte位是效验码.
如果需要实现其cbc加密模式的话,需要添加另外几步.使用注释的地方就是添加cbc加密后添加和修改的方法
//加密算法:"algorithm/mode/padding" "算法/工作模式/填充模式"
//如果要实现这个cbc模式,能加强安全性,并且IvParameterSpec()中的参数可以传任意字符串
public static final String AES= "AES";
//public static final String AES2 = "AES/CBC/PKCS5Padding";
public static final String key ="1234567812345678";public static void main(String[] args) throws Exception{
String input = "这是加密";
//String key = ;
//加密
byte[] mEncrypt = encrypt(input,key);
//解密
byte[] mDerypt = derypt(mEncrypt,key);
//打印解密
System.out.println(new String(mDerypt));
}
private static byte[] derypt(byte[] mEncrypt, String key) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), AES);
Cipher cipher = Cipher.getInstance(AES);
//IvParameterSpec iv = new IvParameterSpec(key.getBytes());
//Cipher cipher = Cipher.getInstance(AES2);
cipher.init(Cipher.DECRYPT_MODE, keySpec)
//cipher.init(Cipher.DECRYPT_MODE, keySpec,iv);;
return bs;
}
private static byte[] encrypt(String input, String key) throws Exception{
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), AES);
//获取加密对象
Cipher cipher = Cipher.getInstance(AES);
//Cipher cipher = Cipher.getInstance(AES2)
//IvParameterSpec iv = new IvParameterSpec(key.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
;//cipher.init(Cipher.ENCRYPT_MODE, keySpec,iv);
//进行加密byte[] bs = cipher.doFinal(input.getBytes());
System.out.println("加密后:"+new String(bs));
return bs;
}