跳至主要內容

Java 实现 AES 加密解密应用

JI,XIAOYONG...大约 2 分钟

简介

最近手机中涉及到用户账户密码保存的问题,选用 AES 加密算法进行加密后,再通过 SharedPreferences 保存在手机端。

本文主要介绍 AES 的加密、解密用法。

代码

初始化秘钥

    private static final String AES = "AES";
    private static final String PASSWPRD = "123456";

    public static SecretKeySpec initKey(){
        SecretKeySpec key = null;

        try {

            KeyGenerator kg = KeyGenerator.getInstance(AES);
            kg.init(128,new SecureRandom(PASSWPRD.getBytes()));//通过这种算法,每次生成的 key 都是一样的
          //也可以 kg.init(128),这样每次生成的 key 都不一样
            SecretKey securityKey = kg.generateKey();
            byte[] encodedKey = securityKey.getEncoded();
            key = new SecretKeySpec(encodedKey, AES);//AES 也可以替换为"AES/CBC/PKCS5PADDING"

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return key;
    }

加密

//核心代码
//source 是要加密的内容
Cipher cipher = Cipher.getInstance(AES);//创建密码器
byte[] byteContent = source.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);//创建密码器
byte[] result = cipher.doFinal(byteContent);//加密

解密

//核心代码
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(source);

加密和解密的结果都是二进制的,无法直接转化为字符串,所以还需要将二进制与十六进制互转

    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xff);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            stringBuffer.append(hex.toUpperCase());
        }
        return stringBuffer.toString();
    }

    public static byte[] parseHexStr2Byte(String hexStr){
        if (hexStr.length() < 1) {
            return null;
        }

        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }

        return result;
    }

这样就可以在初始化一个 key 后,对文本进行加密和解密

//初始化 key
SecretKeySpec key = initKey();
//加密文本并转化为 16 进制,方便保存
String eStr = parseByte2HexStr(encrypt(resource,key));
//将加密 16 进制文本转为二进制,进行解密
String dStr = decrypt(parseHexStr2Byte(estr));

参考文献

JAVA 实现 AES 加密 - CSDN 博客open in new window

源码 github 链接open in new window

文章标题:《Java 实现 AES 加密解密应用》
本文作者: JI,XIAOYONG
发布时间: 2018/01/22 23:51:19 UTC+8
更新时间: 2023/12/30 16:17:02 UTC+8
written by human, not by AI
本文地址: https://jixiaoyong.github.io/blog/posts/375fcf66.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 许可协议。转载请注明出处!
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.8