求!!!用java编写的用户透明加密的源代码 急!!!各位高手帮助!!!

如题,求一段用java语言编写的源代码,要求实现用户透明加密功能。。。还有脱密的JAVA源代码,请帮忙注上中文注解!!!!!万分感谢... 如题,求一段用java语言编写的源代码,要求实现用户透明加密功能。。。
还有脱密的JAVA源代码,请帮忙注上中文注解!!!!!万分感谢
展开
 我来答
sky11qq
2010-12-13
知道答主
回答量:7
采纳率:0%
帮助的人:10.6万
展开全部
很久以前在网上找到一,不知道对LZ有无帮助
package TestNativeOutOfMemoryError;

import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

public class DesEncrypt {
Key key;

/**
* generate a KEY with the param
*
* @param strKey
*/
public void getKey(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();
}
}

/**
* encrypt
*
* @param strMing to be encrypt
* @return
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
try {
return byte2hex(getEncCode(strMing.getBytes()));

// byteMing = strMing.getBytes("UTF8");
// byteMi = this.getEncCode(byteMing);
// strMi = new String( byteMi,"UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
byteMing = null;
byteMi = null;
}
return strMi;
}

/**
* decrypt
*
* @param strMi
* @return
*/
public String getDesString(String strMi) {
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
return new String(getDesCode(hex2byte(strMi.getBytes())));

// byteMing = this.getDesCode(byteMi);
// strMing = new String(byteMing,"UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
byteMing = null;
byteMi = null;
}
return strMing;
}

private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}

private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}

public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}

public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("illlegal data");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}

return b2;
}

public static void main(String[] args) {
DesEncrypt des = new DesEncrypt();
des.getKey("aadd");

String strEnc = des.getEncString("Testing of DES encrypt");
System.out.println(strEnc);

String strDes = des.getDesString(strEnc);
System.out.println(strDes);
new DesEncrypt();
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式