`
234390216
  • 浏览: 10193388 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
A5ee55b9-a463-3d09-9c78-0c0cf33198cd
Oracle基础
浏览量:460806
Ad26f909-6440-35a9-b4e9-9aea825bd38e
springMVC介绍
浏览量:1771803
Ce363057-ae4d-3ee1-bb46-e7b51a722a4b
Mybatis简介
浏览量:1395429
Bdeb91ad-cf8a-3fe9-942a-3710073b4000
Spring整合JMS
浏览量:393886
5cbbde67-7cd5-313c-95c2-4185389601e7
Ehcache简介
浏览量:678220
Cc1c0708-ccc2-3d20-ba47-d40e04440682
Cas简介
浏览量:529293
51592fc3-854c-34f4-9eff-cb82d993ab3a
Spring Securi...
浏览量:1178733
23e1c30e-ef8c-3702-aa3c-e83277ffca91
Spring基础知识
浏览量:461882
4af1c81c-eb9d-365f-b759-07685a32156e
Spring Aop介绍
浏览量:150144
2f926891-9e7a-3ce2-a074-3acb2aaf2584
JAXB简介
浏览量:66849
社区版块
存档分类
最新评论

Java使用AES加解密

阅读更多

Java使用AES加解密

目录

1.1生成密钥

1.2密钥的存储

1.3获取存储的密钥

1.4加解密

1.5使用存储的密钥进行加解密示例

 

AES是一种对称的加密算法,可基于相同的密钥进行加密和解密。Java采用AES算法进行加解密的逻辑大致如下:

1、生成/获取密钥

2、加/解密

 

1.1生成密钥

密钥的生成是通过KeyGenerator来生成的。通过获取一个KeyGenerator实例,然后调用其generateKey()方法即可生成一个SecretKey对象。大致逻辑一般如下:

 

	private SecretKey geneKey() throws Exception {
		//获取一个密钥生成器实例
		KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
		SecureRandom random = new SecureRandom();
		random.setSeed("123456".getBytes());//设置加密用的种子,密钥
		keyGenerator.init(random);
		SecretKey secretKey = keyGenerator.generateKey();
		return secretKey;
	}

 

 

上述生成密钥的过程中指定了固定的种子,每次生成出来的密钥都是一样的。还有一种形式,我们可以通过不指定SecureRandom对象的种子,即不调用其setSeed方法,这样每次生成出来的密钥都可能是不一样的。

	private SecretKey geneKey() throws Exception {
		//获取一个密钥生成器实例
		KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
		SecureRandom random = new SecureRandom();
		keyGenerator.init(random);
		SecretKey secretKey = keyGenerator.generateKey();
		return secretKey;
	}

 

 

通过KeyGenerator的init(keySize)方法进行初始化,而不是通过传递SecureRandom对象进行初始化也可以达到上面的效果,每次生成的密钥都可能是不一样的。但是对应的keySize的指定一定要正确,AES算法的keySize是128。

	private SecretKey geneKey() throws Exception {
		//获取一个密钥生成器实例
		KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
		keyGenerator.init(128);
		SecretKey secretKey = keyGenerator.generateKey();
		return secretKey;
	}

 

 

但是这种每次生成出来的密钥都是不同的情况下,我们需要把加密用的密钥存储起来,以供解密的时候使用,不然就没法进行解密了。

 

1.2密钥的存储

密钥SecretKey里面最核心的内容就是其中的密钥对应的字节数组,可以通过SecretKey的getEncoded()方法获取。然后把它存储起来即可。最简单的方式就是直接写入一个文件中。

		//把上面的密钥存起来
		Path keyPath = Paths.get("D:/aes.key");
		Files.write(keyPath, secretKey.getEncoded());

 

 

1.3获取存储的密钥

获取存储的密钥的核心是把密钥的字节数组转换为对应的SecretKey。这可以通过SecretKeySpec来获取,其实现了SecretKey接口,然后构造参数里面将接收密钥的字节数组。

	private SecretKey readKey(Path keyPath) throws Exception {
		//读取存起来的密钥
		byte[] keyBytes = Files.readAllBytes(keyPath);
		SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);
		return keySpec;
	}

 

 

1.4加解密

Java采用AES算法进行加解密的过程是类似的,具体如下:

1、指定算法,获取一个Cipher实例对象

Cipher cipher = Cipher.getInstance(ALGORITHM);//算法是AES

 

2、生成/读取用于加解密的密钥

SecretKey secretKey = this.geneKey();

 

3、用指定的密钥初始化Cipher对象,同时指定加解密模式,是加密模式还是解密模式。

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

 

4、通过update指定需要加密的内容,不可多次调用。

cipher.update(content.getBytes());

 

5、通过Cipher的dofinal()进行最终的加解密操作。

byte[] result = cipher.doFinal();//加密后的字节数组

 

通过以上几步就完成了使用AES算法进行加解密的操作了。其实第4、5步是可以合在一起的,即在进行doFinal的时候传递需要进行加解密的内容。但是如果update指定了加密的内容,而doFinal的时候也指定了加密的内容,那最终加密出来的结果将是两次指定的加密内容的和对应的加密结果。

byte[] result = cipher.doFinal(content.getBytes());

 

以下是一次加解密操作的完整示例。

public class AESTest {

	private static final String ALGORITHM = "AES";
	
	/**
	 * 生成密钥
	 * @return
	 * @throws Exception
	 */
	private SecretKey geneKey() throws Exception {
		//获取一个密钥生成器实例
		KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
		SecureRandom random = new SecureRandom();
		random.setSeed("123456".getBytes());//设置加密用的种子,密钥
		keyGenerator.init(random);
		SecretKey secretKey = keyGenerator.generateKey();
		//把上面的密钥存起来
		Path keyPath = Paths.get("D:/aes.key");
		Files.write(keyPath, secretKey.getEncoded());
		return secretKey;
	}
	
	/**
	 * 读取存储的密钥
	 * @param keyPath
	 * @return
	 * @throws Exception
	 */
	private SecretKey readKey(Path keyPath) throws Exception {
		//读取存起来的密钥
		byte[] keyBytes = Files.readAllBytes(keyPath);
		SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);
		return keySpec;
	}
	
	/**
	 * 加密测试
	 */
	@Test
	public void testEncrypt() throws Exception {
		//1、指定算法、获取Cipher对象
		Cipher cipher = Cipher.getInstance(ALGORITHM);//算法是AES
		//2、生成/读取用于加解密的密钥
		SecretKey secretKey = this.geneKey();
		//3、用指定的密钥初始化Cipher对象,指定是加密模式,还是解密模式
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		String content = "Hello AES";//需要加密的内容
		//4、更新需要加密的内容
		cipher.update(content.getBytes());
		//5、进行最终的加解密操作
		byte[] result = cipher.doFinal();//加密后的字节数组
		//也可以把4、5步组合到一起,但是如果保留了4步,同时又是如下这样使用的话,加密的内容将是之前update传递的内容和doFinal传递的内容的和。
//		byte[] result = cipher.doFinal(content.getBytes());
		String base64Result = Base64.getEncoder().encodeToString(result);//对加密后的字节数组进行Base64编码
		System.out.println("Result: " + base64Result);
	}
	
	/**
	 * 解密测试
	 */
	@Test
	public void testDecrpyt() throws Exception {
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		SecretKey secretKey = this.geneKey();
		cipher.init(Cipher.DECRYPT_MODE, secretKey);
		String content = "pK9Xw4zqTMXYraDadSGJE3x/ftrDxIg2AM/acq0uixA=";//经过Base64加密的待解密的内容
		byte[] encodedBytes = Base64.getDecoder().decode(content.getBytes());
		byte[] result = cipher.doFinal(encodedBytes);//对加密后的字节数组进行解密
		System.out.println("Result: " + new String(result));
	}
	
}

 

1.5使用存储的密钥进行加解密示例

	@Test
	public void test() throws Exception {
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
		keyGenerator.init(128);
		SecretKey secretKey = keyGenerator.generateKey();
		//把上面的密钥存起来
		Path keyPath = Paths.get("D:/aes.key");
		Files.write(keyPath, secretKey.getEncoded());
		
		//读取存起来的密钥
		SecretKey key = this.readKey(keyPath);
		cipher.init(Cipher.ENCRYPT_MODE, key);
		cipher.update("Hello World".getBytes());
		//密文
		byte[] encryptBytes = cipher.doFinal();
		System.out.println(Base64.getEncoder().encodeToString(encryptBytes));
		
		//用取出来的密钥进行解密
		cipher.init(Cipher.DECRYPT_MODE, key);
		//明文
		byte[] decryptBytes = cipher.doFinal(encryptBytes);
		System.out.println(new String(decryptBytes));
	}

 

在上面的示例中,我们先生成了一个密钥,然后把它保存到本地文件中,然后再把它读出来,分别用以加密和解密。而且我们加密和解密都是用的同一个Cipher对象,但是在使用前需要重新通过init方法初始化加解密模式。

 

(Elim写于2017年4月20日星期四)

 

 

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics