关于作者

姓名:

性别:男

出生日期:--

地区:

联系电话:

QQ:--

婚否:保密
用户名:maafei
笔名:maafei
地区:

日历  

快速登录

+ 用户名:
+ 密 码:

快速通道

最新文章

在线留言



最新评论

访问统计:
文章个数:1
评论个数:0
留言条数:0




Powered by BlogDriver 2.1

我的杂货铺

 

欢迎访问我的杂货铺

文章

测试

测试一下,看看代码什么样子。

package com.laoer.comm.util;

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

/**
 * <p>Title: 天乙软件工作室公共包</p>
 * <p>Description: 天乙软件工作室公共包</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 天乙软件工作室[LAOER.COM/TIANYISOFT.NET]</p>
 * @author 龚天乙(Laoer)
 * @version 1.0
 */

public class DES {

  public static int _DES = 1;
  public static int _DESede = 2;
  public static int _Blowfish = 3;

  private Cipher p_Cipher;
  private SecretKey p_Key;
  private String p_Algorithm;

  private void selectAlgorithm(int al) {
    switch (al) {
      default:
      case 1:
        this.p_Algorithm = "DES";
        break;
      case 2:
        this.p_Algorithm = "DESede";
        break;
      case 3:
        this.p_Algorithm = "Blowfish";
        break;
    }
  }

  public DES(int algorithm) throws Exception {
    this.selectAlgorithm(algorithm);
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
    this.p_Cipher = Cipher.getInstance(this.p_Algorithm);
  }

  public byte[] getKey() {
    return this.checkKey().getEncoded();
  }

  private SecretKey checkKey() {
    try {
      if (this.p_Key == null) {
        KeyGenerator keygen = KeyGenerator.getInstance(this.p_Algorithm);
        this.p_Key = keygen.generateKey();
      }
    }
    catch (Exception nsae) {}
    return this.p_Key;
  }

  public void setKey(byte[] enckey) {
    this.p_Key = new SecretKeySpec(enckey, this.p_Algorithm);
  }

  public byte[] encode(byte[] data) throws Exception {
    //this.p_Cipher.init(Cipher.ENCRYPT_MODE, this.checkKey());
    this.p_Cipher.init(Cipher.ENCRYPT_MODE, this.p_Key);
    return this.p_Cipher.doFinal(data);
  }

  public byte[] decode(byte[] encdata, byte[] enckey) throws Exception {
    this.setKey(enckey);
    this.p_Cipher.init(Cipher.DECRYPT_MODE, this.p_Key);
    return this.p_Cipher.doFinal(encdata);
  }

  public String byte2hex(byte[] b) {
    String hs = "";
    String stmp = "";
    for (int i = 0; i < b.length; i++) {
      stmp = Integer.toHexString(b[i] & 0xFF);
      if (stmp.length() == 1) {
        hs += "0" + stmp;
      }
      else {
        hs += stmp;
      }
    }
    //return hs;
    return hs.toUpperCase();
  }


  public byte[] hex2byte(String hex) throws IllegalArgumentException {
    if (hex.length() % 2 != 0) {
      throw new IllegalArgumentException();
    }
    char[] arr = hex.toCharArray();
    byte[] b = new byte[hex.length() / 2];
    for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
      String swap = "" + arr[i++] + arr[i];
      int byteint = Integer.parseInt(swap, 16) & 0xFF;
      b[j] = new Integer(byteint).byteValue();
    }
    return b;
  }

  public static void main(String[] args) throws Exception {
    long start = System.currentTimeMillis();
    String info = "maafei";
    System.out.println("region string:" + info);
    byte[] key; //密钥文件(byte)
    DES des = new DES(DES._DESede); // 声明DES
    key = des.getKey(); //获取随机生成的密钥
    System.out.println("encrypted key(byte):" + new String(key));
    String hexkey = des.byte2hex(key); //生成十六进制密钥
    System.out.println("encrypted key(hex):" + hexkey);
    byte[] enc = des.encode(info.getBytes()); //生成加密文件(byte)
    System.out.println("encrypted string(byte):" + new String(enc));
    System.out.println("encrypted string length:" + enc.length);

    String hexenc = des.byte2hex(enc); //生成十六进制加密文件

    System.out.println("encrypted string(hex):" + hexenc);

    byte[] dec = des.decode(enc, des.hex2byte(hexkey)); //解密文件,其中转换十六进制密钥为byte
    System.out.println("decrypted string:" + new String(dec)); //生成解密文件字符串, 与info相同
    long end = System.currentTimeMillis();
    System.out.println(end - start);
  }

}

- 作者: maafei 2006年01月6日, 星期五 14:18  回复(0) |  引用(0) 加入博采