加密解密工具类

import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


/**
 * 加密解密工具类
 * @author qiaojun
 */
public class AesUtil {
    private static final String KEY = "l~8^%&^&@jmt6&&*";
    private static final String IV = "(^&%^$qJU/K&^%%J";

    /**
     * 加密
     *
     * @param content 需要加密的内容
     * key  加密密码,16位
     * iv  偏移量,16位
     */
    public static String encrypt(String content) {
        try {
            //1.获取加密key的字节数组。生成AES密钥
            // 注意,为了能与 iOS 统一 ,这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
            byte[] keyBytes = KEY.getBytes();
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");

            //2.获取偏移量的字节数组,实例化IvParameterSpec对象
            byte[] ivBytes = IV.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);

            //3.指定加密的算法、工作模式和填充方式
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

            //4.获取加密内容的字节数组(这里要设置为utf-8),并进行ASE加密
            byte[] contentBytes = content.getBytes("UTF-8");
            byte[] encryptedBytes = cipher.doFinal(contentBytes);

            //5.将加密后的数据Base64加密,并转字符串返回
            return new String(new Base64().encode(encryptedBytes));
        } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | UnsupportedEncodingException
                | BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchPaddingException e) {
            e.printStackTrace();
        }
        //如果有错就返加nulll
        return null;
    }


    /**
     * 解密
     *
     * @param content 需要解密内容
     * key  解密密钥,16位
     * iv  偏移量,16位
     */
    public static String decrypt(String content) {
        try {

            //1.获取加密key的字节数组。生成AES密钥
            // 注意,为了能与 iOS 统一,这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
            byte[] enCodeFormat = KEY.getBytes();
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");

            //2.获取偏移量的字节数组,实例化IvParameterSpec对象
            byte[] ivBytes = IV.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);

            //3.指定加密的算法、工作模式和填充方式
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);

            //4.将待解密内容进行Base64解码
            byte [] contentBytes= new Base64().decode(content.getBytes());

            //5.将Base64解码后的数据进行AES解密
            byte[] decryptedBytes = cipher.doFinal(contentBytes);

            //6、解密后的数据转成字符串返回
            return new String(decryptedBytes, "UTF-8");

        } catch (NoSuchAlgorithmException | IllegalBlockSizeException | InvalidKeyException | NoSuchPaddingException
                | BadPaddingException | InvalidAlgorithmParameterException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下为Java的AES加密解密工具类: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class AESUtil { private static final String ALGORITHM = "AES/CBC/PKCS5Padding"; private static final String CHARSET = "UTF-8"; /** * 加密 * @param data 待加密的字符串 * @param key 密钥 * @param iv 向量 * @return 加密后的字符串 */ public static String encrypt(String data, String key, String iv) { try { Cipher cipher = Cipher.getInstance(ALGORITHM); SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] encrypted = cipher.doFinal(data.getBytes(CHARSET)); return Base64.getEncoder().encodeToString(encrypted); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 解密 * @param data 待解密的字符串 * @param key 密钥 * @param iv 向量 * @return 解密后的字符串 */ public static String decrypt(String data, String key, String iv) { try { Cipher cipher = Cipher.getInstance(ALGORITHM); SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(data)); return new String(decrypted, CHARSET); } catch (Exception e) { e.printStackTrace(); } return null; } } ``` 使用示例: ```java public class Main { public static void main(String[] args) { String data = "Hello, world!"; String key = "1234567890123456"; String iv = "1234567890123456"; String encrypted = AESUtil.encrypt(data, key, iv); String decrypted = AESUtil.decrypt(encrypted, key, iv); System.out.println("明文:" + data); System.out.println("加密后:" + encrypted); System.out.println("解密后:" + decrypted); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值