对称加密——JAVA实现


import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.SecureRandom;

/**
 * @Description: AES加解密工具
 * @Author: yangyongbing
 * @CreateTime: 2023/07/05  15:55
 * @Version: 1.0
 */
public class AESUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(AESUtil.class);

    /**
     * 算法类型:用于指定生成AES的密钥
     */
    private static final String AES_ALGORITHM = "AES";

    // 偏移量
    private static final int OFFSET = 16;

    // 加密器类型:加密算法为AES,加密模式为CBC,补码方式为PKCS5Padding
    private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";



    public static String encrypt(String content, String keySeed){
        Key secretKey = getKey(keySeed);
        try {
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] p = content.getBytes(StandardCharsets.UTF_8);
            byte[] result = cipher.doFinal(p);
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String decrypt(String content, String keySeed) {
        Key secretKey = getKey(keySeed);
        try {
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            BASE64Decoder decoder = new BASE64Decoder();
            String decodeContent = URLDecoder.decode(content, "utf-8");
            byte[] c = decoder.decodeBuffer(decodeContent);
            byte[] result = cipher.doFinal(c);
            return new String(result, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static Key getKey(String keySeed) {
        try {
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(keySeed.getBytes());
            KeyGenerator generator = KeyGenerator.getInstance(AES_ALGORITHM);
            generator.init(secureRandom);
            return generator.generateKey();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


//    public static String encrypt(String content, String keySeed){
//        SecretKeySpec secretKey = new SecretKeySpec(keySeed.getBytes(), AES_ALGORITHM);
//        // 创建初始向量iv用于指定密钥偏移量(可自行指定但必须为128位),因为AES是分组加密,下一组的iv就用上一组加密的密文来充当
        IvParameterSpec parameterSpec = new IvParameterSpec(keySeed.getBytes(), 0, OFFSET);
//        try {
//            // 创建AES加密器
//            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
//            byte[] byteContent  = content.getBytes(StandardCharsets.UTF_8);
//            // 使用加密器的加密模式
//            cipher.init(Cipher.ENCRYPT_MODE,secretKey);
//            // 加密
//            byte[] result = cipher.doFinal(byteContent);
//            // 使用BASE64对加密后的二进制数组进行编码
//            return new Base64().encodeAsString(result);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    public static String decrypt(String content, String keySeed) {
//        SecretKeySpec secretKey = new SecretKeySpec(keySeed.getBytes(), AES_ALGORITHM);
        IvParameterSpec parameterSpec = new IvParameterSpec(keySeed.getBytes(), 0, OFFSET);
//
//        try {
//            // 创建AES加密器
//            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
//            cipher.init(Cipher.DECRYPT_MODE, secretKey);
//            // 解密
//            String decodeContent = URLDecoder.decode(content, "utf-8");
//            byte[] result = cipher.doFinal(new Base64().decode(decodeContent));
//            return new String(result);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }


}

你可能感兴趣的:(安全,java)