zoukankan      html  css  js  c++  java
  • AES加密

    import javax.crypto.*;
    import javax.crypto.spec.*;
    public class AES {
    public static void main(String[] args) throws Exception {
    /*
    加密用的Key
    可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定
    */
    String cKey = "1234567890abcDEF";
    //需要加密的字串
    String cSrc = "我的MSN:miaozu@msn.com,QQ:68524621"
    ;
    //加密
    long lStart = System.currentTimeMillis();
    String enString = AES.Encrypt(cSrc, cKey);
    System.out.println("加密后的字串是:" + enString);
    long lUseTime = System.currentTimeMillis() - lStart;
    System.out.println("加密耗时:" + lUseTime + "毫秒");
    //解密
    lStart = System.currentTimeMillis();
    String DeString = AES.Decrypt(enString, cKey);
    System.out.println("解密后的字串是:" + DeString);
    lUseTime = System.currentTimeMillis() - lStart;
    System.out.println("解密耗时:" + lUseTime + "毫秒");
    }
    public static String Decrypt(String sSrc, String sKey) throws Exception {
    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("ASCII");
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] encrypted1 = hex2byte(sSrc);
    try {
    byte[] original = cipher.doFinal(encrypted1);
    String originalString = new String(original);
    return originalString;
    } catch (Exception e) {
    System.out.println(e.toString());
    return null;
    }
    } catch (Exception ex) {
    System.out.println(ex.toString());
    return null;
    }
    }
    //判断Key是否正确
    public static String Encrypt(String sSrc, String sKey) throws Exception {
    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("ASCII");
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(sSrc.getBytes());
    return byte2hex(encrypted).toLowerCase();
    }
    public static byte[] hex2byte(String strhex) {
    if (strhex == null) {
    return null;
    }
    int l = strhex.length();
    if (l % 2 == 1) {
    return null;
    }
    byte[] b = new byte[l / 2];
    for (int i = 0; i != l / 2; i++) {
    b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 16);
    }
    return b;
    }
    public static String byte2hex(byte[] b) {
    String hs = "";
    String stmp = "";
    for (int n = 0; n < b.length; n++) {
    stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
    if (stmp.length() == 1) {
    hs = hs + "0" + stmp;
    } else {
    hs = hs + stmp;
    }
    }
    return hs.toUpperCase();
    }
    }
    
    
    以上代码执行结果
    加密后的字串是:e965a929ae598b701bc2bc4714164c27524aab4769f269c434c55b61e398600175a38e0a4794cdef9c0463a182d2cde3
    加密耗时:672毫秒
    解密后的字串是:我的MSN:miaozu@msn.com,QQ:68524621
    解密耗时:0毫秒
  • 相关阅读:
    在MVC3 中给HtmlHelper 添加扩展(Extension)来消除魔鬼代码
    NHibernate中使用IQueryOver时,如何添加或(OR)条件
    用@Html.EditorFor在MVC3中封装带表单(Form)提交的分页控件(通用代码)
    .NET MVC3使用CheckBox List(复选框列表)的简单方法
    Castle Windsor的MVC3的例子在最新版本(3.0Beta)上编译不过去的解决办法
    初级编程:编程巧妙注释【附图讲解】
    布同:如何循序渐进学习Python语言
    <转>:写给初学者的话《学习程序之路》
    初级编程:一层循环如何依次遍历二维数组【附代码】
    赛班S60的Python平台的源代码
  • 原文地址:https://www.cnblogs.com/xyzq/p/7145000.html
Copyright © 2011-2022 走看看