在此申明:很多文章转载自互联网和朋友的博客. 常用数据库连接语句 常用常用Jar包下载常用下载hibernate frameworkSpring APIhibernate API jdk6 API JQueryAPI Spring Framework 2.5 Reference中文版 Zblog之家 中国服务器专业定制网 域名查询 谷歌翻译 经典酷站

专注Java

会好好的为你撑起这个世界,再累也不会让你承担。
posts - 544, comments - 76, trackbacks - 1, articles - 1
   :: 首页 :: 新随笔 :: 联系 ::  :: 管理

JavaDES加密

Posted on 2009-07-06 15:03 沙家三少 阅读(25) 评论(0)  编辑 收藏 网摘 所属分类: java
package com.d1xn.core.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * 一般加密解密工具<br>
 * DESede/DES/BlowFish<br>
 * DES的密钥(key)长度是为8个字节,加密解密的速度是最快的<br>
 * DESede的密钥(key)长度是24个字节<br>
 * BlowFish的密钥(key)是可变的 加密最快,强度最高,密钥长度范围(1<=key<=16)<br>
 * 
 * 
@author songdawei
 * 
@version 1.0
 * @date 2008-10-20
 
*/
public class SecretCodeUtil {
    
public final static String DES = "DES";
    
public final static String DESEDE = "DESede";
    
public final static String BLOWFISH = "BlowFish";
    
public final static String MD5 = "MD5";

    
/**
     * md5加密
     * 
     * 
@param plainText
     * 
@return
     
*/
    
public static String getMD5ofStr(String plainText) {
        
try {
            
return getMD5ofStr(string2Bytes(plainText));
        } 
catch (Exception e) {
            e.printStackTrace();
        }
        
return null;
    }

    
public static String getMD5ofStr(byte str[]) {
        
try {
            MessageDigest md 
= MessageDigest.getInstance(MD5);
            md.update(str);
            
byte b[] = md.digest();
            
return bytes2HexString(b);

        } 
catch (Exception e) {
            e.printStackTrace();
        }
        
return null;
    }

    
/**
     * 对数据源进行加密
     * 
     * 
@param src
     *            数据源
     * 
@param key
     *            密钥
     * 
@param name
     *            算法的名称
     * 
@return 返回加密后的数据
     * 
@throws Exception
     
*/
    
public static byte[] encrypt(byte[] src, byte[] key, String name)
            
throws Exception {

        SecretKeySpec securekey 
= new SecretKeySpec(key, name);
        
// Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(name);
        
// 用密匙初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey);
        
// 现在,获取数据并加密
        
// 正式执行加密操作
        return cipher.doFinal(src);
    }

    
/**
     * 对加密的数据源进行解密
     * 
     * 
@param src
     *            数据源
     * 
@param key
     *            密钥
     * 
@param name
     *            算法的名称
     * 
@return 返回解密后的原始数据
     * 
@throws Exception
     
*/
    
public static byte[] decrypt(byte[] src, byte[] key, String name)
            
throws Exception {

        SecretKeySpec securekey 
= new SecretKeySpec(key, name);
        
// Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(name);
        
// 用密匙初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey);
        
// 现在,获取数据并解密
        
// 正式执行解密操作
        return cipher.doFinal(src);
    }

    
/**
     * 二行制转字符串
     * 
     * 
@param b
     * 
@return
     
*/
    
public static String bytes2HexString(byte[] bytes) {
        String hs 
= null;
        
if (bytes != null) {
            
final int size = bytes.length;
            
if (size > 0) {
                StringBuilder sb 
= new StringBuilder();
                
for (int i = 0; i < size; i++) {
                    String tmp 
= (java.lang.Integer.toHexString(bytes[i] & 0XFF));
                    
if (tmp.length() == 1) {
                        sb.append(
"0" + tmp);
                    } 
else {
                        sb.append(tmp);
                    }
                }
                hs 
= sb.toString().toUpperCase();
            }
        }
        
return hs;
    }

    
public static byte[] hex2byte(byte[] b) {
        
if ((b.length % 2!= 0) {
            
return null;
        }
        
byte[] b2 = new byte[b.length / 2];
        
for (int n = 0; n < b.length; n += 2) {
            String item 
= new String(b, n, 2);
            b2[n 
/ 2= (byte) Integer.parseInt(item, 16);
        }
        
return b2;
    }
    
    
/**
     * 十六进位格式字符串转二进制流。
     * 
     * 
@param hs String
     * 
@return byte[]
     
*/
    
public static byte[] hexString2Bytes (String hs) {
        
byte[] bytes = null;
        
if (hs != null) {
            
final int size = (hs.length()) / 2;
            
if (size > 0) {
                bytes 
= new byte[size];
                
for (int i = 0; i < size; i++) {
                    String hsByte 
= hs.substring(i * 2, i * 2 + 2);
                    
byte b = 0;
                    
try {
                        b 
= (byte) (Integer.parseInt(hsByte, 16));
                    } 
catch (java.lang.NumberFormatException e) {
                        b 
= 0;
                    }
                    bytes[i] 
= b;
                }
            }
        }
        
return bytes;
    }

    
/**
     * 字符串解密
     * 
     * 
@param data
     *            字符串加密数据
     * 
@param key
     *            密钥
     * 
@param name
     *            算法名称
     * 
@return
     * 
@throws Exception
     
*/
    
public static String decrypt(String data, String key, String name) {
        
try {
            
return new String(decrypt(hex2byte(data.getBytes()),
                    string2Bytes(key), name));
        } 
catch (Exception e) {

        }
        
return null;
    }
    
    
/**
     * 字符串解密
     * 
     * 
@param data
     *            字符串加密数据
     * 
@param key
     *            密钥
     * 
@param name
     *            算法名称
     * 
@return
     * 
@throws Exception
     
*/
    
public static String decrypt(String data, byte[] key, String name) {
        
try {
            
return new String(decrypt(hex2byte(data.getBytes()),key, name));
        } 
catch (Exception e) {

        }
        
return null;
    }
    
    
/**
     * 把字符串转化成 Unicode Bytes.
     * 
     * 
@param s String
     * 
@return byte[]
     
*/
    
public static byte [] string2Bytes(String s) {
        
byte[] bytes = null;
        
if (s != null) {
            
try {
                bytes 
= s.getBytes("utf-8");
            } 
catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        
return bytes;
    }

    
/**
     * 根据 Unicode Bytes 构造字符串.
     * 
     * 
@param bytes byte[]
     * 
@return String
     
*/
    
public static String bytes2String (byte[] bytes) {
        String s 
= null;
        
if (bytes != null) {
            
try {
                s 
= new String(bytes, "utf-8");
            } 
catch (Exception e) {
                e.printStackTrace();
            }
        }
        
return s;
    }

    
    
/**
     * 字符串加密
     * 
     * 
@param data
     *            字符串数据
     * 
@param key
     *            密钥
     * 
@param name
     *            算法名称
     * 
@throws Exception
     
*/
    
public static String encrypt(String data, byte[] key, String name) {
        
try {
            
return bytes2HexString(encrypt(data.getBytes(), key, name));
        } 
catch (Exception e) {
            e.printStackTrace();
        }
        
return null;
    }
    
    
/**
     * 字符串加密
     * 
     * 
@param data
     *            字符串数据
     * 
@param key
     *            密钥
     * 
@param name
     *            算法名称
     * 
@throws Exception
     
*/
    
public static String encrypt(String data, String key, String name) {
        
try {
            
return bytes2HexString(encrypt(data.getBytes(), string2Bytes(key), name));
        } 
catch (Exception e) {
            e.printStackTrace();
        }
        
return null;
    }
}
0
0
(请您对文章做出评价)
« 上一篇:两日期之间的时间差
» 下一篇:MyEclipse6.5发布项目时不能编译类的解决办法

数据库连接语句 sqlserver: jdbc:sqlserver://localhost:1433;databaseName=db
mysql: jdbc:mysql://localhost:3309/sampledb
oracle: jdbc:oracle:thin:@10.10.10.6:1521:DataBaseName

常用文章链接
  1. 配置文件
  2. Html-Js代码互转便利工具
  3. 颜色提取器
  4. 常见异常处理
  5. AJAX Loading生成
  6. js格式化,加密
  7. 查找Jar文件

jar包下载 ssh jar文件下载 提取码:ed727ed2
ssh 源代码 jar文件下载 提取码:210ddb47

常用下载 java反编译器 提取码:52ff37bb

回顶部