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对象。大致逻辑一般如下:

 


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

 

 

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


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

 

 

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


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

 

 

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

 

1.2密钥的存储

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


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

 

 

1.3获取存储的密钥

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


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

 

 

1.4加解密

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

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


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

 

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


  1. SecretKey secretKey = this.geneKey();  

 

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


  1. cipher.init(Cipher.ENCRYPT_MODE, secretKey);  

 

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


  1. cipher.update(content.getBytes());  

 

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


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

 

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


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

 

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


  1. public class AESTest {  
  2.   
  3.     private static final String ALGORITHM = "AES";  
  4.       
  5.     /** 
  6.      * 生成密钥 
  7.      * @return 
  8.      * @throws Exception 
  9.      */  
  10.     private SecretKey geneKey() throws Exception {  
  11.         //获取一个密钥生成器实例  
  12.         KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);  
  13.         SecureRandom random = new SecureRandom();  
  14.         random.setSeed("123456".getBytes());//设置加密用的种子,密钥  
  15.         keyGenerator.init(random);  
  16.         SecretKey secretKey = keyGenerator.generateKey();  
  17.         //把上面的密钥存起来  
  18.         Path keyPath = Paths.get("D:/aes.key");  
  19.         Files.write(keyPath, secretKey.getEncoded());  
  20.         return secretKey;  
  21.     }  
  22.       
  23.     /** 
  24.      * 读取存储的密钥 
  25.      * @param keyPath 
  26.      * @return 
  27.      * @throws Exception 
  28.      */  
  29.     private SecretKey readKey(Path keyPath) throws Exception {  
  30.         //读取存起来的密钥  
  31.         byte[] keyBytes = Files.readAllBytes(keyPath);  
  32.         SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);  
  33.         return keySpec;  
  34.     }  
  35.       
  36.     /** 
  37.      * 加密测试 
  38.      */  
  39.     @Test  
  40.     public void testEncrypt() throws Exception {  
  41.         //1、指定算法、获取Cipher对象  
  42.         Cipher cipher = Cipher.getInstance(ALGORITHM);//算法是AES  
  43.         //2、生成/读取用于加解密的密钥  
  44.         SecretKey secretKey = this.geneKey();  
  45.         //3、用指定的密钥初始化Cipher对象,指定是加密模式,还是解密模式  
  46.         cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
  47.         String content = "Hello AES";//需要加密的内容  
  48.         //4、更新需要加密的内容  
  49.         cipher.update(content.getBytes());  
  50.         //5、进行最终的加解密操作  
  51.         byte[] result = cipher.doFinal();//加密后的字节数组  
  52.         //也可以把4、5步组合到一起,但是如果保留了4步,同时又是如下这样使用的话,加密的内容将是之前update传递的内容和doFinal传递的内容的和。  
  53. //      byte[] result = cipher.doFinal(content.getBytes());  
  54.         String base64Result = Base64.getEncoder().encodeToString(result);//对加密后的字节数组进行Base64编码  
  55.         System.out.println("Result: " + base64Result);  
  56.     }  
  57.       
  58.     /** 
  59.      * 解密测试 
  60.      */  
  61.     @Test  
  62.     public void testDecrpyt() throws Exception {  
  63.         Cipher cipher = Cipher.getInstance(ALGORITHM);  
  64.         SecretKey secretKey = this.geneKey();  
  65.         cipher.init(Cipher.DECRYPT_MODE, secretKey);  
  66.         String content = "pK9Xw4zqTMXYraDadSGJE3x/ftrDxIg2AM/acq0uixA=";//经过Base64加密的待解密的内容  
  67.         byte[] encodedBytes = Base64.getDecoder().decode(content.getBytes());  
  68.         byte[] result = cipher.doFinal(encodedBytes);//对加密后的字节数组进行解密  
  69.         System.out.println("Result: " + new String(result));  
  70.     }  
  71.       
  72. }  

 

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


  1. @Test  
  2. public void test() throws Exception {  
  3.     Cipher cipher = Cipher.getInstance(ALGORITHM);  
  4.     KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);  
  5.     keyGenerator.init(128);  
  6.     SecretKey secretKey = keyGenerator.generateKey();  
  7.     //把上面的密钥存起来  
  8.     Path keyPath = Paths.get("D:/aes.key");  
  9.     Files.write(keyPath, secretKey.getEncoded());  
  10.       
  11.     //读取存起来的密钥  
  12.     SecretKey key = this.readKey(keyPath);  
  13.     cipher.init(Cipher.ENCRYPT_MODE, key);  
  14.     cipher.update("Hello World".getBytes());  
  15.     //密文  
  16.     byte[] encryptBytes = cipher.doFinal();  
  17.     System.out.println(Base64.getEncoder().encodeToString(encryptBytes));  
  18.       
  19.     //用取出来的密钥进行解密  
  20.     cipher.init(Cipher.DECRYPT_MODE, key);  
  21.     //明文  
  22.     byte[] decryptBytes = cipher.doFinal(encryptBytes);  
  23.     System.out.println(new String(decryptBytes));  
  24. }  

 

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

 

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

 

 

你可能感兴趣的:(安全,Java,Java基础)