加密算法

Code:
  1. package common;   
  2.   
  3. /**  
  4.  * @see 加密解密公共类  
  5.  * @author WangYuxin  
  6.  * @since 1.0  
  7.  * @version 2008-12-12  
  8.  */  
  9. import java.security.*;   
  10.   
  11. import javax.crypto.*;   
  12.   
  13. public class Eryptogram {   
  14.     private  static  String  Algorithm ="DES";   
  15.     static  boolean  debug  = false ;   
  16.   
  17.     /**  
  18.      * 构造函数注解.  
  19.      */  
  20.     public  Eryptogram(){}   
  21.   
  22.        
  23.     /**  
  24.      * @see 生成密钥  
  25.      */  
  26.     public static byte[] getSecretKey() throws NoSuchAlgorithmException   
  27.     {   
  28.         KeyGenerator keygen=KeyGenerator.getInstance(Algorithm);   
  29.         SecretKey deskey=keygen.generateKey();   
  30.         if(debug)   
  31.             System.out.println("生成密钥:"+byte2hex (deskey.getEncoded ()));   
  32.            
  33.         return  deskey.getEncoded();   
  34.            
  35.     }   
  36.        
  37.   
  38.     /**  
  39.      * @see 将指定的数据根据提供的密钥进行加密  
  40.      * @param input 需要加密的数据  
  41.      * @param key 密钥  
  42.      * @return byte[] 加密后的数据  
  43.      * @throws NoSuchPaddingException   
  44.      * @throws NoSuchAlgorithmException   
  45.      * @throws InvalidKeyException   
  46.      * @throws BadPaddingException   
  47.      * @throws IllegalBlockSizeException   
  48.      */  
  49.     public static byte[] encryptData(byte[] input,byte[] key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException   
  50.     {   
  51.         SecretKey deskey=new javax.crypto.spec.SecretKeySpec(key,Algorithm);   
  52.         if(debug)   
  53.         {   
  54.             System.out.println ("加密前的二进串:"+byte2hex(input));   
  55.             System.out.println ("加密前的字符串:"+new String(input));   
  56.         }   
  57.         Cipher c1=Cipher.getInstance(Algorithm);   
  58.         c1.init(Cipher.ENCRYPT_MODE, deskey);   
  59.         byte[] cipherByte=c1.doFinal(input);   
  60.         if(debug)   
  61.         {   
  62.             System.out.println ("加密后的二进串:"+byte2hex (cipherByte ));   
  63.         }   
  64.         return cipherByte;   
  65.            
  66.            
  67.     }   
  68.   
  69.        
  70.     /**  
  71.      * @see 将给定的已加密的数据通过指定的密钥进行解密  
  72.      * @param input 待解密的数据  
  73.      * @param key 密钥  
  74.      * @return byte[] 解密后的数据  
  75.      * @throws NoSuchPaddingException   
  76.      * @throws NoSuchAlgorithmException   
  77.      * @throws InvalidKeyException   
  78.      * @throws BadPaddingException   
  79.      * @throws IllegalBlockSizeException   
  80.      */  
  81.     public static byte[] decryptData(byte[] input,byte[] key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException   
  82.     {   
  83.         SecretKey deskey=new javax.crypto.spec.SecretKeySpec(key,Algorithm);   
  84.         if(debug)   
  85.         {   
  86.             System.out.println("解密前的信息:"+byte2hex(input));   
  87.         }   
  88.         Cipher c1=Cipher.getInstance(Algorithm);   
  89.         c1.init(Cipher.DECRYPT_MODE, deskey);   
  90.         byte[] clearByte=c1.doFinal(input);   
  91.         if(debug)   
  92.         {   
  93.             System.out.println ("解密后的二进串:"+byte2hex(clearByte ));   
  94.             System.out.println ("解密后的字符串:"+(new  String(clearByte )));   
  95.         }   
  96.         return clearByte;   
  97.     }   
  98.        
  99.     /**  
  100.      * @see 字节码转换成16进制字符串  
  101.      * @param b 输入要转换的字节码  
  102.      * @return String 返回转换后的16进制字符串  
  103.      */  
  104.     public static String byte2hex(byte[] b)   
  105.     {   
  106.         String hs="";   
  107.         String stmp="";   
  108.         for(int n=0;n<b.length;n++)   
  109.         {   
  110.             stmp=(java.lang.Integer.toHexString(b[n]0XFF));   
  111.             if(stmp.length()==1)   
  112.             {   
  113.                 hs=hs+":";   
  114.             }   
  115.   
  116.         }   
  117.         return hs.toUpperCase();   
  118.     }   
  119.            
  120.     public static void main(String[] args)   
  121.     {   
  122.         try{   
  123.             debug=false;   
  124.             String aa="1234567";   
  125.                
  126.             Eryptogram etg=new Eryptogram();   
  127.                
  128.             byte[] key=Eryptogram.getSecretKey();   
  129.             System.out.println("key="+key);   
  130.                
  131.             byte[] data=aa.getBytes();   
  132.             System.out.println("data="+data);   
  133.                
  134.             byte[] en=Eryptogram.encryptData(data, key);   
  135.             System.out.println("encryptData="+new String(en));   
  136.                
  137.             byte[] de=Eryptogram.decryptData(en, key);   
  138.             System.out.println("decryptData = "+new String(de));   
  139.         }catch(Exception e)   
  140.         {   
  141.             e.printStackTrace();   
  142.         }   
  143.     }   
  144. }   


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值