DES加密 (Byte[]加密算法) 可以自定义 key 默认采用密钥:wcun5515 ,(密钥为:生活网提供)

package com.transnal.ws.cnlif.bean;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


/**
*  DES加密 (Byte[]加密算法)
*  可以自定义 key
*  默认采用密钥:wcun5515 ,(密钥为:生活网提供)

* @author RenWeigang
*
* @since 2010.09.07
*/
public class DESPlus {

/**
* 编码风格
*/
private static final String ENCODEE ="UTF-8";

/**
* 密钥 key,由生活网提供
*/
    private static final String DEFAULT_KEY = "wcun5515";
   
    /**
     * 设置密钥
     */
private static byte[] des_key;
   
    /**
     * 加密算法的参数接口,IvParameterSpec是它的一个实现,规定了密钥必须是8个字节
     */
    private static AlgorithmParameterSpec iv =null;
   
    /**
     * 密钥对象
     */
    private static Key secretKey =null;
   
static{
try {
des_key = DEFAULT_KEY.getBytes(ENCODEE);

//得到密钥对象
secretKey = getKey(des_key);

//设置向量(密钥必须是8个字节,否则抛出异常)
iv = new IvParameterSpec(des_key);

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

/**
* 解密数据,按默认密钥(wcun5515)解密
* @param message
* 已经被加密后的字符串
* @return
* 解密后的字符串
* @throws Exception
*/
public static String decrypt(String message) throws Exception {

byte[] bytesrc = stringToHexByte(message);

Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

byte[] retByte = cipher.doFinal(bytesrc);

return new String(retByte);

}

/**
* 解密字符串,自定义密钥解密
* @param message
* 已经被加密后的字符串
* @param key
* 解密 密钥[密钥必须为8个字节,并且与加密密钥符合]
* @return
* 解密后的字符串
* @throws Exception
*/
public static String decrypt(String message,String key) throws Exception {
if(des_key!=null)
des_key=null;
if(secretKey!=null)
secretKey=null;
if(iv!=null)
iv=null;
byte[] bytesrc=stringToHexByte(message);

Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

Key strKey = getKey(key.getBytes(ENCODEE));

//规定了密钥必须是8个字节
AlgorithmParameterSpec aiv = new IvParameterSpec(key.getBytes(ENCODEE));
cipher.init(Cipher.DECRYPT_MODE,strKey,aiv);

byte[] retByte = cipher.doFinal(bytesrc);

return new String(retByte);

}

/**
* 加密数据
* 按默认密钥(wcun5515)加密
* String明文输入,byte[]密文输出 
*
* @param message
* 要加密的字符串
* @param toLowerCase
* 被加密的字符串是否转为小写 True: 小写 ; False 为不转
* @return
* @throws Exception
*/
public static byte[] encrypt(String message,boolean toLowerCase)throws Exception {

//得到加密对象Cipher
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

//设置工作模式为加密模式,给出密钥和向量
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

if(toLowerCase){
//把要被加密的用户名都转为小写
message = java.net.URLEncoder.encode(message,ENCODEE).toLowerCase();
}
return cipher.doFinal(message.getBytes(ENCODEE));

}

/**
* 加密字符串(针对用户名)
* 按默认密钥(wcun5515)加密
* @param message
* 需加密的字符串
* @return
* 加密后的字符串
* @throws Exception
*/
public static String encrypt(String message)throws Exception {

return byteToHexString(encrypt(message,true),true);

}

/**
* 加密字符串
* 按默认密钥(wcun5515)加密
* @param message
* 要加密的字符串
* @param messageToLowerCase
* 被加密的字符串是否转为小写,True 为转为小写;False为默认
* @param hexStringToUpperCase
* 加密后的字符串是否转为大写,True 为转为大写;False为默认
* @return
* 加密后的字符串
* @throws Exception
*/
public static String encrypt(String message,boolean messageToLowerCase,boolean hexStringToUpperCase)throws Exception {

return byteToHexString(encrypt(message,messageToLowerCase),hexStringToUpperCase);

}

/**
* 加密字符串,自定义密钥
* @param message
* 要加密的字符串
* @param key
* 加密 密钥,[密钥为8个字节]
* @return
* 加密后的字符串
* @throws Exception
*/
public static String encrypt(String message, String key)throws Exception {
if(des_key!=null)
des_key=null;
if(secretKey!=null)
secretKey=null;
if(iv!=null)
iv=null;

Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
Key strKey =  getKey(key.getBytes(ENCODEE));
//规定了密钥必须是8个字节
IvParameterSpec aiv = new IvParameterSpec(key.getBytes(ENCODEE));
cipher.init(Cipher.ENCRYPT_MODE, strKey,aiv);
byte[] hexByte = cipher.doFinal(message.getBytes(ENCODEE));
return byteToHexString(hexByte,false);
}


/**
* 将表示16进制值的字符串转换为byte数组
* @param paramString
* 需要转换的字符串
* @return
* 转换后的字节
*/
public static byte[] stringToHexByte(String paramString){

// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte digest[] = new byte[paramString.length() / 2];

//采用不同的算法,加密后的数据也不相同
for (int i = 0; i < digest.length; i++){
String byteString = paramString.substring(2 * i, 2 * i + 2);
int byteValue = Integer.parseInt(byteString, 16);
digest[i] = (byte) byteValue;
}
return digest;
}

/**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813
* 和 StringToHexByte(String paramString) 互为可逆的转换过程.
*
* @param paramByte
* 需要转换的byte数组
* @param toUpperCase
* 是否以大写的形式输出;True为大写 ;False 为默认
* @return
* 转换后的字符串
*/
public static String byteToHexString(byte paramByte[],boolean toUpperCase) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < paramByte.length; i++) {
String plainText = Integer.toHexString(0xff & paramByte[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}
if(toUpperCase){
return hexString.toString().toUpperCase();
}
return hexString.toString();
}

/**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
* @param arrBTmp
* 构成该字符串的字节数组
* @return
* 生成的密钥
* @throws Exception
*/
private static Key getKey(byte[] paramByte) throws Exception {

// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];

// 将原始字节数组转换为8位
for (int i = 0; i < paramByte.length && i < arrB.length; i++) {
arrB[i] = paramByte[i];
}

// 生成密钥
Key key = new SecretKeySpec(arrB, "DES");

return key;
}




/**
* 测试类
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {

//默认key 加密与解密
String value = "renweigang";

System.out.println("加密数据:" + value);

String a = encrypt(value);
System.out.println("加密后的数据为:" + a);

String b = decrypt(a);
System.out.println("解密后的数据:" + b);


///////自定义key加密与解密///////////////////////////////
// String key = "wcun5515";
// String value = "renweigang";
//
// System.out.println("加密数据:" + value);
//
// String a = encrypt(value,key);
// System.out.println("加密后的数据为:" + a);
//
// String b = decrypt(a,key);
// System.out.println("解密后的数据:" + b);
}
}

你可能感兴趣的:(bean,算法,生活,Security)