aes加密解密,含128、192、256位,cbc、cfb、ecb、ofb、
pcbc模式
AES加解密总共有以下这些
算法/模式/填充字节加密后数据长度不满16字节加密后长度
AES/CBC/NoPadding                  16                          不⽀持
AES/CBC/PKCS5Padding                32                          16
AES/CBC/ISO10126Padding            32                          16
AES/CFB/NoPadding                  16                          原始数据长度
AES/CFB/PKCS5Padding                32                          16
AES/CFB/ISO10126Padding            32                          16
AES/ECB/NoPadding                  16                          不⽀持
AES/ECB/PKCS5Padding                32                          16
AES/ECB/ISO10126Padding            32                          16
AES/OFB/NoPadding                  16                          原始数据长度
AES/OFB/PKCS5Padding                32                          16
AES/OFB/ISO10126Padding            32                          16
AES/PCBC/NoPadding                  16                          不⽀持
AES/PCBC/PKCS5Padding              32                          16
AES/PCBC/ISO10126Padding            32                          16
⼀、不带模式和填充来获取AES算法的时候,其默认使⽤AES/ECB/PKCS5Padding(输⼊可以不是16字节,也不需要填充向量)Cipher cipher = Instance("AES");
下⾯是 AES/ECB/PKCS5Padding 128位、192位、256加解密
String content = "在线助⼿";
// ⽣成密钥需要的密码值
String key = "www.it399";
/**
* AES加密⽅式⼀:AES不指定模式和填充,默认为 ECB/PKCS5Padding
*
* 不能使⽤填充向量
* java.security.InvalidAlgorithmParameterException: ECB mode cannot use IV
*/
System.out.println("【0】AES不指定模式和填充,默认为 ECB/PKCS5Padding,输⼊可以不是16字节,也不需要填充向量\n");
//128
byte[] encrypt = encryptOrdecrypt(Bytes(CHARSET),key,null,AESType.AES_128,EncodeType.AES_DEFAULT);
encryptOrdecrypt(false,encrypt,key,null,AESType.AES_128,EncodeType.AES_DEFAULT);
//192
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,null,AESType.AES_192,EncodeType.AES_DEFAULT);
encryptOrdecrypt(false,encrypt,key,null,AESType.AES_192,EncodeType.AES_DEFAULT);
//256
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,null,AESType.AES_256,EncodeType.AES_DEFAULT);
encryptOrdecrypt(false,encrypt,key,null,AESType.AES_256,EncodeType.AES_DEFAULT);
结果如下:
【0】AES不指定模式和填充,默认为 ECB/PKCS5Padding,输⼊可以不是16字节,也不需要填充向量
秘钥长度,128,加密⽅式: AES,加密结果:09942AC3BB18703E2BAF29EC18E69BCC
秘钥长度,128,解密⽅式: AES,解密结果:在线助⼿
秘钥长度,192,加密⽅式: AES,加密结果:B9F574EF92836DFD2CC0EE03E7A0E717
秘钥长度,192,解密⽅式: AES,解密结果:在线助⼿
秘钥长度,256,加密⽅式: AES,加密结果:118CD83850A220EFD791FF6B1FF180F8
秘钥长度,256,解密⽅式: AES,解密结果:在线助⼿
⼆、AES/CBC 加密⽅式
2.1 AES/CBC/NoPadding
输⼊必须是16字节,不然报错 pto.IllegalBlockSizeException: Input length not multiple of 16 bytes
CBC模式必须提供初始向量IvParameterSpec,不然报错 java.security.InvalidKeyException: Parameters missing
content:在线助⼿
key: www.it399111
at pto.provider.CipherCore.finalNoPadding(CipherCore.java:1041)
at pto.provider.CipherCore.doFinal(CipherCore.java:1009)
at pto.provider.CipherCore.doFinal(CipherCore.java:847)
at pto.ineDoFinal(AESCipher.java:446)
pto.Cipher.doFinal(Cipher.java:2165)
at com.csy.de.pt(AESUtil.java:80)
at com.csy.de.aes.AESUtil.main(AESUtil.java:200)
java.security.InvalidKeyException: Parameters missing
at pto.provider.CipherCore.init(CipherCore.java:470)
at pto.ineInit(AESCipher.java:313)
pto.Cipher.implInit(Cipher.java:802)
pto.Cipher.chooseProvider(Cipher.java:864)
pto.Cipher.init(Cipher.java:1249)
pto.Cipher.init(Cipher.java:1186)
at com.csy.de.aes.AESUtil.decrypt(AESUtil.java:117)
at com.csy.de.aes.AESUtil.main(AESUtil.java:202)
encode: null
decode: null
初始化加密模式的时改成
Cipher cipher = Instance(“AES/CBC/NoPadding”);
java.security.InvalidKeyException: Parameters missing解决办法:
if (modeAndPadding.equals(EncodeType.AES_CBC_NoPadding)) {
//指定⼀个初始化向量 (Initialization vector,IV), IV 必须是16位
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(getIV()));
} else {
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
}
下⾯是 AES/CBC/NoPadding,AES/CBC/PKCS5Padding,AES/CBC/ISO10126Padding 128位,192位,256加密解密(⽂末附完整代码,)
/**
* 1.1 AES/CBC
* AES/CBC/NoPadding
* AES/CBC/PKCS5Padding
* AES/CBC/ISO10126Padding
*/
System.out.println("【1.1】AES_CBC_NoPadding模式");
content = "在线助⼿在线助⼿在线助⼿在线助⼿";
key = "www.it399";
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_128,EncodeType.AES_CBC_NoPadding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_128,EncodeType.AES_CBC_NoPadding);
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_192,EncodeType.AES_CBC_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_192,EncodeType.AES_CBC_PKCS5Padding);
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_256,EncodeType.AES_CBC_ISO10126Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_256,EncodeType.AES_CBC_ISO10126Padding);
秘钥长度,128,加密⽅式: AES/CBC/NoPadding,加密结果:81C094D454913311113FA1E5B5A3B6778DC81026A19A52C51FAF54B82E48448A813E1FDE3F97秘钥长度,128,解密⽅式: AES/CBC/NoPadding,解密结果:在线助⼿在线助⼿在线助⼿在线助⼿
秘钥长度,192,加密⽅式: AES/CBC/PKCS5Padding,加密结果:5BCD216A00741AB48668742393A8F995C63B4A875F6BD460BFB04DBCF5BF35E429C0C177秘钥长度,192,解密⽅式: AES/CBC/PKCS5Padding,解密结果:在线助⼿在线助⼿在线助⼿在线助⼿
秘钥长度,256,加密⽅式: AES/CBC/ISO10126Padding,加密结果:1A45746F4E488AB13752033
C21F9EE9C64417ECAA3A8FA08326D76DD1A052445218AA6D 秘钥长度,256,解密⽅式: AES/CBC/ISO10126Padding,解密结果:在线助⼿在线助⼿在线助⼿在线助⼿
三、AES/CFB 加密⽅式
需要填充变量,不然报如下错误
Exception in thread "main" java.security.InvalidKeyException:
Parameters missing
下⾯是AES/CFB/NoPadding,AES/CFB/PKCS5Padding,AES/CFB/ISO10126Padding 128位,192位,256位加加密解密
/**
* 1.2 AES/CFB
* AES/CBC/NoPadding
* AES/CBC/PKCS5Padding
* AES/CBC/ISO10126Padding
*/
System.out.println("【1.2】AES_CFB_NoPadding模式\n");
content = "在线助⼿";
// ⽣成密钥需要的密码值
key = "www.it399";
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_128,EncodeType.AES_CFB_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_128,EncodeType.AES_CFB_PKCS5Padding);
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_192,EncodeType.AES_CFB_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_192,EncodeType.AES_CFB_PKCS5Padding);
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_256,EncodeType.AES_CFB_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_256,EncodeType.AES_CFB_PKCS5Padding);
四、AES/ECB 加密⽅式
AES/ECB不要填充变量,不然会报如下错误
Exception in thread "main" java.security.InvalidAlgorithmParameterException: ECB mode
cannot use IV
下⾯是AES/ECB/NoPadding,AES/ECB/PKCS5Padding,AES/ECB/ISO10126Padding 128位,192位,256位加加密解密
/**
* 1.3 AES/ECB
* AES/ECB/NoPadding
* AES/ECB/PKCS5Padding
* AES/ECB/ISO10126Padding
*/
System.out.println("【1.3】AES_ECB模式");
content = "在线助⼿";
// ⽣成密钥需要的密码值
key = "www.it399";
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,null,AESType.AES_128,EncodeType.AES_ECB_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,null,AESType.AES_128,EncodeType.AES_ECB_PKCS5Padding);
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,null,AESType.AES_192,EncodeType.AES_ECB_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,null,AESType.AES_192,EncodeType.AES_ECB_PKCS5Padding);
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,null,AESType.AES_256,EncodeType.AES_ECB_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,null,AESType.AES_256,EncodeType.AES_ECB_PKCS5Padding);
五、AES/OFB 加密⽅式
下⾯是AES/OFB/NoPadding,AES/OFB/PKCS5Padding,AES/OFB/ISO10126Padding 128位,192位,256位加加密解密 * 需要填充向量
/**
* 1.4 AES/OFB
* AES/OFB/NoPadding
* AES/OFB/PKCS5Padding
* AES/OFB/ISO10126Padding
*/
System.out.println("【1.4】AES_OFB模式");
content = "在线助⼿";
// ⽣成密钥需要的密码值
key = "www.it399";
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_128,EncodeType.AES_OFB_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_128,EncodeType.AES_OFB_PKCS5Padding);
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_192,EncodeType.AES_OFB_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_192,EncodeType.AES_OFB_PKCS5Padding);
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_256,Encod
eType.AES_OFB_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_256,EncodeType.AES_OFB_PKCS5Padding);
六、AES/PCBC 加密⽅式
下⾯是AES/PCBC/NoPadding,AES/PCBC/PKCS5Padding,AES/PCBC/ISO10126Padding 128位,192位,256位加加密解密
* 需要填充向量
/**
* 1.5 AES/PCBC
* AES/PCBC/NoPadding
* AES/PCBC/PKCS5Padding
* AES/PCBC/ISO10126Padding
*/
System.out.println("【1.5】AES_PCBC模式");
content = "在线助⼿";
// ⽣成密钥需要的密码值
key = "www.it399";
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_128,EncodeType.AES_PCBC_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_128,EncodeType.AES_PCBC_PKCS5Padding);
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_192,EncodeType.AES_PCBC_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_192,EncodeType.AES_PCBC_PKCS5Padding);
encrypt = encryptOrdecrypt(Bytes(CHARSET),key,getIV(),AESType.AES_256,EncodeType.AES_PCBC_PKCS5Padding);
encryptOrdecrypt(false,encrypt,key,getIV(),AESType.AES_256,EncodeType.AES_PCBC_PKCS5Padding);
七、完整代码
AESUtil .java
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
pto.BadPaddingException;
java加密方式有哪些pto.Cipher;
pto.IllegalBlockSizeException;
pto.KeyGenerator;
pto.NoSuchPaddingException;
pto.SecretKey;
pto.spec.IvParameterSpec;
pto.spec.SecretKeySpec;
/**
* 在线助⼿|在线⼯具|在线⽣成|在线制作
* www.it399/
* 在线助⼿博客
* www.it399/blog/index
*/
public class AESUtil {
public static final String CHARSET = "UTF-8";
private static byte[] encryptOrDecrypt(int mode,byte[] byteContent, String key,byte[] iv, AESType type, String modeAndPadding) throws InvalidKeyException,            KeyGenerator kgen = Instance("AES");
//此处解决,linux报错
SecureRandom random = Instance("SHA1PRNG");
random.Bytes());
kgen.init(type.value, random);
SecretKey secretKey = ateKey();
byte[] enCodeFormat = Encoded();
SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Instance(modeAndPadding);// 创建密码器
if ( null !=iv ) {
//指定⼀个初始化向量 (Initialization vector,IV), IV 必须是16位
cipher.init(mode, keySpec, new IvParameterSpec(iv));
} else {
cipher.init(mode, keySpec);
}
byte[] result = cipher.doFinal(byteContent);
return result;
}
public static void main(String[] args) throws Exception {
/
/        System.out.println("【1】AES不指定模式和填充,默认为 ECB/PKCS5Padding,输⼊可以不是16字节,也不需要填充向量\n");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。