我从网上看了一个加密解密的程序感觉不错想应用,但是我的字符串都是存在文件中的(System.ini)共四行,要求一行一行存,一行行读。但是那个加密解密的程序中加密后生成的是字节码数组,按我的做法怎么都是出错请各位帮帮忙教一下我到底如何才能把加密后的文件正确存入文件,并正确从文件中取出?请给出详细源代码
加密解密源代码:
package bestteam;
import   java.security.*;  
import   javax.crypto.*;
import   java.io.*;
public   class  Crypt{
      private static String Algorithm = "DES";    
          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();  
                  return   deskey.getEncoded();
          }
          public   static   byte[]   encode(byte[]   input,   byte[]   key)
                  throws   Exception
          {
     SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm);
          Cipher   c1   =   Cipher.getInstance(Algorithm);
                  c1.init(Cipher.ENCRYPT_MODE,   deskey);
                  byte[]   cipherByte   =   c1.doFinal(input);
                  return   cipherByte;
          }          public   static   byte[]   decode(byte[]   input,   byte[]   key)
                  throws   Exception
          {
     SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key,Algorithm);
                  Cipher   c1   =   Cipher.getInstance(Algorithm);
                  c1.init(Cipher.DECRYPT_MODE,   deskey);
                  byte[]   clearByte   =   c1.doFinal(input);
                    return   clearByte;
          }
          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();
          }   
  }

解决方案 »

  1.   

    建议你不要对加密和解密后的字节码进行转换,直接用ByteArrayOutputStream进行在存储,
    读取时用ByteArrayInputStream处理应该不会有问题。
      

  2.   

    字节码数组可以转换成String 的啊
      

  3.   

    同意 iisbocai(波菜) 的观点,你加密不就为了让人不知道内容么?字节码数组就字节码数组,你把它直接用ByteArrayOutputStream进行存储有什么问题么?
      

  4.   

    能不能给出 ByteArrayInputStream读文件的详细实例代码?
      

  5.   

    public static void main(String[] args) throws Exception {
            System.out.println("Hello World!");
            InputStream is = new FileInputStream("c:/1.txt");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] bf = new byte[1024];
            int len = 0;
            while ((len = is.read(bf)) > 0) {
                baos.write(bf, 0, len);
            }
            is.close();
            FileOutputStream os = new FileOutputStream("c:/2.txt");
            baos.writeTo(os);    }    public static void main(String[] args) {
            byte[] inputbytes = { 'a', 'b', 'c', 'd', 'e'};
            ByteArrayInputStream in = new ByteArrayInputStream(inputbytes);        System.out.println("Available: " + in.available());
            int b;        while ((b=in.read()) >= 0)        // reads "abcde"
                System.out.print((char)b);
            in.reset();
            System.out.println();        in.skip(3);                       // skip "abc"        while ((b=in.read()) >= 0)        // reads "de"
                System.out.print((char)b);
            System.out.println();
        }
      

  6.   

    晕我要的是怎么把已经写成的字节码文件在java中以byte数组返回