学习中碰到的一个特别奇怪的问题,送100分! 你这个过程就是不可逆的new String(ee) //这个过程会作GB2312转码加密解密应该在byte数组上进行加密: String-》byte[]-》加密byte[]解密: 加密byte[]-》byte[]-》String 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 得到的byte[]不等于ee, 你是指他们的值不相等,还是什么?我试的结果是相等的(因为两个过程用的都是默认的字符集)。如果,你是指地址不相等,那是肯定的。 实际问题是这样的:我做的加密程序是针对我的web应用配置文件的数据库密码的,只要系统启动,第一次读取配置文件中数据库的密码,就强行写一个加密的串到密码部分做覆盖,当应用再启动时候读取这个加密的字符串再解码。因为要读取加密的串,所以碰到以上的问题,现在该如何解决阿?还有,我需要存入xml文件的这个加密的串不能是乱码,否则xml文件会出错(<?xml version="1.0" encoding="GB2312"?>),程序如下:import java.security.*;import javax.crypto.*;public class Crypt { private static String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish static boolean debug = false; static{ Security.addProvider(new com.sun.crypto.provider.SunJCE()); } //生成密钥, 注意此步骤时间比较长 public static byte[] getKey() throws Exception{ KeyGenerator keygen = KeyGenerator.getInstance(Algorithm); SecretKey deskey = keygen.generateKey(); if (debug) System.out.println("生成密钥:"+byte2hex(deskey.getEncoded())); return deskey.getEncoded(); return "好好学习".getBytes(); } //加密 public static byte[] encode(byte[] input,byte[] key) throws Exception{ SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm); if (debug){ System.out.println("加密前的二进串:"+byte2hex(input)); System.out.println("加密前的字符串:"+new String(input)); } Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE,deskey); byte[] cipherByte=c1.doFinal(input); if (debug) System.out.println("加密后的二进串:"+byte2hex(cipherByte)); return cipherByte; } //解密 public static byte[] decode(byte[] input,byte[] key) throws Exception{ SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm); if (debug) System.out.println("解密前的信息:"+byte2hex(input)); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.DECRYPT_MODE,deskey); byte[] clearByte=c1.doFinal(input); if (debug){ System.out.println("解密后的二进串:"+byte2hex(clearByte)); System.out.println("解密后的字符串:"+(new String(clearByte))); } return clearByte; } //字节码转换成16进制字符串 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; if (n<b.length-1) hs=hs+":"; } return hs.toUpperCase(); } public static void main(String[] args) throws Exception{ debug = true; byte[] key = "好好学习".getBytes();//特定的key byte[] e = encode("123".getBytes(),key);//"123"是我要加密的字符串 String object = new String(e,"8859_1");//这是我要存入xml文件的字符串 byte[] d = decode(object.getBytes("8859_1"),key);//解密 }} 另外,这种形势的加密解密就象websphere的admin.config文件的数据库连接密码一样的形式----都是为了让类似密码的数据不被看到 http://expert.csdn.net/Expert/topic/1440/1440515.xml?temp=.1720392已经解决! Matcher的返回结果怎么不对、明明是多个字母的。 一则随机数取值范围的疑惑 困扰了一个下午,还是没解决的JDK问题 我要用java 的相关技术做心跳检测的客户端,和用户的上下线请请求,知道原理的请进 怎样建立一个远程目录?? 利用游览器游览applet需要什么配置,或需要注意什么? 因为我一直搞不定,谢!!!! 请帮忙 关于awt的label换行 简单的问题 请问在awt中怎样让Button处于按下状态 Java中哪个函数能实现查看cpu状态的功能?? 怎样在一个按扭按下时,既提交了表单还能传一个自定义的变量。
因为要读取加密的串,所以碰到以上的问题,现在该如何解决阿?还有,我需要存入xml文件的这个加密的串不能是乱码,否则xml文件会出错(<?xml version="1.0" encoding="GB2312"?>),
程序如下:import java.security.*;
import javax.crypto.*;
public class Crypt { private static String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish static boolean debug = false; static{
Security.addProvider(new com.sun.crypto.provider.SunJCE());
} //生成密钥, 注意此步骤时间比较长
public static byte[] getKey() throws Exception{
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
if (debug)
System.out.println("生成密钥:"+byte2hex(deskey.getEncoded()));
return deskey.getEncoded();
return "好好学习".getBytes();
} //加密
public static byte[] encode(byte[] input,byte[] key) throws Exception{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm);
if (debug){
System.out.println("加密前的二进串:"+byte2hex(input));
System.out.println("加密前的字符串:"+new String(input));
}
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE,deskey);
byte[] cipherByte=c1.doFinal(input);
if (debug)
System.out.println("加密后的二进串:"+byte2hex(cipherByte));
return cipherByte;
} //解密
public static byte[] decode(byte[] input,byte[] key) throws Exception{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm);
if (debug)
System.out.println("解密前的信息:"+byte2hex(input));
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE,deskey);
byte[] clearByte=c1.doFinal(input);
if (debug){
System.out.println("解密后的二进串:"+byte2hex(clearByte));
System.out.println("解密后的字符串:"+(new String(clearByte)));
}
return clearByte;
} //字节码转换成16进制字符串
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;
if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
} public static void main(String[] args) throws Exception{
debug = true;
byte[] key = "好好学习".getBytes();//特定的key
byte[] e = encode("123".getBytes(),key);//"123"是我要加密的字符串
String object = new String(e,"8859_1");//这是我要存入xml文件的字符串
byte[] d = decode(object.getBytes("8859_1"),key);//解密
}
}
已经解决!