Java使⽤AES加密和解密的实例详解
Java使⽤AES加密和解密的实例详解
前⾔:
AES的基本要求是,采⽤对称分组密码体制,密钥长度的最少⽀持为128、192、256,分组长度128位,算法应易于各种硬件和软件实现。1998年NIST开始AES第⼀轮分析、测试和征集,共产⽣了15个候选算法。1999年3⽉完成了第⼆轮AES2的分析、测试。2000年10⽉2⽇美国政府正式宣布选中⽐利时密码学家Joan Daemen 和 Vincent Rijmen 提出的⼀种密码算法RIJNDAEL 作为 AES.   在应⽤⽅⾯,尽管DES在安全上是脆弱的,但由于快速DES芯⽚的⼤量⽣产,使得DES仍能暂时继续使⽤,为提⾼安全强度,通常使⽤独⽴密钥的三级DES。但是DES迟早要被AES代替。流密码体制较之分组密码在理论上成熟且安全,但未被列⼊下⼀代加密标准。 
AES加密数据块和密钥长度可以是128⽐特、192⽐特、256⽐特中的任意⼀个。
AES加密有很多轮的重复和变换。⼤致步骤如下:
1、密钥扩展(KeyExpansion),
2、初始轮(Initial Round),
3、重复轮(Rounds),每⼀轮⼜包括:SubBytes、ShiftRows、MixColumns、AddRoundKey,
4、最终轮(Final Round),最终轮没有MixColumns。
AES是⼀种对称的加密算法,可基于相同的密钥进⾏加密和解密。Java采⽤AES算法进⾏加解密的逻辑⼤致如下:
1、⽣成/获取密钥
2、加/解密
1.1⽣成密钥
密钥的⽣成是通过KeyGenerator来⽣成的。通过获取⼀个KeyGenerator实例,然后调⽤其generateKey()⽅法即可⽣成⼀个SecretKey对象。⼤致逻辑⼀般如下:
private SecretKey geneKey() throws Exception {
//获取⼀个密钥⽣成器实例
KeyGenerator keyGenerator = Instance(ALGORITHM);
SecureRandom random = new SecureRandom();
random.setSeed("123456".getBytes());//设置加密⽤的种⼦,密钥
keyGenerator.init(random);
SecretKey secretKey = ateKey();
return secretKey;
}
上述⽣成密钥的过程中指定了固定的种⼦,每次⽣成出来的密钥都是⼀样的。还有⼀种形式,我们可以通过不指定SecureRandom对象的种⼦,即不调⽤其setSeed⽅法,这样每次⽣成出来的密钥都可能是不⼀样的。
private SecretKey geneKey() throws Exception {
//获取⼀个密钥⽣成器实例
KeyGenerator keyGenerator = Instance(ALGORITHM);
SecureRandom random = new SecureRandom();
keyGenerator.init(random);
SecretKey secretKey = ateKey();
return secretKey;
}
通过KeyGenerator的init(keySize)⽅法进⾏初始化,⽽不是通过传递SecureRandom对象进⾏初始化也可以达到上⾯的效果,每次⽣成的密钥都可能是不⼀样的。但是对应的keySize的指定⼀定要正确,AES算法的keySize是128。
private SecretKey geneKey() throws Exception {
//获取⼀个密钥⽣成器实例
KeyGenerator keyGenerator = Instance(ALGORITHM);
keyGenerator.init(128);
SecretKey secretKey = ateKey();
return secretKey;
}
但是这种每次⽣成出来的密钥都是不同的情况下,我们需要把加密⽤的密钥存储起来,以供解密的时候使⽤,不然就没法进⾏解密了。
1.2密钥的存储
密钥SecretKey⾥⾯最核⼼的内容就是其中的密钥对应的字节数组,可以通过SecretKey的getEncoded()⽅法获取。然后把它存储起来即可。最简单的⽅式就是直接写⼊⼀个⽂件中。
//把上⾯的密钥存起来
Path keyPath = ("D:/aes.key");
Files.write(keyPath, Encoded());
1.3获取存储的密钥
获取存储的密钥的核⼼是把密钥的字节数组转换为对应的SecretKey。这可以通过SecretKeySpec来获取,其实现了SecretKey接⼝,然后构造参数⾥⾯将接收密钥的字节数组。
private SecretKey readKey(Path keyPath) throws Exception {
//读取存起来的密钥
byte[] keyBytes = adAllBytes(keyPath);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);
return keySpec;
}
1.4加解密
Java采⽤AES算法进⾏加解密的过程是类似的,具体如下:
1、指定算法,获取⼀个Cipher实例对象
Cipher cipher = Instance(ALGORITHM);//算法是AES
2、⽣成/读取⽤于加解密的密钥
SecretKey secretKey = Key();
3、⽤指定的密钥初始化Cipher对象,同时指定加解密模式,是加密模式还是解密模式。
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
4、通过update指定需要加密的内容,不可多次调⽤。
cipher.Bytes());
5、通过Cipher的dofinal()进⾏最终的加解密操作。
byte[] result = cipher.doFinal();//加密后的字节数组
通过以上⼏步就完成了使⽤AES算法进⾏加解密的操作了。其实第4、5步是可以合在⼀起的,即在进⾏doFinal的时候传递需要进⾏加解密的内容。但是如果update指定了加密的内容,⽽doFinal的时候也指定了加密的内容,那最终加密出来的结果将是两次指定的加密内容的和对应的加密结果。
byte[] result = cipher.Bytes());
以下是⼀次加解密操作的完整⽰例。
public class AESTest {
private static final String ALGORITHM = "AES";
/**
* ⽣成密钥
* @return
* @throws Exception
*/
private SecretKey geneKey() throws Exception {
//获取⼀个密钥⽣成器实例
KeyGenerator keyGenerator = Instance(ALGORITHM);
SecureRandom random = new SecureRandom();
random.setSeed("123456".getBytes());//设置加密⽤的种⼦,密钥
keyGenerator.init(random);
SecretKey secretKey = ateKey();
//把上⾯的密钥存起来
Path keyPath = ("D:/aes.key");
Files.write(keyPath, Encoded());
return secretKey;
}
/**
* 读取存储的密钥springboot其实就是spring
* @param keyPath
* @return
* @throws Exception
*/
private SecretKey readKey(Path keyPath) throws Exception {
//读取存起来的密钥
byte[] keyBytes = adAllBytes(keyPath);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);
return keySpec;
}
/**
* 加密测试
*/
@Test
public void testEncrypt() throws Exception {
//1、指定算法、获取Cipher对象
Cipher cipher = Instance(ALGORITHM);//算法是AES
//2、⽣成/读取⽤于加解密的密钥
SecretKey secretKey = Key();
//3、⽤指定的密钥初始化Cipher对象,指定是加密模式,还是解密模式
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String content = "Hello AES";//需要加密的内容
/
/4、更新需要加密的内容
cipher.Bytes());
//5、进⾏最终的加解密操作
byte[] result = cipher.doFinal();//加密后的字节数组
//也可以把4、5步组合到⼀起,但是如果保留了4步,同时⼜是如下这样使⽤的话,加密的内容将是之前update传递的内容和doFinal传递的内容的和。//  byte[] result = cipher.Bytes());
String base64Result = Encoder().encodeToString(result);//对加密后的字节数组进⾏Base64编码
System.out.println("Result: " + base64Result);
}
/**
* 解密测试
*/
@Test
public void testDecrpyt() throws Exception {
Cipher cipher = Instance(ALGORITHM);
SecretKey secretKey = Key();
cipher.init(Cipher.DECRYPT_MODE, secretKey);
String content = "pK9Xw4zqTMXYraDadSGJE3x/ftrDxIg2AM/acq0uixA=";//经过Base64加密的待解密的内容
byte[] encodedBytes = Decoder().Bytes());
byte[] result = cipher.doFinal(encodedBytes);//对加密后的字节数组进⾏解密
System.out.println("Result: " + new String(result));
}
}
1.5使⽤存储的密钥进⾏加解密⽰例
@Test
public void test() throws Exception {
Cipher cipher = Instance(ALGORITHM);
KeyGenerator keyGenerator = Instance(ALGORITHM);
keyGenerator.init(128);
SecretKey secretKey = ateKey();
//把上⾯的密钥存起来
Path keyPath = ("D:/aes.key");
Files.write(keyPath, Encoded());
/
/读取存起来的密钥
SecretKey key = adKey(keyPath);
cipher.init(Cipher.ENCRYPT_MODE, key);
cipher.update("Hello World".getBytes());
//密⽂
byte[] encryptBytes = cipher.doFinal();
System.out.Encoder().encodeToString(encryptBytes));
//⽤取出来的密钥进⾏解密
cipher.init(Cipher.DECRYPT_MODE, key);
//明⽂
byte[] decryptBytes = cipher.doFinal(encryptBytes);
System.out.println(new String(decryptBytes));
}
在上⾯的⽰例中,我们先⽣成了⼀个密钥,然后把它保存到本地⽂件中,然后再把它读出来,分别⽤以加密和解密。⽽且我们加密和解密都是⽤的同⼀个Cipher对象,但是在使⽤前需要重新通过init⽅法初始化加解密模式。
感谢阅读,希望能帮助到⼤家,谢谢⼤家对本站的⽀持!

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