首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

加密后字符串在文件中怎么读取?

2012-02-24 
加密后字符串在文件中如何读取????急急我从网上看了一个加密解密的程序感觉不错想应用,但是我的字符串都是

加密后字符串在文件中如何读取????急急
我从网上看了一个加密解密的程序感觉不错想应用,但是我的字符串都是存在文件中的(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();
                    }

     
    }


[解决办法]
建议你不要对加密和解密后的字节码进行转换,直接用ByteArrayOutputStream进行在存储,
读取时用ByteArrayInputStream处理应该不会有问题。
[解决办法]
同意 iisbocai(波菜) 的观点,你加密不就为了让人不知道内容么?字节码数组就字节码数组,你把它直接用ByteArrayOutputStream进行存储有什么问题么?
[解决办法]
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();
}

热点排行