DES 加密

来源:互联网 发布:淘宝被同行恶意拍下 编辑:程序博客网 时间:2024/05/22 00:27
  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. * @param src 数据源  
  20. * @param key 密钥,长度必须是8的倍数  
  21. * @return  返回加密后的数据  
  22. * @throws Exception  
  23. */   
  24.    
  25. public static byte[] encrypt(byte[] src, byte[] key)throws Exception {   
  26.    
  27.        //DES算法要求有一个可信任的随机数源   
  28.    
  29.        SecureRandom sr = new SecureRandom();   
  30.    
  31.        // 从原始密匙数据创建DESKeySpec对象    
  32.    
  33.        DESKeySpec dks = new DESKeySpec(key);   
  34.    
  35.        // 创建一个密匙工厂,然后用它把DESKeySpec转换成   
  36.    
  37.        // 一个SecretKey对象   
  38.    
  39.        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);   
  40.    
  41.        SecretKey securekey = keyFactory.generateSecret(dks);   
  42.    
  43.        // Cipher对象实际完成加密操作   
  44.    
  45.        Cipher cipher = Cipher.getInstance(DES);   
  46.    
  47.        // 用密匙初始化Cipher对象   
  48.    
  49.        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);   
  50.    
  51.        // 现在,获取数据并加密   
  52.    
  53.        // 正式执行加密操作   
  54.    
  55.        return cipher.doFinal(src);   
  56.    
  57.     }   
  58.    
  59.    
  60.    
  61.     /**  
  62.     * 解密  
  63.     * @param src 数据源  
  64.     * @param key 密钥,长度必须是8的倍数  
  65.     * @return   返回解密后的原始数据  
  66.     * @throws Exception  
  67.     */   
  68.    
  69.     public static byte[] decrypt(byte[] src, byte[] key)throws Exception {   
  70.    
  71.        // DES算法要求有一个可信任的随机数源   
  72.    
  73.        SecureRandom sr = new SecureRandom();   
  74.    
  75.        // 从原始密匙数据创建一个DESKeySpec对象   
  76.    
  77.        DESKeySpec dks = new DESKeySpec(key);   
  78.    
  79.        // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成   
  80.    
  81.        // 一个SecretKey对象   
  82.    
  83.        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);   
  84.    
  85.        SecretKey securekey = keyFactory.generateSecret(dks);   
  86.    
  87.        // Cipher对象实际完成解密操作   
  88.    
  89.        Cipher cipher = Cipher.getInstance(DES);   
  90.    
  91.        // 用密匙初始化Cipher对象   
  92.    
  93.        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);   
  94.    
  95.        // 现在,获取数据并解密   
  96.    
  97.        // 正式执行解密操作   
  98.    
  99.        return cipher.doFinal(src);   
  100.    
  101.     }   
  102.    
  103.  /**  
  104.   * 密码解密  
  105.   * @param data  
  106.   * @return  
  107.   * @throws Exception  
  108.   */   
  109.    
  110.  public final static String decrypt(String data){   
  111.    
  112.     try {   
  113.    
  114.      return new String(decrypt(hex2byte(data.getBytes()),   
  115.    
  116.         PASSWORD_CRYPT_KEY.getBytes()));   
  117.    
  118.    }catch(Exception e) {   
  119.    
  120.    }   
  121.    
  122.    return null;   
  123.    
  124.  }   
  125.    
  126.  /**  
  127.   * 密码加密  
  128.   * @param password  
  129.   * @return  
  130.   * @throws Exception  
  131.   */   
  132.    
  133.  public final static String encrypt(String password){   
  134.    
  135.    try {   
  136.    
  137.      return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes()));   
  138.    }catch(Exception e) {   
  139.    
  140.    }   
  141.    
  142.    return null;   
  143.    
  144.  }   
  145.    
  146. /**  
  147. * 二行制转字符串  
  148. * @param b  
  149. * @return  
  150. */   
  151.    
  152.  public static String byte2hex(byte[] b) {   
  153.    
  154.        String hs = "";   
  155.    
  156.        String stmp = "";   
  157.    
  158.        for (int n = 0; n < b.length; n++) {   
  159.    
  160.            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));   
  161.    
  162.            if (stmp.length() == 1)   
  163.    
  164.                hs = hs + "0" + stmp;   
  165.    
  166.            else   
  167.    
  168.                hs = hs + stmp;   
  169.    
  170.        }   
  171.    
  172.        return hs.toUpperCase();   
  173.    
  174.   }   
  175.    
  176.     
  177.    
  178.  public static byte[] hex2byte(byte[] b) {   
  179.    
  180.    if((b.length%2)!=0)   
  181.    
  182.       throw new IllegalArgumentException("长度不是偶数");   
  183.    
  184.        byte[] b2 = new byte[b.length/2];   
  185.    
  186.        for (int n = 0; n < b.length; n+=2) {   
  187.    
  188.          String item = new String(b,n,2);   
  189.    
  190.          b2[n/2] = (byte)Integer.parseInt(item,16);   
  191.    
  192.        }   
  193.    
  194.    return b2;   
  195.  }   
原创粉丝点击