你这个过程就是不可逆的
new String(ee) //这个过程会作GB2312转码
加密解密应该在byte数组上进行加密: String-》byte[]-》加密byte[]
解密: 加密byte[]-》byte[]-》String

解决方案 »

  1.   

    得到的byte[]不等于ee, 你是指他们的值不相等,还是什么?我试的结果是相等的(因为两个过程用的都是默认的字符集)。如果,你是指地址不相等,那是肯定的。
      

  2.   

    实际问题是这样的:我做的加密程序是针对我的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);//解密
      }
    }
      

  3.   

    另外,这种形势的加密解密就象websphere的admin.config文件的数据库连接密码一样的形式----都是为了让类似密码的数据不被看到
      

  4.   

    http://expert.csdn.net/Expert/topic/1440/1440515.xml?temp=.1720392
    已经解决!