java实现AES方式加密
admin
2023-01-31 05:41:57
0
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 org.apache.commons.codec.binary.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class AESUtil {
    private static final String IV_STRING = "sdf4ddfsFD86Vdf2";
    private static final String encoding = "UTF-8";
    public static String encryptAES(String content, String key)
            throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, UnsupportedEncodingException,
            InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        byte[] byteContent = content.getBytes(encoding);
        // 注意,为了能与 iOS 统一
        // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
        byte[] enCodeFormat = key.getBytes(encoding);
        SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
        byte[] initParam = IV_STRING.getBytes(encoding);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
        // 指定加密的算法、工作模式和填充方式
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(byteContent);
        // 同样对加密后数据进行 base64 编码
        String base64 = new Base64().encodeToString(encryptedBytes);
        //进行url编码 去掉= ? &
        return URLEncoder.encode(base64,encoding);
    }
    public static String decryptAES(String content, String key)
            throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidAlgorithmParameterException,
            IllegalBlockSizeException, BadPaddingException, IOException {
        //URL解码
        content = URLDecoder.decode(content,encoding);
        // base64 解码
        byte[] encryptedBytes = Base64.decodeBase64(content);
        byte[] enCodeFormat = key.getBytes(encoding);
        SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
        byte[] initParam = IV_STRING.getBytes(encoding);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
        byte[] result = cipher.doFinal(encryptedBytes);
        return new String(result, encoding);
    }
    
    public static void main(String[] args) throws Exception {
    JSONObject json = new JSONObject();
    json.put("custNum", "111111");
    /*json.put("name", "张三");
    json.put("cityCode", "100001");
    json.put("cityName", "北京市");
    json.put("mobileNo", "15651876590");*/
        String content = json.toJSONString();
        System.out.println("加密前:" + content);
        String key = "djadiKJdj49dFJLd";
        System.out.println("加密密钥和解密密钥:" + key);
        String encrypt = encryptAES(content, key);
        System.out.println("加密后:" + encrypt);
        String decrypt = decryptAES(encrypt , key);
        System.out.println("解密后:" + decrypt);
    }
}


相关内容

热门资讯

玩家最新攻略“阳光巴厘岛.有没... 家人们!今天小编来为大家解答阳光巴厘岛透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里买...
今日重大通报“大庆划水麻将.怎... 家人们!今天小编来为大家解答大庆划水麻将透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里...
玩家攻略科普“欢乐龙城9.究竟... 您好:欢乐龙城9这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款游...
终于明白“青松游戏.真的有挂吗... 您好:青松游戏这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款游戏...
【第一资讯】“火神牛牛.怎么开... 有 亲,根据资深记者爆料火神牛牛是可以开挂的,确实有挂(咨询软件无需打开...
今日重大消息“大庆划水麻将.有... 网上科普关于“大庆划水麻将有没有挂”话题很是火热,小编也是针对大庆划水麻将作*弊开挂的方法以及开挂对...
今日重大通报“同城跑胡子.到底... 网上科普关于“同城跑胡子有没有挂”话题很是火热,小编也是针对同城跑胡子作*弊开挂的方法以及开挂对应的...
【第一消息】“灯塔麻将.辅助开... 家人们!今天小编来为大家解答灯塔麻将透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里买很...
【第一消息】“沧海麻将.到底是... 网上科普关于“沧海麻将有没有挂”话题很是火热,小编也是针对沧海麻将作*弊开挂的方法以及开挂对应的知识...
京东:近期有不法分子冒名邮寄虚... 【大河财立方消息】 12月22日,京东发言人微博发文称,近期,发现有不法分子冒充京东向用户邮寄低价值...