星星博客 »  > 

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);
        }
    }
}

相关文章