java的des加密
package ajax.des;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class CryptoTools {
private static final String key_ = "ising@ws"; //
private static final byte [] DESkey = key_.getBytes();//设置密钥,略去
private static final byte[] DESIV = { (byte)0x12,(byte) 0x34, (byte)0x56, (byte)0x78, (byte)0x90,
(byte)0xAB, (byte)0xCD,(byte) 0xEF };//设置向量,略去
private static AlgorithmParameterSpec iv =null;//加密算法的参数接口,IvParameterSpec是它的一个实现
private static Key key =null;
public CryptoTools() throws Exception {
DESKeySpec keySpec = new DESKeySpec(DESkey);//设置密钥参数
iv = new IvParameterSpec(DESIV);//设置向量
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//获得密钥工厂
key = keyFactory.generateSecret(keySpec);//得到密钥对象
}
public String encode(String data) throws Exception {
Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");//得到加密对象Cipher
enCipher.init(Cipher.ENCRYPT_MODE,key,iv);//设置工作模式为加密模式,给出密钥和向量
byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(pasByte);
}
public String decode(String data) throws Exception{
Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
deCipher.init(Cipher.DECRYPT_MODE,key,iv);
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] pasByte=deCipher.doFinal(base64Decoder.decodeBuffer(data));
return new String(pasByte,"UTF-8");
}
public static void main(String[] args) throws Exception{
String value = "hello";
CryptoTools tools =new CryptoTools();
System.out.println(tools.encode(value));
System.out.println("解密"+tools.decode(tools.encode(value)));
}
} /> }
[/code]
haohao
2008-07-23 13:01:51
评论:0
阅读:49
引用:0
