java encode and decode by des
package com.tian.test16;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import javax.crypto.*;
import sun.misc.*;
public class Encrypt {
private Key key;
public void setKey(String strKey) {
try {
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(strKey.getBytes()));
this.key = generator.generateKey();
generator = null;
} catch (Exception e) {
e.printStackTrace();
}
}
public String encodeString(String str) {
BASE64Encoder base64en = new BASE64Encoder();
byte[] fisrt = null;
try {
fisrt = str.getBytes("UTF8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DES");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] second = null;
try {
second = cipher.doFinal(fisrt);
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = base64en.encode(second);
return result;
}
public String decodeString(String str) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] first = null;
try {
first = base64De.decodeBuffer(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DES");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] second = null;
try {
second = cipher.doFinal(first);
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = null;
try {
result = new String(second, "UTF8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
Encrypt et = new Encrypt();
et.setKey("my test key");
String encode = et.encodeString("mr tian this is the test account");
System.out.println(encode);
String decode = et.decodeString(encode);
System.out.println(decode);
}
}
上一篇: mongodb 参数详解
推荐阅读
-
java encode and decode by des
-
js中encode、decode的应用说明 博客分类: js jsencodedecode
-
Java加密解密快速入门上篇【包括MD5、BASE64、DES、RSA等算法】 博客分类: Java java加密md5base64安全
-
用java实现3des加密
-
JAVA 实现DES MD5加密
-
java加密MD5,DES
-
摘要(MD5\SHA1\MAC)、对称加密(DES\AES)、非对称加密(DSA\RSA)签名在JAVA中应用
-
DES加密解密 博客分类: Java desutil
-
DES加密解密 博客分类: Java desutil
-
DES加密解密 博客分类: Java DESUtil