java 加密解密

package com.zwl.util;
import java.security.MessageDigest;
public class MD5Util {
    /***
     * MD5加码 生成32位md5码
     */
    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }


    /**
     * 加密解密算法 执行一次加密,两次解密
     */
    public static String convertMD5(String inStr) {
        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;
    }


    // 测试主函数
    public static void main(String args[]) {
        String s = new String("http://ipx1000.com&18729232312");
        System.out.println("原始:" + s);
        System.out.println("MD5后:" + string2MD5(s));
        System.out.println("加密的:" + convertMD5(s));
        System.out.println("解密的:" + convertMD5(convertMD5(s)));
    }

}


des、base64加密:

  1. import java.io.IOException;  
  2. import java.security.SecureRandom;  
  3.    
  4. import javax.crypto.Cipher;  
  5. import javax.crypto.SecretKey;  
  6. import javax.crypto.SecretKeyFactory;  
  7. import javax.crypto.spec.DESKeySpec;  
  8.   
  9. import Decoder.BASE64Decoder;  
  10. import Decoder.BASE64Encoder;  
  11.    
  12.    
  13. public class Des {  
  14.    
  15.     private final static String DES = "DES";  
  16.        
  17.     /** 
  18.      * Description 根据键值进行加密 
  19.      * @param data  
  20.      * @param key  加密键byte数组 
  21.      * @return 
  22.      * @throws Exception 
  23.      */  
  24.     public static String encrypt(String data, String key) throws Exception {  
  25.         byte[] bt = encrypt(data.getBytes(), key.getBytes());  
  26.         String strs = new BASE64Encoder().encode(bt);  
  27.         return strs;  
  28.     }  
  29.    
  30.     /** 
  31.      * Description 根据键值进行解密 
  32.      * @param data 
  33.      * @param key  加密键byte数组 
  34.      * @return 
  35.      * @throws IOException 
  36.      * @throws Exception 
  37.      */  
  38.     public static String decrypt(String data, String key) throws IOException,  
  39.             Exception {  
  40.         if (data == null)  
  41.             return null;  
  42.         BASE64Decoder decoder = new BASE64Decoder();  
  43.         byte[] buf = decoder.decodeBuffer(data);  
  44.         byte[] bt = decrypt(buf,key.getBytes());  
  45.         return new String(bt);  
  46.     }  
  47.    
  48.     /** 
  49.      * Description 根据键值进行加密 
  50.      * @param data 
  51.      * @param key  加密键byte数组 
  52.      * @return 
  53.      * @throws Exception 
  54.      */  
  55.     private static byte[] encrypt(byte[] data, byte[] key) throws Exception {  
  56.         // 生成一个可信任的随机数源  
  57.         SecureRandom sr = new SecureRandom();  
  58.    
  59.         // 从原始密钥数据创建DESKeySpec对象  
  60.         DESKeySpec dks = new DESKeySpec(key);  
  61.    
  62.         // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象  
  63.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);  
  64.         SecretKey securekey = keyFactory.generateSecret(dks);  
  65.    
  66.         // Cipher对象实际完成加密操作  
  67.         Cipher cipher = Cipher.getInstance(DES);  
  68.    
  69.         // 用密钥初始化Cipher对象  
  70.         cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);  
  71.    
  72.         return cipher.doFinal(data);  
  73.     }  
  74.        
  75.        
  76.     /** 
  77.      * Description 根据键值进行解密 
  78.      * @param data 
  79.      * @param key  加密键byte数组 
  80.      * @return 
  81.      * @throws Exception 
  82.      */  
  83.     private static byte[] decrypt(byte[] data, byte[] key) throws Exception {  
  84.         // 生成一个可信任的随机数源  
  85.         SecureRandom sr = new SecureRandom();  
  86.    
  87.         // 从原始密钥数据创建DESKeySpec对象  
  88.         DESKeySpec dks = new DESKeySpec(key);  
  89.    
  90.         // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象  
  91.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);  
  92.         SecretKey securekey = keyFactory.generateSecret(dks);  
  93.    
  94.         // Cipher对象实际完成解密操作  
  95.         Cipher cipher = Cipher.getInstance(DES);  
  96.    
  97.         // 用密钥初始化Cipher对象  
  98.         cipher.init(Cipher.DECRYPT_MODE, securekey, sr);  
  99.    
  100.         return cipher.doFinal(data);  
  101.     }  
  102.       
  103.       
  104.     /** 
  105.      * Description 获取字符串MD5值 
  106.      * @param sourceStr 
  107.      */  
  108.     private static String MD5(String sourceStr) {  
  109.         String result = "";  
  110.         try {  
  111.             MessageDigest md = MessageDigest.getInstance("MD5");  
  112.             md.update(sourceStr.getBytes());  
  113.             byte b[] = md.digest();  
  114.             int i;  
  115.             StringBuffer buf = new StringBuffer("");  
  116.             for (int offset = 0; offset < b.length; offset++) {  
  117.                 i = b[offset];  
  118.                 if (i < 0)  
  119.                     i += 256;  
  120.                 if (i < 16)  
  121.                     buf.append("0");  
  122.                 buf.append(Integer.toHexString(i));  
  123.             }  
  124.             result = buf.toString();  
  125.             // System.out.println("MD5(" + sourceStr + ",32) = " + result);  
  126.             // System.out.println("MD5(" + sourceStr + ",16) = " +  
  127.             // buf.toString().substring(8, 24));  
  128.         } catch (NoSuchAlgorithmException e) {  
  129.             log.error(e.getMessage());  
  130.         }  
  131.         return result;  
  132.     }  
  133.       
  134.       
  135.     public static void main(String[] args) throws Exception {  
  136.         String data = "{devType:\"1\",Sys:\"01\",Name:\"张三\",PoId:\"000002\",TarPho:\"15527609770\",Desc:\"张三偷窃\"}";  
  137.         String key = "12345678";//秘钥  
  138.         String encode = encrypt(data, key);  
  139.         System.err.println(encode);  
  140.         String dcode = decrypt(encode, key);  
  141.         System.err.println(dcode);  
  142.     }  
  143. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值