java中使用DES加密解密实例
在前面介绍了一些加密解密类的使用,这里综合起来做一个简单的测试,代码如下:
mainactivity:
package com.home.testdes;
import android.os.bundle;
import android.util.log;
import android.app.activity;
public class mainactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
desutil u = new desutil();
string mi = u.getenc("i love you");
log.i("加密后", mi);
string ming = u.getdec(mi);
log.i("解密后", ming);
}
}
加密解密工具类:
package com.home.testdes;
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 android.util.base64;
/**
* 使用des加密和解密工具类
*
* @author administrator
*
*/
public class desutil {
private key key;// 密钥的key值
private byte[] deskey;
private byte[] desiv = { 0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xab,
(byte) 0xcd, (byte) 0xef };
private algorithmparameterspec iv = null;// 加密算法的参数接口
public desutil() {
try {
this.deskey = "abcdefghijk".getbytes("utf-8");// 设置密钥
deskeyspec keyspec = new deskeyspec(deskey);// 设置密钥参数
iv = new ivparameterspec(desiv);// 设置向量
secretkeyfactory keyfactory = secretkeyfactory.getinstance("des");// 获得密钥工厂
key = keyfactory.generatesecret(keyspec);// 得到密钥对象
} catch (exception e) {
e.printstacktrace();
}
}
/**
* 加密string 明文输入密文输出
*
* @param inputstring
* 待加密的明文
* @return 加密后的字符串
*/
public string getenc(string inputstring) {
byte[] bytemi = null;
byte[] byteming = null;
string outputstring = "";
try {
byteming = inputstring.getbytes("utf-8");
bytemi = this.getenccode(byteming);
byte[] temp = base64.encode(bytemi, base64.default);
outputstring = new string(temp);
} catch (exception e) {
} finally {
byteming = null;
bytemi = null;
}
return outputstring;
}
/**
* 解密string 以密文输入明文输出
*
* @param inputstring
* 需要解密的字符串
* @return 解密后的字符串
*/
public string getdec(string inputstring) {
byte[] byteming = null;
byte[] bytemi = null;
string strming = "";
try {
bytemi = base64.decode(inputstring.getbytes(), base64.default);
byteming = this.getdescode(bytemi);
strming = new string(byteming, "utf8");
} catch (exception e) {
} finally {
byteming = null;
bytemi = null;
}
return strming;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param bt
* 待加密的字节码
* @return 加密后的字节码
*/
private byte[] getenccode(byte[] bt) {
byte[] bytefina = null;
cipher cipher;
try {
// 得到cipher实例
cipher = cipher.getinstance("des/cbc/pkcs5padding");
cipher.init(cipher.encrypt_mode, key, iv);
bytefina = cipher.dofinal(bt);
} catch (exception e) {
e.printstacktrace();
} finally {
cipher = null;
}
return bytefina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param bt
* 待解密的字节码
* @return 解密后的字节码
*/
private byte[] getdescode(byte[] bt) {
cipher cipher;
byte[] bytefina = null;
try {
// 得到cipher实例
cipher = cipher.getinstance("des/cbc/pkcs5padding");
cipher.init(cipher.decrypt_mode, key, iv);
bytefina = cipher.dofinal(bt);
} catch (exception e) {
e.printstacktrace();
} finally {
cipher = null;
}
return bytefina;
}
}
上一篇: tomcat301与java301解析
下一篇: java裁剪图片并保存的示例分享