java加密算法之AES篇

12 篇文章 0 订阅
10 篇文章 0 订阅
加密
  字符串
public static String encode(String encodeKey,String content){
    try {
        KeyGenerator keygen= KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(encodeKey.getBytes());
        keygen.init(128, random);
        SecretKey originalKey=keygen.generateKey();
        byte [] raw=originalKey.getEncoded();
        SecretKey key=new SecretKeySpec(raw, "AES");
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher=Cipher.getInstance("AES","BC");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] data = content.getBytes("utf-8");
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return MyBase64Utils.encodeStringForString(encryptedData);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
  map
public static String encode(String encodeKey, Map<String,Object> content){
    try {
        KeyGenerator keygen= KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        random.setSeed(encodeKey.getBytes());
        keygen.init(128, random);
        SecretKey originalKey=keygen.generateKey();
        byte [] raw=originalKey.getEncoded();
        SecretKey key=new SecretKeySpec(raw, "AES");
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher=Cipher.getInstance("AES","BC");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] data = JsonUtils.toJson(content).getBytes();
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return MyBase64Utils.encodeStringForString(encryptedData);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
解密
public static String decode(String encodeKey,String decodeContent){
   try {
        KeyGenerator keygen=KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG",  "SUN");
        random.setSeed(encodeKey.getBytes());
        keygen.init(128, random);
        SecretKey originalKey=keygen.generateKey();
        byte [] raw=originalKey.getEncoded();
        SecretKey key=new SecretKeySpec(raw, "AES");
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher=Cipher.getInstance("AES","BC");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] encryptedData = MyBase64Utils.decodeStringForByte(decodeContent);
        int inputLen = encryptedData.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段解密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
        byte[] decryptedData = out.toByteArray();
        out.close();
        return new String(decryptedData);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
测试
 public static void main(String[] args) {
     String encode = encode("128742634", "I'm encode content");
     String decode = decode("128742634", encode);
     System.out.println("Decode result is :"+decode);
 }
测试结果

在这里插入图片描述

全代码
public class AESUtils {

    /**
     * RSA最大加密明文大小
     */
    private static final int MAX_ENCRYPT_BLOCK = 117;
    /**
     * RSA最大解密密文大小
     */
    private static final int MAX_DECRYPT_BLOCK = 128;


    public static String encode(String encodeKey,String content){
        try {
            KeyGenerator keygen= KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(encodeKey.getBytes());
            keygen.init(128, random);
            SecretKey originalKey=keygen.generateKey();
            byte [] raw=originalKey.getEncoded();
            SecretKey key=new SecretKeySpec(raw, "AES");
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher=Cipher.getInstance("AES","BC");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] data = content.getBytes("utf-8");
            int inputLen = data.length;
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int offSet = 0;
            byte[] cache;
            int i = 0;
            // 对数据分段加密
            while (inputLen - offSet > 0) {
                if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                    cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
                } else {
                    cache = cipher.doFinal(data, offSet, inputLen - offSet);
                }
                out.write(cache, 0, cache.length);
                i++;
                offSet = i * MAX_ENCRYPT_BLOCK;
            }
            byte[] encryptedData = out.toByteArray();
            out.close();
            return MyBase64Utils.encodeStringForString(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String encode(String encodeKey, Map<String,Object> content){
        try {
            KeyGenerator keygen= KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
            random.setSeed(encodeKey.getBytes());
            keygen.init(128, random);
            SecretKey originalKey=keygen.generateKey();
            byte [] raw=originalKey.getEncoded();
            SecretKey key=new SecretKeySpec(raw, "AES");
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher=Cipher.getInstance("AES","BC");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] data = JsonUtils.toJson(content).getBytes();
            int inputLen = data.length;
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int offSet = 0;
            byte[] cache;
            int i = 0;
            // 对数据分段加密
            while (inputLen - offSet > 0) {
                if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                    cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
                } else {
                    cache = cipher.doFinal(data, offSet, inputLen - offSet);
                }
                out.write(cache, 0, cache.length);
                i++;
                offSet = i * MAX_ENCRYPT_BLOCK;
            }
            byte[] encryptedData = out.toByteArray();
            out.close();
            return MyBase64Utils.encodeStringForString(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decode(String encodeKey,String decodeContent){
        try {
            KeyGenerator keygen=KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG",  "SUN");
            random.setSeed(encodeKey.getBytes());
            keygen.init(128, random);
            SecretKey originalKey=keygen.generateKey();
            byte [] raw=originalKey.getEncoded();
            SecretKey key=new SecretKeySpec(raw, "AES");
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher=Cipher.getInstance("AES","BC");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encryptedData = MyBase64Utils.decodeStringForByte(decodeContent);
            int inputLen = encryptedData.length;
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int offSet = 0;
            byte[] cache;
            int i = 0;
            // 对数据分段解密
            while (inputLen - offSet > 0) {
                if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                    cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
                } else {
                    cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
                }
                out.write(cache, 0, cache.length);
                i++;
                offSet = i * MAX_DECRYPT_BLOCK;
            }
            byte[] decryptedData = out.toByteArray();
            out.close();
            return new String(decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static void main(String[] args) {
        String encode = encode("128742634", "I'm encode content");
        String decode = decode("128742634", encode);
        System.out.println("Decode result is :"+decode);
    }
}

>>>源码下载链接>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwx-apollo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值