网站首页|课程体系|师资力量|学员中心|就业信息|学习园地|新闻动态|中心简介|在线报名
 位置: 东方标准广州 >> 学习园地 >> 建站技术 >> JAVA教程 >> 正文
公告: 受著名动漫企业三学苑/士丹尼 著名软件企业京华网络/立信集团委托招募培训动漫软件人才70名免费试听随时体验
简单加密/解密方法包装, 含encode(),decode(),md5()[东方标准广州北京路]

简单加密/解密方法包装, 含encode(),decode(),md5()[东方标准广州北京路]

package steeven;

/*
用途: 简单加密/解密方法包装
作者: steeven@kali.com.cn
日期: 12/05/2001
感谢: http://www-900.ibm.com/developerworks/java/l-security/index.shtml

说明:
this class need jce, download here:
http://java.sun.com/security/index.html

*/

import java.security.*;
import javax.crypto.*;
public class crypt {

  private static string algorithm="des"; //定义 加密算法,可用 des,desede,blowfish

  static boolean debug = false;

  static{
    security.addprovider(new com.sun.crypto.provider.sunjce());
  }

  //生成密钥, 注意此步骤时间比较长
  public static byte[] getkey() throws exception{
    keygenerator keygen = keygenerator.getinstance(algorithm);
    secretkey deskey = keygen.generatekey();
    if (debug)
      system.out.println("生成密钥:"+byte2hex(deskey.getencoded()));
    return deskey.getencoded();
  }

  //加密
  public static byte[] encode(byte[] input,byte[] key) throws exception{
    secretkey deskey = new javax.crypto.spec.secretkeyspec(key,algorithm);
    if (debug){
      system.out.println("加密前的二进串:"+byte2hex(input));
      system.out.println("加密前的字符串:"+new string(input));
    }
    cipher c1 = cipher.getinstance(algorithm);
    c1.init(cipher.encrypt_mode,deskey);
    byte[] cipherbyte=c1.dofinal(input);
    if (debug)
      system.out.println("加密后的二进串:"+byte2hex(cipherbyte));
    return cipherbyte;
  }

  //解密
  public static byte[] decode(byte[] input,byte[] key) throws exception{
    secretkey deskey = new javax.crypto.spec.secretkeyspec(key,algorithm);
    if (debug)
      system.out.println("解密前的信息:"+byte2hex(input));
    cipher c1 = cipher.getinstance(algorithm);
    c1.init(cipher.decrypt_mode,deskey);
    byte[] clearbyte=c1.dofinal(input);
    if (debug){
      system.out.println("解密后的二进串:"+byte2hex(clearbyte));
      system.out.println("解密后的字符串:"+(new string(clearbyte)));
    }
    return clearbyte;
  }

  //md5()信息摘要, 不可逆
  public static byte[] md5(byte[] input) throws exception{
    java.security.messagedigest alg=java.security.messagedigest.getinstance("md5"); //or "sha-1"
    if (debug){
      system.out.println("摘要前的二进串:"+byte2hex(input));
      system.out.println("摘要前的字符串:"+new string(input));
    }
    alg.update(input);
    byte[] digest = alg.digest();
    if (debug)
      system.out.println("摘要后的二进串:"+byte2hex(digest));
    return digest;
  }

  //字节码转换成16进制字符串
  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;
        if (n<b.length-1)  hs=hs+":";
      }
    return hs.touppercase();
  }

  public static void main(string[] args) throws exception{
    debug = true;
//    byte[] key = getkey();
    byte[] key = "好好学习".getbytes();
    decode(encode("测试加密".getbytes(),key),key);
    md5("测试加密".getbytes());
  }
}


关于本站Aboutweb | 友情站点LinkSite | 联系方式Contact | 汇款方式Remit | 版权申明Copyright

地址:广州市北京路步行街312号青年文化宫6楼
咨询电话:020-83303626 13288633114 咨询QQ群:11123656

Copyright© 1993-2007 Gowinedu.com .All Rights Reserved
点击这里给我发消息许老师
点击这里给我发消息李老师
点击这里给我发消息贾老师
点击此处关闭洽谈通漂浮窗口!
在线客服