/*
* 文件名:DESCoder.java
* 修改内容:新增
*/
package com.huawei.gts.impl.util.des;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* <p>功能简述</p>
* <p>功能详述</p>
* @author Lin Wen Xian KF74690
* @version 1.0, 2013-1-22
* @see
* @since
*/
public class DESCoder
{
// a weak key
private final static BASE64Encoder base64encoder = new BASE64Encoder();
private final static BASE64Decoder base64decoder = new BASE64Decoder();
private static final String encoding = "UTF-8";//"GBK";
private static final String sKey = "12345678901234567890123456789012";//"k34AAE4TAABMAQAA9HcAAIdCAADbRQAA";
/**
* 加密字符串
*/
public static String ebotongEncrypto(String str)
{
String result = str;
if (str != null && str.length() > 0)
{
try
{
byte[] encodeByte = symmetricEncrypto(str.getBytes(encoding));
result = base64encoder.encode(encodeByte);
}
catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 解密字符串 (物料价格把String变成Double)
*/
public static double ebotongDecrypto(String str)
{
double result = 0;
if (str != null && str.length() > 0)
{
try
{
byte[] encodeByte = base64decoder.decodeBuffer(str);
byte[] decoder = symmetricDecrypto(encodeByte);
result = Double.parseDouble(new String(decoder, encoding));
}
catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 加密byte[]
*/
public static byte[] ebotongEncrypto(byte[] str)
{
byte[] result = null;
if (str != null && str.length > 0)
{
try
{
System.out.println("str:"+str);
byte[] encodeByte = symmetricEncrypto(str);
System.out.println("encodeByte:"+encodeByte);
result = base64encoder.encode(encodeByte).getBytes();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 解密byte[]
*/
public static byte[] ebotongDecrypto(byte[] str)
{
byte[] result = null;
if (str != null && str.length > 0)
{
try
{
byte[] encodeByte = base64decoder.decodeBuffer(new String(str,encoding));
//byte[] encodeByte = base64decoder.decodeBuffer(new String(str));
byte[] decoder = symmetricDecrypto(encodeByte);
result = new String(decoder).getBytes(encoding);
result = new String(decoder).getBytes();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 对称加密字节数组并返回
*
* @param byteSource 需要加密的数据
* @return 经过加密的数据
* @throws Exception
*/
public static byte[] symmetricEncrypto(byte[] byteSource) throws Exception
{
try
{
int mode = Cipher.ENCRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = sKey.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] result = cipher.doFinal(byteSource);
return result;
}
catch (Exception e)
{
throw e;
}
finally
{
}
}
/**
* 对称解密字节数组并返回
*
* @param byteSource 需要解密的数据
* @return 经过解密的数据
* @throws Exception
*/
public static byte[] symmetricDecrypto(byte[] byteSource) throws Exception
{
try
{
int mode = Cipher.DECRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = sKey.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] result = cipher.doFinal(byteSource);
return result;
}
catch (Exception e)
{
throw e;
}
finally
{
}
}
public static void main(String[] args)
{
/*start 加密 “你好”*/
try
{
String dd=ebotongEncrypto("你好");
System.out.println(dd); //输出为:QO2klVpoYT8=
}
catch (Exception e)
{
e.printStackTrace();
}
/*end 加密 “你好”*/
/*start 解密 为“你好”*/
try
{
byte[] encodeByte = base64decoder.decodeBuffer("QO2klVpoYT8=");
byte[] decoder = symmetricDecrypto(encodeByte);
String dd= new String(decoder, encoding);
System.out.println(dd); //输出为:你好
}
catch (Exception e)
{
e.printStackTrace();
}
/*end 解密*/
}
}
* 文件名:DESCoder.java
* 修改内容:新增
*/
package com.huawei.gts.impl.util.des;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* <p>功能简述</p>
* <p>功能详述</p>
* @author Lin Wen Xian KF74690
* @version 1.0, 2013-1-22
* @see
* @since
*/
public class DESCoder
{
// a weak key
private final static BASE64Encoder base64encoder = new BASE64Encoder();
private final static BASE64Decoder base64decoder = new BASE64Decoder();
private static final String encoding = "UTF-8";//"GBK";
private static final String sKey = "12345678901234567890123456789012";//"k34AAE4TAABMAQAA9HcAAIdCAADbRQAA";
/**
* 加密字符串
*/
public static String ebotongEncrypto(String str)
{
String result = str;
if (str != null && str.length() > 0)
{
try
{
byte[] encodeByte = symmetricEncrypto(str.getBytes(encoding));
result = base64encoder.encode(encodeByte);
}
catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 解密字符串 (物料价格把String变成Double)
*/
public static double ebotongDecrypto(String str)
{
double result = 0;
if (str != null && str.length() > 0)
{
try
{
byte[] encodeByte = base64decoder.decodeBuffer(str);
byte[] decoder = symmetricDecrypto(encodeByte);
result = Double.parseDouble(new String(decoder, encoding));
}
catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 加密byte[]
*/
public static byte[] ebotongEncrypto(byte[] str)
{
byte[] result = null;
if (str != null && str.length > 0)
{
try
{
System.out.println("str:"+str);
byte[] encodeByte = symmetricEncrypto(str);
System.out.println("encodeByte:"+encodeByte);
result = base64encoder.encode(encodeByte).getBytes();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 解密byte[]
*/
public static byte[] ebotongDecrypto(byte[] str)
{
byte[] result = null;
if (str != null && str.length > 0)
{
try
{
byte[] encodeByte = base64decoder.decodeBuffer(new String(str,encoding));
//byte[] encodeByte = base64decoder.decodeBuffer(new String(str));
byte[] decoder = symmetricDecrypto(encodeByte);
result = new String(decoder).getBytes(encoding);
result = new String(decoder).getBytes();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}
/**
* 对称加密字节数组并返回
*
* @param byteSource 需要加密的数据
* @return 经过加密的数据
* @throws Exception
*/
public static byte[] symmetricEncrypto(byte[] byteSource) throws Exception
{
try
{
int mode = Cipher.ENCRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = sKey.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] result = cipher.doFinal(byteSource);
return result;
}
catch (Exception e)
{
throw e;
}
finally
{
}
}
/**
* 对称解密字节数组并返回
*
* @param byteSource 需要解密的数据
* @return 经过解密的数据
* @throws Exception
*/
public static byte[] symmetricDecrypto(byte[] byteSource) throws Exception
{
try
{
int mode = Cipher.DECRYPT_MODE;
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyData = sKey.getBytes();
DESKeySpec keySpec = new DESKeySpec(keyData);
Key key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(mode, key);
byte[] result = cipher.doFinal(byteSource);
return result;
}
catch (Exception e)
{
throw e;
}
finally
{
}
}
public static void main(String[] args)
{
/*start 加密 “你好”*/
try
{
String dd=ebotongEncrypto("你好");
System.out.println(dd); //输出为:QO2klVpoYT8=
}
catch (Exception e)
{
e.printStackTrace();
}
/*end 加密 “你好”*/
/*start 解密 为“你好”*/
try
{
byte[] encodeByte = base64decoder.decodeBuffer("QO2klVpoYT8=");
byte[] decoder = symmetricDecrypto(encodeByte);
String dd= new String(decoder, encoding);
System.out.println(dd); //输出为:你好
}
catch (Exception e)
{
e.printStackTrace();
}
/*end 解密*/
}
}