前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java中使用AES对数据进行加密

Java中使用AES对数据进行加密

作者头像
geekfly
发布2022-05-06 19:36:34
6260
发布2022-05-06 19:36:34
举报
文章被收录于专栏:geekflygeekfly

此处介绍的是Java自带的AES加密算法,并且支持中文,具体参数如下:

算法模式:ECB 密钥 长度:128bits 16位长 偏移量: 默认 补码方式:PKCS5Padding 解密串编码方式:base64

秘钥为16为长度的字符串。 1. 加密函数

代码语言:javascript
复制
 /**
     * 使用参数中的密钥加密
     * @param 明文
     * @param 密钥
     * @return 密文
     */
    public static String Encrypt(String sSrc, String sKey) {
        try{
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

            return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

2.解密函数

代码语言:javascript
复制
 /**
     * 使用参数中的密钥解密
     * @param 密文
     * @param 密钥
     * @return 明文
     */
    public static String Decrypt(String sSrc, String sKey) {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

3.测试方法

代码语言:javascript
复制
        String password = "ABCDEFGHIJKLMNOP";
        String content1 = "我的博客名是geekfly";
        System.out.println("加密前:" + content1);  
        String content2 = Encrypt(content1, password);
        System.out.println("加密后:" + content2);
        String content3 = Decrypt(content2, password);
        System.out.println("解密后:" + content3); 

结果

加密前:我的博客名是geekfly 加密后:ef96GdBlS/TAX8R9mGEuA3w+kpcvBDu/8dI1qupbPQA= 解密后:我的博客名是geekfly

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-08-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档