AES对称加密实例
Posted DataRain
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AES对称加密实例相关的知识,希望对你有一定的参考价值。
这是一个AES对称加密的实例,主要是用于对文字或者文件进行加密,这个实例不需要引入其他包,只需要使用JDK提供的方法就可以实现对称加解密的功能。记录这个主要是因为有的示例中会使用bcprov-jdk15on这个包中所提供的Provider算法,即引入以下类型包:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.51</version>
</dependency>
例如使用这个包里面的算法示例为:
Cipher cipher = Cipher.getInstance(ALGORITHMS
, new org.bouncycastle.jce.provider.BouncyCastleProvider());
我在使用这个包里面的算法时候曾经遇到过一种问题,初步估计是由于JDK的版本问题,会引起下面的异常:
java.lang.SecurityException: JCE cannot authenticate the provider BC
at javax.crypto.Cipher.getInstance(Cipher.java:657)
因此为了避免不必要的问题,在简单使用AES加密方法的情况下,不需要采用这个包中的加解密算法,直接使用JDK中的方法即可。
/**
* AES加、解密算法工具类
*/
public class AESUtil {
/**
* 加密算法AES
*/
private static final String KEY_ALGORITHM = "AES";
/**
* key size: must be equal to 128, 192 or 256
*/
private static final Integer KEY_LENGTH = 128;
/**
* 算法名称/加密模式/数据填充方式
*/
private static final String ALGORITHMS = "AES/ECB/PKCS5Padding";
/**
* 默认加解密码
*/
public static final String DEFAULT_ENCRYPT_PASSWORD = "pass_word";
private AESUtil() {
}
/**
* 加密
*
* @param content 加密的字符串
* @param encryptKey key值
*/
public static String encrypt(String content, String encryptKey)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(ALGORITHMS);
cipher.init(Cipher.ENCRYPT_MODE, generateKey(encryptKey));
byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
return Base64.encodeBase64String(b);
}
/**
* 加密文件
*
* @param inFilePath 需要加密的文件路径
* @param outFilePath 加密文件输出路径
* @param encryptKey 加密密钥
* @throws java.io.IOException
* @throws javax.crypto.NoSuchPaddingException
* @throws java.security.NoSuchAlgorithmException
* @throws java.security.InvalidKeyException
*/
public static void encryptFileToFile(String inFilePath, String outFilePath, String encryptKey)
throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
Cipher cipher = Cipher.getInstance(ALGORITHMS);
cipher.init(Cipher.ENCRYPT_MODE, generateKey(encryptKey));
try (InputStream inputStream = new FileInputStream(new File(inFilePath));
InputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
OutputStream outputStream = new FileOutputStream(new File(outFilePath))) {
int isRead;
byte[] cache = new byte[1024];
while ((isRead = cipherInputStream.read(cache, 0, cache.length)) != -1) {
outputStream.write(cache, 0, isRead);
}
}
}
/**
* 解密文件
*
* @param inFilePath 需要解密的文件路径
* @param encryptKey 解密密钥
* @return 解密后的文件输出流
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws IOException
*/
public static ByteArrayOutputStream decryptFileToStream(String inFilePath, String encryptKey)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IOException {
Cipher cipher = Cipher.getInstance(ALGORITHMS);
cipher.init(Cipher.DECRYPT_MODE, generateKey(encryptKey));
try (InputStream inputStream = new FileInputStream(new File(inFilePath));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher)) {
int isRead;
byte[] cache = new byte[1024];
while ((isRead = inputStream.read(cache, 0, cache.length)) != -1) {
cipherOutputStream.write(cache, 0, isRead);
}
return outputStream;
}
}
/**
* 解密
*
* @param encryptStr 解密的字符串
* @param decryptKey 解密的key值
*/
public static String decrypt(String encryptStr, String decryptKey)
throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException,
InvalidKeyException {
byte[] decodeBase64 = Base64.decodeBase64(encryptStr);
Cipher cipher = Cipher.getInstance(ALGORITHMS);
cipher.init(Cipher.DECRYPT_MODE, generateKey(decryptKey));
byte[] decryptBytes = cipher.doFinal(decodeBase64);
return new String(decryptBytes);
}
/**
* create the secret key with the password
*
* @param password user define password
* @return the secret key object
* @throws java.security.NoSuchAlgorithmException
*/
private static SecretKey generateKey(String password) throws NoSuchAlgorithmException {
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes(StandardCharsets.UTF_8));
KeyGenerator gen = KeyGenerator.getInstance(KEY_ALGORITHM);
gen.init(KEY_LENGTH, random);
return gen.generateKey();
}
}
以上是关于AES对称加密实例的主要内容,如果未能解决你的问题,请参考以下文章
更新实现代码对称加密与解密剖析:AES,高级加密标准(Advanced Encryption Standard,缩写AES)