java aes jdk_JDK自带方法实现AES对称加密

该博客展示了如何使用Java JDK自带的方法实现AES对称加密,包括ECB(电子密码本)和CBC(密码块链接)两种模式。代码中详细解释了加密解密过程,以及初始化向量IV的重要性。示例代码演示了加密和解密字符串,并以Base64编码输出结果。
摘要由CSDN通过智能技术生成

1 packagejdbc.pro.lin;2

3 importjava.security.InvalidAlgorithmParameterException;4 importjava.security.InvalidKeyException;5 importjava.security.NoSuchAlgorithmException;6

7 importjavax.crypto.BadPaddingException;8 importjavax.crypto.Cipher;9 importjavax.crypto.IllegalBlockSizeException;10 importjavax.crypto.KeyGenerator;11 importjavax.crypto.NoSuchPaddingException;12 importjavax.crypto.SecretKey;13 importjavax.crypto.spec.IvParameterSpec;14 importjavax.crypto.spec.SecretKeySpec;15

16 importorg.apache.commons.codec.binary.Base64;17

18 public classMyAES {19 /**

20 * 注意key和加密用到的字符串是不一样的 加密还要指定填充的加密模式和填充模式 AES密钥可以是128或者256,加密模式包括ECB, CBC等21 * ECB模式是分组的模式,CBC是分块加密后,每块与前一块的加密结果异或后再加密 第一块加密的明文是与IV变量进行异或22 */

23 public static final String KEY_ALGORITHM = "AES";24 public static final String ECB_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";25 public static final String CBC_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";26 public static final String PLAIN_TEXT = "MANUTD is the greatest club in the world";27

28 /**

29 * IV(Initialization Value)是一个初始值,对于CBC模式来说,它必须是随机选取并且需要保密的30 * 而且它的长度和密码分组相同(比如:对于AES 128为128位,即长度为16的byte类型数组)31 *32 */

33 public static final byte[] IVPARAMETERS = new byte[] { 1, 2, 3, 4, 5, 6, 7,34 8, 9, 10, 11, 12, 13, 14, 15, 16};35

36 public static voidmain(String[] arg) {37 byte[] secretBytes =generateAESSecretKey();38 SecretKey key =restoreSecretKey(secretBytes);39 byte[] encodedText =AesEcbEncode(PLAIN_TEXT.getBytes(), key);40

41 System.out.println("AES ECB encoded with Base64: " +Base64.encodeBase64String(encodedText));42 System.out.println("AES ECB decoded: "

43 +AesEcbDecode(encodedText, key));44

45

46

47 encodedText =AesCbcEncode(PLAIN_TEXT.getBytes(), key, IVPARAMETERS);48

49

50 System.out.println("AES CBC encoded with Base64: " +Base64.encodeBase64String(encodedText));51 System.out.println("AES CBC decoded: "

52 +AesCbcDecode(encodedText, key,53 IVPARAMETERS));54 }55

56 /**

57 * 使用ECB模式进行加密。 加密过程三步走: 1. 传入算法,实例化一个加解密器 2. 传入加密模式和密钥,初始化一个加密器 3.58 * 调用doFinal方法加密59 *60 *@paramplainText61 *@return

62 */

63 public static byte[] AesEcbEncode(byte[] plainText, SecretKey key) {64

65 try{66

67 Cipher cipher =Cipher.getInstance(ECB_CIPHER_ALGORITHM);68 cipher.init(Cipher.ENCRYPT_MODE, key);69 returncipher.doFinal(plainText);70 } catch (NoSuchAlgorithmException |NoSuchPaddingException71 | InvalidKeyException |IllegalBlockSizeException72 |BadPaddingException e) {73 //TODO Auto-generated catch block

74 e.printStackTrace();75 }76 return null;77 }78

79 /**

80 * 使用ECB解密,三步走,不说了81 *82 *@paramdecodedText83 *@paramkey84 *@return

85 */

86 public static String AesEcbDecode(byte[] decodedText, SecretKey key) {87 try{88 Cipher cipher =Cipher.getInstance(ECB_CIPHER_ALGORITHM);89 cipher.init(Cipher.DECRYPT_MODE, key);90 return newString(cipher.doFinal(decodedText));91 } catch (NoSuchAlgorithmException |NoSuchPaddingException92 | InvalidKeyException |IllegalBlockSizeException93 |BadPaddingException e) {94 //TODO Auto-generated catch block

95 e.printStackTrace();96 }97 return null;98

99 }100

101 /**

102 * CBC加密,三步走,只是在初始化时加了一个初始变量103 *104 *@paramplainText105 *@paramkey106 *@paramIVParameter107 *@return

108 */

109 public static byte[] AesCbcEncode(byte[] plainText, SecretKey key,110 byte[] IVParameter) {111 try{112 IvParameterSpec ivParameterSpec = newIvParameterSpec(IVParameter);113

114 Cipher cipher =Cipher.getInstance(CBC_CIPHER_ALGORITHM);115 cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);116 returncipher.doFinal(plainText);117

118 } catch (NoSuchAlgorithmException |NoSuchPaddingException119 | InvalidKeyException |InvalidAlgorithmParameterException120 | IllegalBlockSizeException |BadPaddingException e) {121 //TODO Auto-generated catch block

122 e.printStackTrace();123 }124 return null;125 }126

127 /**

128 * CBC 解密129 *130 *@paramdecodedText131 *@paramkey132 *@paramIVParameter133 *@return

134 */

135 public static String AesCbcDecode(byte[] decodedText, SecretKey key,136 byte[] IVParameter) {137 IvParameterSpec ivParameterSpec = newIvParameterSpec(IVParameter);138

139 try{140 Cipher cipher =Cipher.getInstance(CBC_CIPHER_ALGORITHM);141 cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);142 return newString(cipher.doFinal(decodedText));143 } catch (NoSuchAlgorithmException |NoSuchPaddingException144 | InvalidKeyException |InvalidAlgorithmParameterException145 | IllegalBlockSizeException |BadPaddingException e) {146 //TODO Auto-generated catch block

147 e.printStackTrace();148 }149

150 return null;151

152 }153

154 /**

155 * 1.创建一个KeyGenerator 2.调用KeyGenerator.generateKey方法156 * 由于某些原因,这里只能是128,如果设置为256会报异常,原因在下面文字说明157 *158 *@return

159 */

160 public static byte[] generateAESSecretKey() {161 KeyGenerator keyGenerator;162 try{163 keyGenerator =KeyGenerator.getInstance(KEY_ALGORITHM);164 //keyGenerator.init(256);

165 returnkeyGenerator.generateKey().getEncoded();166 } catch(NoSuchAlgorithmException e) {167 //TODO Auto-generated catch block

168 e.printStackTrace();169 }170 return null;171 }172

173 /**

174 * 还原密钥175 *176 *@paramsecretBytes177 *@return

178 */

179 public static SecretKey restoreSecretKey(byte[] secretBytes) {180 SecretKey secretKey = newSecretKeySpec(secretBytes, KEY_ALGORITHM);181 returnsecretKey;182 }183 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值