aes+base64带有效期的消息加密、解密

package util;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

/**
 * @author: sk
 * @date: 2020/8/10
 * @des: 对userName通过AES加密,附带有效期。
 */
public class AESUtil {
    //秘钥随机密码
    private static String pwd = "aaa";
    //密文有效期10分钟
    private static Integer validity = 10;

    /**
     *@描述 :加密
     *@参数
     *@返回值
     *@创建人  sk
     *@创建时间  2020/8/10
     *@修改人和其它信息
     */
    public static String encrypt(String content) {
        byte[] result = null;
        try {
            if (StringUtils.isEmpty(content)){
                return null;
            }
            //获取一个秘钥生成器
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(pwd.getBytes());
            //初始化128位key
            kgen.init(128, random);
            //生产一个秘钥
            SecretKey secretKey = kgen.generateKey();
            //返回基本编码格式秘钥,如果秘钥不支持编码则返回为空!
            byte[] enCodeFormat = secretKey.getEncoded();
            //转换为AES专用秘钥
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            //创建密码器
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            //初始化为加密模式密码器
            cipher.init(Cipher.ENCRYPT_MODE, key);
            /**时间戳放入明文在解密时验证密文是否在有效期内**/
            content = System.currentTimeMillis()+"@" + content;
            byte[] byteContent = content.getBytes();
            //加密
            result = cipher.doFinal(byteContent);
        } catch (Exception e) {
            return null;
        }
        //返回base64编码密文
        return new String(Base64.encode(result));
    }

    /**
     *@描述 :解密
     *@参数
     *@返回值
     *@创建人  sk
     *@创建时间  2020/8/10
     *@修改人和其它信息
     */
    public static String decrypt(String content) {
        byte[] result = null;
        byte[] bytes = Base64.decode(content);
        try {
            //获取一个秘钥生成器
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(pwd.getBytes());
            //初始化为128位key
            kgen.init(128, random);
            //生成一个秘钥
            SecretKey secretKey = kgen.generateKey();
            //生成一个基本编码格式秘钥
            byte[] enCodeFormat = secretKey.getEncoded();
            //转换为AES专用秘钥
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            //创建密码器
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            //初始化为加密模式密码器
            cipher.init(Cipher.DECRYPT_MODE, key);
            //解密
            result = cipher.doFinal(bytes);
        } catch (Exception e) {
            return null;
        }
        String plaintext = new String(result);
        String[] split = plaintext.split("@");
        //计算有效期
        boolean b = (System.currentTimeMillis() - Long.parseLong(split[0])) / (1000 * 60) <= validity;
        //有效期内,返回明文
        if (b){
            return split[1];
        }
        //密文失效,返回null
        else {
            return null;
        }
    }



    public static void main(String[] args) throws InterruptedException {
//        String content = "123654哈哈";
//        System.out.println("加密前=" + content);
//        String encrypt = AESUtil.encrypt(content);
//        System.out.println("加密后=" + encrypt);
//        Thread.sleep(200000);
        String decrypt = AESUtil.decrypt("e/4cLTX722PgxXkHG30s5OedHOi44UGOqkC6ei3eWuc=");
        System.out.println("解密后=" + decrypt);

    }


}

 

posted @ 2020-08-11 10:55  蓝领笑笑生  阅读(1002)  评论(0编辑  收藏  举报