aes加密工具类

这是我的网站kongjs.com刚开始写的工具类,用aes加密

package com.kongjs.diary.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;

public class AesCryptoUtils {
    private static final Log logger = LogFactory.getLog(AesCryptoUtils.class);
    private static final String salt = "K^JW7&cz%!9u4ug#";
    private static final String iv = "K^JW7&cz%!9u4ug#";
    private static byte[] saltKey = salt.getBytes(StandardCharsets.UTF_8);
    private static final byte[] ivKey = iv.getBytes(StandardCharsets.UTF_8);
    private static final SecretKey secretKey = new SecretKeySpec(saltKey, "AES");
    private AesCryptoUtils(){}
    public static String encode(CharSequence rawPassword) {
        String encode = null;
        try {
            byte[] rawPass = rawPassword.toString().getBytes(StandardCharsets.UTF_8);
            byte[] encrypted;
            logger.info("AES 加密");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivKey);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
            encrypted = cipher.doFinal(rawPass);
            encode = Base64.getEncoder().encodeToString(encrypted);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NullPointerException | InvalidAlgorithmParameterException e) {
            logger.error("AES 加密失败",e);
        }
        return encode;
    }

    public static String decode(CharSequence password) {
        String decode = null;
        try {
            byte[] pass = Base64.getDecoder().decode(password.toString().getBytes(StandardCharsets.UTF_8));
            byte[] decrypted;
            logger.info("AES 解密");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivKey);
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            decrypted = cipher.doFinal(pass);
            decode = new String(decrypted);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NullPointerException | InvalidAlgorithmParameterException e) {
            logger.error("AES 解密失败",e);
        }
        return decode;
    }

    public static String getSalt() {
        return new String(Base64.getEncoder().encode(saltKey));
    }

    public static void setSalt(CharSequence slatKey) {
        KeyGenerator keygen;
        SecureRandom secureRandom;
        try {
            logger.info("AES 加盐");
            keygen = KeyGenerator.getInstance("AES");
            secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(slatKey.toString().getBytes());
            keygen.init(128, secureRandom);
            Key key = keygen.generateKey();
            saltKey = key.getEncoded();
        } catch (NoSuchAlgorithmException e) {
            logger.error("AES 加盐失败",e);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值