我从网上看了一个加密解密的程序感觉不错想应用,但是我的字符串都是存在文件中的(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();
}
}
加密解密源代码:
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();
}
}
读取时用ByteArrayInputStream处理应该不会有问题。
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();
}