java中 AES/CBC/PKCS7Padding加密解密十六进制或base64

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.Security;

public class AesUtil {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    private static final String IV = "33AB00C0B0Q00F12";
    private static final String KEY = "34bfeacAB39053deb006df63";
    private static final String AES_ALGORITHM = "AES/CBC/PKCS7Padding";


    private static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length() / 2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++) {
            result[i] = (byte) Integer.parseInt(hexString.substring(2 * i, 2 * i + 2), 16);
        }
        return result;
    }

    private static String byteArrayToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b & 0xff));
        }
        return sb.toString();
    }
    /**
     * @description : AES加密返回16进制
     **/
    public static String encryptHex(String data) {
        try {
            Key k = new SecretKeySpec(KEY.getBytes(), "AES");
            byte[] encoded = k.getEncoded();
            SecretKeySpec aes = new SecretKeySpec(encoded, "AES");
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, aes, iv);
            byte[] bytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
            return byteArrayToHexString(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @description : AES解密返回16进制
     **/
    public static String decryptHex(String content) {
        try {
            Key k = new SecretKeySpec(KEY.getBytes(), "AES");
            byte[] encoded = k.getEncoded();
            SecretKeySpec aes = new SecretKeySpec(encoded, "AES");
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, aes, iv);
            byte[] encryptedBytes = hexStringToByteArray(content);
            byte[] bytes = cipher.doFinal(encryptedBytes);
            return new String(bytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * @description : 解密返回 Base64
     **/
    public static String decryptBase64(String content) {
        try {
            Key k = new SecretKeySpec(KEY.getBytes(), "AES");
            byte[] encoded = k.getEncoded();
            SecretKeySpec aes = new SecretKeySpec(encoded, "AES");
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, aes, iv);
            byte[] bytes = cipher.doFinal(Base64.decodeBase64(content));
            return new String(bytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * @description : 加密返回 Base64
     **/
    public static String encryptBase64(String data) {
        try {
            Key k = new SecretKeySpec(KEY.getBytes(), "AES");
            byte[] encoded = k.getEncoded();
            SecretKeySpec aes = new SecretKeySpec(encoded, "AES");
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, aes, iv);
            byte[] bytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
            return Base64.encodeBase64String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String content = "hello world";
        System.out.println(encryptHex(content));
        System.out.println(decryptHex(encryptHex(content)));
        System.out.println(encryptBase64(content));
        System.out.println(decryptBase64(encryptBase64(content)));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想要了解如何在 Spring Boot 使用 AES/CBC/PKCS7Padding解密字符串,我可以给您提供一些帮助。 首先,您需要使用 Java 的 `javax.crypto` 包来进行加解密操作。以下是一个示例代码: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.tomcat.util.codec.binary.Base64; public class AesUtil { private static final String ENCODING = "UTF-8"; private static final String ALGORITHM = "AES"; private static final String TRANSFORMATION = "AES/CBC/PKCS7Padding"; private static final String IV = "0123456789abcdef"; public static String encrypt(String content, String key) throws Exception { byte[] contentBytes = content.getBytes(ENCODING); byte[] keyBytes = key.getBytes(ENCODING); byte[] ivBytes = IV.getBytes(ENCODING); SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, ALGORITHM); IvParameterSpec iv = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv); byte[] encryptedBytes = cipher.doFinal(contentBytes); return Base64.encodeBase64String(encryptedBytes); } public static String decrypt(String content, String key) throws Exception { byte[] contentBytes = Base64.decodeBase64(content); byte[] keyBytes = key.getBytes(ENCODING); byte[] ivBytes = IV.getBytes(ENCODING); SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, ALGORITHM); IvParameterSpec iv = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv); byte[] decryptedBytes = cipher.doFinal(contentBytes); return new String(decryptedBytes, ENCODING); } } ``` 在上面的代码,我们使用 `Cipher` 类来进行加解密操作,其 `TRANSFORMATION` 变量指定了加密算法的名称和填充方式。`encrypt` 方法接受要加密的字符串和密钥作为参数,并返回加密后的字符串。`decrypt` 方法接受要解密的字符串和密钥作为参数,并返回解密后的字符串。 请注意,为了确保加解密结果的一致性,必须使用相同的密钥和初始化向量(IV)进行加解密操作。在上面的代码,我们使用了一个硬编码的 IV 值,但在实际应用,应该使用一个随机生成的 IV 值,以增加加密的安全性。 希望这可以帮助您实现您的需求!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值