zoukankan      html  css  js  c++  java
  • AES算法

    AES128Bit ECB加解密算法:

    public static String Encrypt(String data,String key) throws Exception{
      byte[] raw = HexUtils.hexStr2ByteArr(key);
      SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
      Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");// "算法/模式/补码方式"
      cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
      byte[] inData = HexUtils.hexStr2ByteArr(data);
      byte[] outData = cipher.doFinal(inData);
      return HexUtils.byteArr2HexStr(outData);     
    }
    
    /**
         * 16进制字符串转字节数组,单字节(双字符)转单字节
         * 
         * @param data
         * @return
         */
        public static byte[] hexStr2ByteArr(String data) {
            if (data == null || data.equals("")) {
                return null;
            }
            data = data.toUpperCase();
            int length = data.length() / 2;
            char[] hexChars = data.toCharArray();
            byte[] d = new byte[length];
            for (int i = 0; i < length; i++) {
                int pos = i * 2;
                d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }
            return d;
            // 另一种写法
            // public static byte[] hexStrToByteArr(String data) {
            // if ((data == null) || (data.length() < 2))
            // return null;
            // int len = data.length() / 2;
            // byte[] buffer = new byte[len];
            // for (int i = 0; i < len; i++) {
            // buffer[i] = (byte) Integer.parseInt(data.substring(i * 2, i * 2 + 2),
            // 16);
            // }
            // return buffer;
            // }
        }
    public static String byteArr2HexStr(byte[] data) {
            String hs = "";
            String stmp = "";
            for (int n = 0; n < data.length; n++) {
                stmp = (Integer.toHexString(data[n] & 0XFF));
                if (stmp.length() == 1)
                    hs = hs + "0" + stmp + "";
                else
                    hs = hs + stmp + "";
            }
            return hs.toUpperCase();
        }
    /**
         * AES128bit ECB解密
         * 
         * @param data
         *            16进制数据,16字节整数倍
         * @param key
         *            16进制数据,16字节
         * @return
         * @throws Exception
         */
        public static String Decrypt(String data, String key) throws Exception {
            if (data.length() % 32 != 0) {
                System.out.println("数据不是16字节整数倍");
                return null;
            }
    
            if (key.length() != 32) {
                System.out.print("Key长度不是16字节");
                return null;
            }
    
            byte[] raw = HexUtils.hexStr2ByteArr(key);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");// "算法/模式/补码方式"
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] inData = HexUtils.hexStr2ByteArr(data);
            byte[] outData = cipher.doFinal(inData);
            return HexUtils.byteArr2HexStr(outData);
        }
  • 相关阅读:
    查看本机安装了哪些.Net Framework版本的方法
    tomcat启动超时, Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds...
    设置PL/SQL Developer 字符集
    JDK安装与环境变量配置
    function 与 => 的区别
    如何使用tomcat,使用域名直接访问javaweb项目首页
    Spring整合Struts2
    Oracle to_char函数的使用方法
    PLSQL 误删表恢复操作
    Python可视化动态图表,基于Python环境,使用jupyter notebook编辑ipynb文件,基于pyecharts生成Sankey的桑基图HTML制作
  • 原文地址:https://www.cnblogs.com/wangpengpeng/p/13202757.html
Copyright © 2011-2022 走看看