【數據加密】簡單加密/解密方法包裝

  1. package steeven;
  2. /*
  3. 用途: 簡單加密/解密方法包裝
  4. 作者: [email protected]
  5. 日期: 12/05/2001
  6. 感謝: http://www-900.ibm.com/developerWorks/java/l-security/index.shtml
  7. 說明:
  8. This Class need JCE, download here:
  9. http://java.sun.com/security/index.html
  10. */
  11. import java.security.*;
  12. import javax.crypto.*;
  13. public class Crypt {
  14.   private static String Algorithm="DES"//定義 加密算法,可用 DES,DESede,Blowfish
  15.   static boolean debug = false;
  16.   static{
  17.     Security.addProvider(new com.sun.crypto.provider.SunJCE());
  18.   }
  19.   //生成密鑰, 注意此步驟時間比較長
  20.   public static byte[] getKey() throws Exception{
  21.     KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
  22.     SecretKey deskey = keygen.generateKey();
  23.     if (debug)
  24.       System.out.println("生成密鑰:"+byte2hex(deskey.getEncoded()));
  25.     return deskey.getEncoded();
  26.   }
  27.   //加密
  28.   public static byte[] encode(byte[] input,byte[] key) throws Exception{
  29.     SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm);
  30.     if (debug){
  31.       System.out.println("加密前的二進串:"+byte2hex(input));
  32.       System.out.println("加密前的字符串:"+new String(input));
  33.     }
  34.     Cipher c1 = Cipher.getInstance(Algorithm);
  35.     c1.init(Cipher.ENCRYPT_MODE,deskey);
  36.     byte[] cipherByte=c1.doFinal(input);
  37.     if (debug)
  38.       System.out.println("加密後的二進串:"+byte2hex(cipherByte));
  39.     return cipherByte;
  40.   }
  41.   //解密
  42.   public static byte[] decode(byte[] input,byte[] key) throws Exception{
  43.     SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm);
  44.     if (debug)
  45.       System.out.println("解密前的信息:"+byte2hex(input));
  46.     Cipher c1 = Cipher.getInstance(Algorithm);
  47.     c1.init(Cipher.DECRYPT_MODE,deskey);
  48.     byte[] clearByte=c1.doFinal(input);
  49.     if (debug){
  50.       System.out.println("解密後的二進串:"+byte2hex(clearByte));
  51.       System.out.println("解密後的字符串:"+(new String(clearByte)));
  52.     }
  53.     return clearByte;
  54.   }
  55.   //md5()信息摘要, 不可逆
  56.   public static byte[] md5(byte[] input) throws Exception{
  57.     java.security.MessageDigest alg=java.security.MessageDigest.getInstance("MD5"); //or "SHA-1"
  58.     if (debug){
  59.       System.out.println("摘要前的二進串:"+byte2hex(input));
  60.       System.out.println("摘要前的字符串:"+new String(input));
  61.     }
  62.     alg.update(input);
  63.     byte[] digest = alg.digest();
  64.     if (debug)
  65.       System.out.println("摘要後的二進串:"+byte2hex(digest));
  66.     return digest;
  67.   }
  68.   //字節碼轉換成16進制字符串
  69.   public static String byte2hex(byte[] b) {
  70.     String hs="";
  71.     String stmp="";
  72.     for (int n=0;n<b.length;n++){
  73.       stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
  74.       if (stmp.length()==1)
  75.         hs=hs+"0"+stmp;
  76.       else hs=hs+stmp;
  77.         if (n<b.length-1)  hs=hs+":";
  78.       }
  79.     return hs.toUpperCase();
  80.   }
  81.   public static void main(String[] args) throws Exception{
  82.     debug = true;
  83. //    byte[] key = getKey();
  84.     byte[] key = "好好學習".getBytes();
  85.     decode(encode("測試加密".getBytes(),key),key);
  86.     md5("測試加密".getBytes());
  87.   }
  88. }
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章