收藏 分销(赏)

AES加密JAVA代码实现.doc

上传人:仙人****88 文档编号:8661114 上传时间:2025-02-24 格式:DOC 页数:4 大小:14.29KB
下载 相关 举报
AES加密JAVA代码实现.doc_第1页
第1页 / 共4页
AES加密JAVA代码实现.doc_第2页
第2页 / 共4页
点击查看更多>>
资源描述
AES加密JAVA代码实现 2007年04月06日 星期五 15:11 package com.forms.struts.form; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class AES {      /**       * Turns array of bytes into string       *       * @param buf       *             Array of bytes to convert to hex string       * @return Generated hex string       */ private final static String algorithm = "AES";         private static String asHex(byte buf[])      {          StringBuffer strbuf = new StringBuffer(buf.length * 2);          int i;                   for (i = 0; i < buf.length; i++)          {              if (((int) buf[i] & 0xff) < 0x10) strbuf.append("0");              strbuf.append(Long.toString((int) buf[i] & 0xff, 16));          }          return strbuf.toString();      }           private static byte[] asBin(String src){       if(src.length()<1) return null;       byte[] encrypted = new byte[src.length()/2];       for(int i=0;i<src.length()/2;i++){        int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1 ), 16);        int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);               encrypted[i] = (byte)(high * 16 + low);       }       return encrypted;      }           public static String getRawKey() {             try{           // Get the KeyGenerator           KeyGenerator kgen = KeyGenerator.getInstance(algorithm);           kgen.init(128); // 192 and 256 bits may not be available           // Generate the secret key specs.           SecretKey skey = kgen.generateKey();           byte[] raw = skey.getEncoded();           return asHex(raw);       }catch (Exception e) {           //App.log.info("AES", "获取密钥出错," + e.getMessage());           return "";    }              }           public static String getEncrypt(String message, String rawKey){       byte[] key = asBin(rawKey);          // Instantiate the cipher          try{              SecretKeySpec skeySpec = new SecretKeySpec(key, algorithm);                     Cipher cipher = Cipher.getInstance(algorithm);           cipher.init(Cipher.ENCRYPT_MODE, skeySpec);           byte[] encrypted = cipher.doFinal(message.getBytes());                     return asHex(encrypted);          }catch (Exception e) {           //App.log.info("AES", "获取加密串出错," + e.getMessage());           return "";    }              }           public static String getDecrypt(String encrypted, String rawKey){       byte[] tmp = asBin(encrypted);       byte[] key = asBin(rawKey);             try{        SecretKeySpec skeySpec = new SecretKeySpec(key, algorithm);                  Cipher cipher = Cipher.getInstance(algorithm);           cipher.init(Cipher.DECRYPT_MODE, skeySpec);           byte[] decrypted = cipher.doFinal(tmp);                     return new String(decrypted);       }catch (Exception e) {           //App.log.info("AES", "获取解密串出错," + e.getMessage());           return "";    }            }                public static void main(String[] args) throws Exception      {          String message = "带镜粉盒电费活动家jhdjf8748";                   String rawKey = getRawKey();   //得到钥匙          System.out.println("Key = " + rawKey);                   String encrypted = getEncrypt(message,rawKey);          System.out.println("org text = " + message);//原数据          System.out.println("encrypted = " + encrypted);//加密以后                   String decrypted = getDecrypt(encrypted,rawKey);//解密串                   System.out.println("decrypted = " + decrypted);                                       } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服