赞
踩
目录
我们为什么要用AES算法来进行加解密?
AES(Advanced Encryption Standard),又称高级加密标准,是一种对称加密算法,也是目前广泛使用的加密技术之一。其主要特点是加密速度快、安全性高、可扩展性好等。
AES 算法采用对称加密的方式,即加密和解密使用相同的密钥进行操作。密钥长度可以是 128、192 或 256 位,其中 128 位密钥被广泛使用,因为它可以提供足够的安全性和高效的加密速度。AES 加密和解密过程中采用分块加密的方式,即将明文分成若干个块,再分别进行加密操作,最后将加密得到的密文合并起来。
AES 算法具有以下优点:
安全性高:AES 算法采用了强大的对称加密技术,能够提供非常高的安全性,能够有效保护数据的机密性和完整性。
速度快:AES 算法采用了分组加密的方式,能够充分利用硬件加速和多线程技术,具有非常高效的加密速度。
可扩展性好:由于 AES 算法的密钥长度可以灵活调整,因此可以提供比较灵活的加密需求,适用于不同场景和应用。
常见性高:AES 算法在现代密码学领域得到广泛应用,是目前最常用的对称加密算法之一,因此在实际应用中具有良好的兼容性和可移植性。
AES 算法主要用于网络通信、数据存储、加密解密等方面。在网络通信中,AES 加密可以保护互联网数据的机密性和完整性,防止敏感信息被窃取和篡改;在数据存储中,AES 加密可以保护存储的数据,避免盗窃和泄露;在加密解密方面,AES 算法可以用于实现各种安全协议和应用程序,例如 SSL/TLS、IPSec、PGP 等。
- crypto:
- #AES消息秘钥
- ASE_SIGN_KEY: MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCFeSnQmMwPfwnvrozctUM8YmkvieWgsxvh2aAl71GVeGcsxCvIO1K9uAPFJdpSwATBRR3v0V6PYsatO9t0n3WF9HvX2LVkF+uKI7g5IdXsTe5e5XORMz4ESYvus2onq2SmMUCTel8Wd0hQNWB90GG9Mc1TT4uwhMyTR37t6DSctZXelyx1f6fOb2RIz+eDD8tI7feibBo17Jkmh3cK96xYUC4wPiSRbVRv96+1jc4lbi4qfwPXRnxbrDWD3NJxh4yt4abrp0BNq4o0vEDqmyjtOytOW1gdmHPvDML7LO10xm4lDYVTvqyow0R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Tz6473cdljd4GB0T/JWGFX2HNzvEcs=
- #签名公钥
- SIGNATURE_PUBLIC_KEY: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvRr+SjbZOHIRwcd99rS8YDp6Cfc6Q4jEOmHGbiztxgvyDS1jO9SPy78j6VNKcAtTcAhtBjIDT2sezupIjZKf9H/t3+4zxUH9Kj+ybBrJNW3ZhSMunDp5K7x9RcmCRnU8QcCcYsEwa90FDd54f3YzuUD3kX35/mIfWZCLD3mY6d7txzGHlWlV84UyQDK1cqAYlUvMyHg7Ff0q1NrVwXU0fVC8PBtxnqzukny42oDEN0r7jIvrK0sKmZSVHfoHVjWPhVL9RWuN/GP2RV0k0nb856A9cxedlV236lSBpPNYgWiglPGWxTmrwOI66GE1PZ/DMJBp+RaPveh52NA0mA4JoQIDAQAB
- package com.jmh.demo03.com.blog;
-
-
- import org.apache.tomcat.util.codec.binary.Base64;
-
- import javax.crypto.Cipher;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import java.nio.charset.StandardCharsets;
-
- /**
- * AES加密工具类
- */
- public class AESEncryptUtil {
-
- /**
- * 参数分别代表 算法名称/加密模式/数据填充方式
- */
- private static final String ALGORITHMS = "AES/CBC/PKCS5Padding";
-
- private static final String AES = "AES";
-
- /**
- * 加密
- *
- * @param content 未加密的字符串
- * @param encryptKey key值 字节数组 ----16位
- * @param SIV 加盐 字节数组 ----16位
- * @return 加密后的内容
- * @throws Exception 异常
- */
- public static String encrypt(String content, byte[] encryptKey, byte[] SIV) throws Exception {
- Cipher cipher = Cipher.getInstance(ALGORITHMS);
- // 加密向量
- IvParameterSpec iv = new IvParameterSpec(SIV);
- cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey, AES), iv);
- byte[] encryptByte = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
- // 采用base64算法进行转码,避免出现中文乱码
- return Base64.encodeBase64String(encryptByte);
- }
-
- /**
- * 解密
- *
- * @param encodeEncryptString 未解密的字符串
- * @param decryptKey 解密的key值 字节数组 ----16位
- * @param SIV 加盐 字节数组 ----16位
- * @return 解密后的内容
- * @throws Exception 异常
- */
- public static String decrypt(String encodeEncryptString, byte[] decryptKey, byte[] SIV) throws Exception {
- Cipher cipher = Cipher.getInstance(ALGORITHMS);
- // 加密向量
- IvParameterSpec iv = new IvParameterSpec(SIV);
- cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey, AES), iv);
- // 采用base64算法进行转码,避免出现中文乱码
- byte[] decryptByte = cipher.doFinal(Base64.decodeBase64(encodeEncryptString));
- return new String(decryptByte, StandardCharsets.UTF_8);
- }
-
- }
- package com.jmh.service.base.util;
-
- import java.security.MessageDigest;
-
- /**
- * 数字摘要算法工具
- *
- */
- public class MessageDigestUtil {
- // Md:Md2、Md4、Md5(产生出一个128位,即16字节的散列值(hash value)。在Java中可以将其Hex为一个32长度的字符串。)
- // SHA:SHA1、SHA224、SHA256、SHA384、SHA512
-
- private static final String MD5 = "MD5";
- private static final String SHA1 = "SHA-1";
- private static final String SHA224 = "SHA-224";
- private static final String SHA256 = "SHA-256";
- private static final String SHA384 = "SHA-384";
- private static final String SHA512 = "SHA-512";
- private static final String CHARSET = "UTF-8";
-
- /**
- * @param input 输入 ----Byte
- * @return 返回16个字节
- * @throws Exception
- */
- public static byte[] MD5(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest md5 = MessageDigest.getInstance(MD5);
- // 执行消息摘要算法
- return md5.digest(input);
- }
-
- /**
- * @param input 输入 ----Byte
- * @return 返回20个字节
- * @throws Exception
- */
- public static byte[] SHA1(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha1 = MessageDigest.getInstance(SHA1);
- // 执行消息摘要算法
- return sha1.digest(input);
- }
-
- /**
- * @param input 输入 ----Byte
- * @return 返回28个字节
- * @throws Exception
- */
- public static byte[] SHA224(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha224 = MessageDigest.getInstance(SHA224);
- // 执行消息摘要算法
- return sha224.digest(input);
- }
-
- /**
- * @param input 输入 ----Byte
- * @return 返回32个字节
- * @throws Exception
- */
- public static byte[] SHA256(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha256 = MessageDigest.getInstance(SHA256);
- // 执行消息摘要算法
- return sha256.digest(input);
- }
-
- /**
- * @param input 输入 ----Byte
- * @return 返回48个字节
- * @throws Exception
- */
- public static byte[] SHA384(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha384 = MessageDigest.getInstance(SHA384);
- // 执行消息摘要算法
- return sha384.digest(input);
- }
-
- /**
- * @param input ----Byte
- * @return 返回64个字节
- * @throws Exception
- */
- public static byte[] SHA512(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha512 = MessageDigest.getInstance(SHA512);
- // 执行消息摘要算法
- return sha512.digest(input);
- }
-
- /**
- * @param input 输入 ----String
- * @return 返回16个字节
- * @throws Exception
- */
- public static byte[] MD5(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest md5 = MessageDigest.getInstance(MD5);
- // 执行消息摘要算法
- return md5.digest(input.getBytes("UTF-8"));
- }
-
- /**
- * @param input 输入 ----String
- * @return 返回20个字节
- * @throws Exception
- */
- public static byte[] SHA1(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha1 = MessageDigest.getInstance(SHA1);
- // 执行消息摘要算法
- return sha1.digest(input.getBytes("UTF-8"));
- }
-
- /**
- * @param input 输入 ----String
- * @return 返回28个字节
- * @throws Exception
- */
- public static byte[] SHA224(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha224 = MessageDigest.getInstance(SHA224);
- // 执行消息摘要算法
- return sha224.digest(input.getBytes("UTF-8"));
- }
-
- /**
- * @param input 输入 ----String
- * @return 返回32个字节
- * @throws Exception
- */
- public static byte[] SHA256(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha256 = MessageDigest.getInstance(SHA256);
- // 执行消息摘要算法
- return sha256.digest(input.getBytes("UTF-8"));
- }
-
- /**
- * @param input 输入 ----String
- * @return 返回48个字节
- * @throws Exception
- */
- public static byte[] SHA384(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha384 = MessageDigest.getInstance(SHA384);
- // 执行消息摘要算法
- return sha384.digest(input.getBytes("UTF-8"));
- }
-
- /**
- * @param input ----String
- * @return 返回64个字节
- * @throws Exception
- */
- public static byte[] SHA512(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha512 = MessageDigest.getInstance(SHA512);
- // 执行消息摘要算法
- return sha512.digest(input.getBytes("UTF-8"));
- }
-
- /**
- * @param input 输入 ----Byte
- * @return 返回32个字符16进制字符串
- * @throws Exception
- */
- public static String MD5ToHex(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest md5 = MessageDigest.getInstance(MD5);
- // 执行消息摘要算法
- return toHex(md5.digest(input));
- }
-
- /**
- * @param input 输入 ----Byte
- * @return 返回40个字符16进制字符串
- * @throws Exception
- */
- public static String SHA1ToHex(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha1 = MessageDigest.getInstance(SHA1);
- // 执行消息摘要算法
- return toHex(sha1.digest(input));
- }
-
- /**
- * @param input 输入 ----Byte
- * @return 返回56个字符16进制字符串
- * @throws Exception
- */
- public static String SHA224ToHex(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha224 = MessageDigest.getInstance(SHA224);
- // 执行消息摘要算法
- return toHex(sha224.digest(input));
- }
-
- /**
- * @param input 输入 ----Byte
- * @return 返回64个字符16进制字符串
- * @throws Exception
- */
- public static String SHA256ToHex(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha256 = MessageDigest.getInstance(SHA256);
- // 执行消息摘要算法
- return toHex(sha256.digest(input));
- }
-
- /**
- * @param input 输入 ----Byte
- * @return 返回96个字符16进制字符串
- * @throws Exception
- */
- public static String SHA384ToHex(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha384 = MessageDigest.getInstance(SHA384);
- // 执行消息摘要算法
- return toHex(sha384.digest(input));
- }
-
- /**
- * @param input ----Byte
- * @return 返回128个字符16进制字符串
- * @throws Exception
- */
- public static String SHA512ToHex(byte[] input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha512 = MessageDigest.getInstance(SHA512);
- // 执行消息摘要算法
- return toHex(sha512.digest(input));
- }
-
- /**
- * @param input 输入 ----String
- * @return 返回32个字符16进制字符串
- * @throws Exception
- */
- public static String MD5ToHex(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest md5 = MessageDigest.getInstance(MD5);
- // 执行消息摘要算法
- return toHex(md5.digest(input.getBytes("UTF-8")));
- }
-
- /**
- * @param input 输入 ----String
- * @return 返回40个字符16进制字符串
- * @throws Exception
- */
- public static String SHA1ToHex(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha1 = MessageDigest.getInstance(SHA1);
- // 执行消息摘要算法
- return toHex(sha1.digest(input.getBytes("UTF-8")));
- }
-
- /**
- * @param input 输入 ----String
- * @return 返回56个字符16进制字符串
- * @throws Exception
- */
- public static String SHA224ToHex(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha224 = MessageDigest.getInstance(SHA224);
- // 执行消息摘要算法
- return toHex(sha224.digest(input.getBytes("UTF-8")));
- }
-
- /**
- * @param input 输入 ----String
- * @return 返回64个字符16进制字符串
- * @throws Exception
- */
- public static String SHA256ToHex(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha256 = MessageDigest.getInstance(SHA256);
- // 执行消息摘要算法
- return toHex(sha256.digest(input.getBytes("UTF-8")));
- }
-
- /**
- * @param input 输入 ----String
- * @return 返回96个字符16进制字符串
- * @throws Exception
- */
- public static String SHA384ToHex(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha384 = MessageDigest.getInstance(SHA384);
- // 执行消息摘要算法
- return toHex(sha384.digest(input.getBytes("UTF-8")));
- }
-
- /**
- * @param input ----String
- * @return 返回128个字符16进制字符串
- * @throws Exception
- */
- public static String SHA512ToHex(String input) throws Exception {
- // 创建消息摘要对象
- MessageDigest sha512 = MessageDigest.getInstance(SHA512);
- // 执行消息摘要算法
- return toHex(sha512.digest(input.getBytes("UTF-8")));
- }
-
- /**
- * 将字节数组转换成16进制字符串
- * @param digest
- * @return
- */
- private static String toHex(byte[] digest) {
- StringBuilder stringBuilder = new StringBuilder();
- // 对密文进行迭代
- for (byte b : digest) {
- // 把密文转换成16进制
- String string = Integer.toHexString(b & 0xff);
- if (string.length() == 1) {
- string = "0" + string;
- }
- stringBuilder.append(string);
- }
- return stringBuilder.toString();
- }
-
- }
- package com.fesion.service.service.jmhDemo;
-
- import com.fesion.service.base.util.AESEncryptUtil;
- import com.fesion.service.base.util.MessageDigestUtil;
- import lombok.SneakyThrows;
- import lombok.extern.slf4j.Slf4j;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- /**
- * @author 蒋明辉
- * @data 2023/6/18 23:25
- */
- @SpringBootTest
- @RunWith(SpringRunner.class)
- @Slf4j
- public class Demo02 {
-
- /**
- * AES消息秘钥,这个就是我们在第一步application.yml文件里面配置的
- */
- @Value("${crypto.ASE_SIGN_KEY}")
- private String ASE_SIGN_KEY;
-
- /**
- * AES秘钥初始向量
- */
- @Value("${crypto.ASE_SIV}")
- private String ASE_SIV;
-
- /**
- * AES加解密
- */
- @Test
- @SneakyThrows
- public void demo01(){
- //加密--------------------------------
- //要加密的内容
- String conent="蒋明辉是大帅哥呢!!!";
- String encrypt = AESEncryptUtil.encrypt(conent, MessageDigestUtil.MD5(ASE_SIGN_KEY), MessageDigestUtil.MD5(ASE_SIV));
-
- //解密--------------------------------
- String decrypt = AESEncryptUtil.decrypt(encrypt, MessageDigestUtil.MD5(ASE_SIGN_KEY), MessageDigestUtil.MD5(ASE_SIV));
-
- System.out.println("-----------------加密---------------------");
- System.out.println("加密的内容:"+encrypt);
- System.out.println("-----------------解密---------------------");
- System.out.println("加密的内容:"+decrypt);
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。