DES 加密

java 代码
 
  1. package encrypt;  
  2.   
  3. import java.security.*;  
  4. import javax.crypto.Cipher;  
  5. import javax.crypto.SecretKey;  
  6. import javax.crypto.SecretKeyFactory;  
  7. import javax.crypto.spec.DESKeySpec;  
  8.   
  9.   
  10. public class DesEncrypt {  
  11.   
  12.     private static final String PASSWORD_CRYPT_KEY = "xredleaf";  
  13.     private final static String DES = "DES";  
  14.   
  15.   
  16.    
  17. /** 
  18.  
  19. * 加密 
  20.  
  21. * @param src 数据源 
  22.  
  23. * @param key 密钥,长度必须是8的倍数 
  24.  
  25. * @return  返回加密后的数据 
  26.  
  27. * @throws Exception 
  28.  
  29. */  
  30.   
  31. public static byte[] encrypt(byte[] src, byte[] key)throws Exception {  
  32.   
  33.        //DES算法要求有一个可信任的随机数源  
  34.   
  35.        SecureRandom sr = new SecureRandom();  
  36.   
  37.        // 从原始密匙数据创建DESKeySpec对象   
  38.   
  39.        DESKeySpec dks = new DESKeySpec(key);  
  40.   
  41.        // 创建一个密匙工厂,然后用它把DESKeySpec转换成  
  42.   
  43.        // 一个SecretKey对象  
  44.   
  45.        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);  
  46.   
  47.        SecretKey securekey = keyFactory.generateSecret(dks);  
  48.   
  49.        // Cipher对象实际完成加密操作  
  50.   
  51.        Cipher cipher = Cipher.getInstance(DES);  
  52.   
  53.        // 用密匙初始化Cipher对象  
  54.   
  55.        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);  
  56.   
  57.        // 现在,获取数据并加密  
  58.   
  59.        // 正式执行加密操作  
  60.   
  61.        return cipher.doFinal(src);  
  62.   
  63.     }  
  64.   
  65.   
  66.   
  67.     /** 
  68.  
  69.     * 解密 
  70.  
  71.     * @param src 数据源 
  72.  
  73.     * @param key 密钥,长度必须是8的倍数 
  74.  
  75.     * @return   返回解密后的原始数据 
  76.  
  77.     * @throws Exception 
  78.  
  79.     */  
  80.   
  81.     public static byte[] decrypt(byte[] src, byte[] key)throws Exception {  
  82.   
  83.        // DES算法要求有一个可信任的随机数源  
  84.   
  85.        SecureRandom sr = new SecureRandom();  
  86.   
  87.        // 从原始密匙数据创建一个DESKeySpec对象  
  88.   
  89.        DESKeySpec dks = new DESKeySpec(key);  
  90.   
  91.        // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成  
  92.   
  93.        // 一个SecretKey对象  
  94.   
  95.        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);  
  96.   
  97.        SecretKey securekey = keyFactory.generateSecret(dks);  
  98.   
  99.        // Cipher对象实际完成解密操作  
  100.   
  101.        Cipher cipher = Cipher.getInstance(DES);  
  102.   
  103.        // 用密匙初始化Cipher对象  
  104.   
  105.        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);  
  106.   
  107.        // 现在,获取数据并解密  
  108.   
  109.        // 正式执行解密操作  
  110.   
  111.        return cipher.doFinal(src);  
  112.   
  113.     }  
  114.   
  115.  /** 
  116.  
  117.   * 密码解密 
  118.  
  119.   * @param data 
  120.  
  121.   * @return 
  122.  
  123.   * @throws Exception 
  124.  
  125.   */  
  126.   
  127.  public final static String decrypt(String data){  
  128.   
  129.     try {  
  130.   
  131.      return new String(decrypt(hex2byte(data.getBytes()),  
  132.   
  133.         PASSWORD_CRYPT_KEY.getBytes()));  
  134.   
  135.    }catch(Exception e) {  
  136.   
  137.    }  
  138.   
  139.    return null;  
  140.   
  141.  }  
  142.   
  143.  /** 
  144.  
  145.   * 密码加密 
  146.  
  147.   * @param password 
  148.  
  149.   * @return 
  150.  
  151.   * @throws Exception 
  152.  
  153.   */  
  154.   
  155.  public final static String encrypt(String password){  
  156.   
  157.    try {  
  158.   
  159.      return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes()));  
  160.    }catch(Exception e) {  
  161.   
  162.    }  
  163.   
  164.    return null;  
  165.   
  166.  }  
  167.   
  168. /** 
  169.  
  170. * 二行制转字符串 
  171.  
  172. * @param b 
  173.  
  174. * @return 
  175.  
  176. */  
  177.   
  178.  public static String byte2hex(byte[] b) {  
  179.   
  180.        String hs = "";  
  181.   
  182.        String stmp = "";  
  183.   
  184.        for (int n = 0; n < b.length; n++) {  
  185.   
  186.            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));  
  187.   
  188.            if (stmp.length() == 1)  
  189.   
  190.                hs = hs + "0" + stmp;  
  191.   
  192.            else  
  193.   
  194.                hs = hs + stmp;  
  195.   
  196.        }  
  197.   
  198.        return hs.toUpperCase();  
  199.   
  200.   }  
  201.   
  202.    
  203.   
  204.  public static byte[] hex2byte(byte[] b) {  
  205.   
  206.    if((b.length%2)!=0)  
  207.   
  208.       throw new IllegalArgumentException("长度不是偶数");  
  209.   
  210.        byte[] b2 = new byte[b.length/2];  
  211.   
  212.        for (int n = 0; n < b.length; n+=2) {  
  213.   
  214.          String item = new String(b,n,2);  
  215.   
  216.          b2[n/2] = (byte)Integer.parseInt(item,16);  
  217.   
  218.        }  
  219.   
  220.    return b2;  
  221.  }  
  222. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值