前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android加密篇 AES

Android加密篇 AES

作者头像
全栈程序员站长
发布2022-08-27 11:58:23
1.6K0
发布2022-08-27 11:58:23
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

在这里插入图片描述
在这里插入图片描述

AES加密是一种高级加密标准,是一种区块加密标准。它是一个对称密码,就是说加密和解密用相同的密钥。WPA/WPA2经常用的加密方式就是AES加密算法。

代码语言:javascript
复制
import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESUtils3 { 
   
    /* 算法/模式/填充 */
    private static final String CipherMode = "AES/ECB/PKCS5Padding";

    /* 创建密钥 */
    private static SecretKeySpec createKey(String password) { 
   
        byte[] data = null;
        if (password == null) { 
   
            password = "";
        }
        StringBuffer sb = new StringBuffer(32);
        sb.append(password);
        while (sb.length() < 32) { 
   
            sb.append("0");
        }
        if (sb.length() > 32) { 
   
            sb.setLength(32);
        }

        try { 
   
            data = sb.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) { 
   
            e.printStackTrace();
        }
        return new SecretKeySpec(data, "AES");
    }

    /* 加密字节数据 */
    public static byte[] encrypt(byte[] content, String password) { 
   
        try { 
   
            SecretKeySpec key = createKey(password);
            System.out.println(key);
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
        return null;
    }

    /*加密(结果为16进制字符串) */
    public static String encrypt(String content, String password) { 
   
        byte[] data = null;
        try { 
   
            data = content.getBytes("UTF-8");
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
        data = encrypt(data, password);
        String result = byte2hex(data);
        return result;
    }

    /*解密字节数组*/
    public static byte[] decrypt(byte[] content, String password) { 
   
        try { 
   
            SecretKeySpec key = createKey(password);
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
        return null;
    }

    /*解密16进制的字符串为字符串 */
    public static String decrypt(String content, String password) { 
   
        byte[] data = null;
        try { 
   
            data = hex2byte(content);
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
        data = decrypt(data, password);
        if (data == null) return null;
        String result = null;
        try { 
   
            result = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException e) { 
   
            e.printStackTrace();
        }
        return result;
    }

    /*字节数组转成16进制字符串 */
    public static String byte2hex(byte[] b) { 
    // 一个字节的数,
        StringBuffer sb = new StringBuffer(b.length * 2);
        String tmp = "";
        for (int n = 0; n < b.length; n++) { 
   
            // 整数转成十六进制表示
            tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (tmp.length() == 1) { 
   
                sb.append("0");
            }
            sb.append(tmp);
        }
        return sb.toString().toUpperCase(); // 转成大写
    }

    /*将hex字符串转换成字节数组 */
    private static byte[] hex2byte(String inputString) { 
   
        if (inputString == null || inputString.length() < 2) { 
   
            return new byte[0];
        }
        inputString = inputString.toLowerCase();
        int l = inputString.length() / 2;
        byte[] result = new byte[l];
        for (int i = 0; i < l; ++i) { 
   
            String tmp = inputString.substring(2 * i, 2 * i + 2);
            result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
        }
        return result;
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/145447.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月1,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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