zoukankan      html  css  js  c++  java
  • java实现DES加密与解密,md5加密

    很多时候要对秘要进行持久化加密,此时的加密采用md5。采用对称加密的时候就采用DES方法了

      1 import java.io.IOException;
      2 import java.security.MessageDigest;
      3 import java.security.SecureRandom;
      4 
      5 import javax.crypto.Cipher;
      6 import javax.crypto.SecretKey;
      7 import javax.crypto.SecretKeyFactory;
      8 import javax.crypto.spec.DESKeySpec;
      9 
     10 import sun.misc.BASE64Decoder;
     11 import sun.misc.BASE64Encoder;
     12 
     13 /**
     14  * 密匙工具类(包含des加密与md5加密)
     15  * @author mingge
     16  *
     17  */
     18 public class KeysUtil {
     19 
     20         private final static String DES = "DES";
     21         
     22         private final static String MD5 = "MD5";
     23         
     24         private final static String KEY="opeddsaead323353484591dadbc382a18340bf83414536";
     25      
     26         /**
     27          * MD5加密算法
     28          * @param data
     29          * @return
     30          */
     31         public static String md5Encrypt(String data) {
     32             String resultString = null;
     33             try {
     34                 resultString = new String(data);
     35                 MessageDigest md = MessageDigest.getInstance(MD5);
     36                 resultString =byte2hexString(md.digest(resultString.getBytes()));
     37             } catch (Exception ex) {
     38             }
     39             return resultString;
     40         }
     41         
     42 
     43         private  static String byte2hexString(byte[] bytes) {
     44             StringBuffer bf = new StringBuffer(bytes.length * 2);
     45             for (int i = 0; i < bytes.length; i++) {
     46                 if ((bytes[i] & 0xff) < 0x10) {
     47                     bf.append("T0");
     48                 }
     49                 bf.append(Long.toString(bytes[i] & 0xff, 16));
     50             }
     51             return bf.toString();
     52         }
     53         
     54         /**
     55          * Description 根据键值进行加密
     56          * @param data 
     57          * @param key  加密键byte数组
     58          * @return
     59          * @throws Exception
     60          */
     61         public static String desEncrypt(String data, String key) throws Exception {
     62             if (key==null) {
     63                 key=KEY;
     64             }
     65             byte[] bt = encrypt(data.getBytes(), key.getBytes());
     66             String strs = new BASE64Encoder().encode(bt);
     67             return strs;
     68         }
     69      
     70         /**
     71          * Description 根据键值进行解密
     72          * @param data
     73          * @param key  加密键byte数组
     74          * @return
     75          * @throws IOException
     76          * @throws Exception
     77          */
     78         public static String desDecrypt(String data, String key) throws IOException,
     79                 Exception {
     80             if (data == null){
     81                 return null;
     82             }
     83             if (key==null) {
     84                 key=KEY;
     85             }
     86             BASE64Decoder decoder = new BASE64Decoder();
     87             byte[] buf = decoder.decodeBuffer(data);
     88             byte[] bt = decrypt(buf,key.getBytes());
     89             return new String(bt);
     90         }
     91      
     92         /**
     93          * Description 根据键值进行加密
     94          * @param data
     95          * @param key  加密键byte数组
     96          * @return
     97          * @throws Exception
     98          */
     99         private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
    100             // 生成一个可信任的随机数源
    101             SecureRandom sr = new SecureRandom();
    102             // 从原始密钥数据创建DESKeySpec对象
    103             DESKeySpec dks = new DESKeySpec(key);
    104             // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
    105             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    106             SecretKey securekey = keyFactory.generateSecret(dks);
    107             // Cipher对象实际完成加密操作
    108             Cipher cipher = Cipher.getInstance(DES);
    109             // 用密钥初始化Cipher对象
    110             cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
    111             return cipher.doFinal(data);
    112         }
    113          
    114          
    115         /**
    116          * Description 根据键值进行解密
    117          * @param data
    118          * @param key  加密键byte数组
    119          * @return
    120          * @throws Exception
    121          */
    122         private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    123             // 生成一个可信任的随机数源
    124             SecureRandom sr = new SecureRandom();
    125             // 从原始密钥数据创建DESKeySpec对象
    126             DESKeySpec dks = new DESKeySpec(key);
    127             // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
    128             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    129             SecretKey securekey = keyFactory.generateSecret(dks);
    130             // Cipher对象实际完成解密操作
    131             Cipher cipher = Cipher.getInstance(DES);
    132             // 用密钥初始化Cipher对象
    133             cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
    134             return cipher.doFinal(data);
    135         }
    136 }

    看到最后我也醉了。。。

  • 相关阅读:
    Laravel5.0学习--03 Artisan命令
    yar框架使用笔记
    MySQL外键之级联
    笛卡尔积
    PHP钩子机制
    CentOS安装PHP7+Nginx+MySQL
    Linux压缩命令
    Git命令汇总
    使用List把一个长字符串分解成若干个短字符串
    Adb connection Error:远程主机强迫关闭了一个现有的连接
  • 原文地址:https://www.cnblogs.com/huzi007/p/4288267.html
Copyright © 2011-2022 走看看