我也正在做这个,现在做了一个自己的简单算法,只能保存成二进制的,保存成字符串再转回来就乱码,还没找到解决办法.你有什么好东西也发给我一份呀.
不过java本身的保密性能也不好,别人如果从源码入手又怎么好呢?
import java.io.*;
class xiao2
{
private static String keyword="中文加密码pfq";
private String orgstr="this is the test!";
public String encrypt(String instr)
{
try{
byte key[]=keyword.getBytes();
byte ins[]=instr.getBytes();
int keyindex=key.length;
int insindex=ins.length;
byte outs[]=new byte[insindex];
for (int i=0;i<insindex ;i++ )
{
int index=i%keyindex;
int k=ins[i]+key[index];
if(k>255)
k=k-255;
outs[i]=(byte)k;
}
String outstr=new String(outs);
return outstr;
}catch(Exception e)
{
return instr;
}
}
public String uncrypt(String instr)
{
try{
byte key[]=keyword.getBytes();
byte ins[]=instr.getBytes();
int keyindex=key.length;
int insindex=ins.length;
byte orgs[]=new byte[insindex];
for (int i = 0 ;i<insindex ;i++ )
{
int index=i%keyindex;
int k=ins[i]-key[index];
if(k<0)
k=k+256;
orgs[i]=(byte)k;
}
String orgstr=new String(orgs);
return orgstr;
}catch(Exception e)
{
return instr;
}
}
public static void main(String[] args) throws UnsupportedEncodingException
{
String str1="this 中国人,i<>>&@page toewt中国民国s the first test string!";
System.out.println(str1);
xiao2 xx=new xiao2();
str1=xx.encrypt(str1);
System.out.println(str1);
str1=xx.uncrypt(str1);
System.out.println(str1);
}
}
不过java本身的保密性能也不好,别人如果从源码入手又怎么好呢?
import java.io.*;
class xiao2
{
private static String keyword="中文加密码pfq";
private String orgstr="this is the test!";
public String encrypt(String instr)
{
try{
byte key[]=keyword.getBytes();
byte ins[]=instr.getBytes();
int keyindex=key.length;
int insindex=ins.length;
byte outs[]=new byte[insindex];
for (int i=0;i<insindex ;i++ )
{
int index=i%keyindex;
int k=ins[i]+key[index];
if(k>255)
k=k-255;
outs[i]=(byte)k;
}
String outstr=new String(outs);
return outstr;
}catch(Exception e)
{
return instr;
}
}
public String uncrypt(String instr)
{
try{
byte key[]=keyword.getBytes();
byte ins[]=instr.getBytes();
int keyindex=key.length;
int insindex=ins.length;
byte orgs[]=new byte[insindex];
for (int i = 0 ;i<insindex ;i++ )
{
int index=i%keyindex;
int k=ins[i]-key[index];
if(k<0)
k=k+256;
orgs[i]=(byte)k;
}
String orgstr=new String(orgs);
return orgstr;
}catch(Exception e)
{
return instr;
}
}
public static void main(String[] args) throws UnsupportedEncodingException
{
String str1="this 中国人,i<>>&@page toewt中国民国s the first test string!";
System.out.println(str1);
xiao2 xx=new xiao2();
str1=xx.encrypt(str1);
System.out.println(str1);
str1=xx.uncrypt(str1);
System.out.println(str1);
}
}
xiao2 xx=new xiao2();
str1=xx.encrypt(str1);
(2)System.out.println(str1);
str1=xx.uncrypt(str1);
(3)System.out.println(str1);第(1) 句输出。
(2),(3)句不输出?
import java.io.*;
class xiao
{
private static String keyword="我高兴起来就加工工业 密它";
private String orgstr="this is the test!";
public byte[] encrypt(String instr)
{
byte key[]=keyword.getBytes();
byte ins[]=instr.getBytes();
int keyindex=key.length;
int insindex=ins.length;
byte outs[]=new byte[2*insindex];
for (int i=0;i<insindex ;i++ )
{
int index=i%keyindex;
int k=ins[i]+key[index];
int d=(int)(255*Math.random());
if(k>255)
k=k-255;
outs[2*i]=(byte)k;
outs[2*i+1]=(byte)d;
}
return outs;
}
public String uncrypt(byte[] b)
{
byte key[]=keyword.getBytes();
byte ins[]=b;
int keyindex=key.length;
int insindex=ins.length;
byte orgs[]=new byte[insindex/2];
for (int i = 0 ;i<insindex ;i+=2 )
{
int index=(i/2)%keyindex;
int k=ins[i]-key[index];
if(k<0)
k=k+256;
orgs[i/2]=(byte)k;
}
String orgstr=new String(orgs);
return orgstr;
}
public static void main(String[] args)
{
String str1="this 中国人,i<>>&@page toewt中国民国s the first test string!";
System.out.println(str1);
xiao xx=new xiao();
byte b1[]=xx.encrypt(str1);
str1=new String(b1);
System.out.println(str1);
str1=xx.uncrypt(b1);
System.out.println(str1);
}
}
应该是没有问题了,我准备给文件加一下试试。。
-------------------------------
//fileencrypt.java import java.io.*;
public class fileencrypt{
private static String keyword="我高兴起来就加工工业 密它";
private String orgstr="this is the test!";
public fileencrypt(String fin,String fout){
String strMsgReturn = "";
try
{
File file = new File(fin);
byte buff[] = new byte[(int)file.length()];
InputStream in = new FileInputStream(file);
in.read(buff);
String srcdata=new String(buff);
buff = encrypt(srcdata);
File fileout=new File(fout);
FileOutputStream out=new FileOutputStream(fileout);
ObjectOutputStream o=new ObjectOutputStream(out);
o.write(buff);
o.flush();
in.close();
out.close();
o.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return;
}
public byte[] encrypt(String instr)
{
byte key[]=keyword.getBytes();
byte ins[]=instr.getBytes();
int keyindex=key.length;
int insindex=ins.length;
byte outs[]=new byte[2*insindex];
for (int i=0;i<insindex ;i++ )
{
int index=i%keyindex;
int k=ins[i]+key[index];
int d=(int)(255*Math.random());
if(k>255)
k=k-255;
outs[2*i]=(byte)k;
outs[2*i+1]=(byte)d;
}
return outs;
}
public static void main(String args[])
{
new fileencrypt("f:\\ok.txt","f:\\okencrypt.txt");
}
}
-------------------------
//fileuncrypt.java
import java.io.*;
public class fileuncrypt{
private static String keyword="我高兴起来就加工工业 密它";
private String orgstr="this is the test!";
public fileuncrypt(String fin,String fout){
String strMsgReturn = "";
try
{
File file = new File(fin);
byte buff[] = new byte[(int)file.length()];
InputStream in = new FileInputStream(file);
in.read(buff);
String srcdata;
srcdata= uncrypt(buff);
File fileout=new File(fout);
FileOutputStream out=new FileOutputStream(fileout);
OutputStreamWriter o=new OutputStreamWriter(out);
o.write(srcdata);
o.flush();
in.close();
out.close();
o.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return;
}
public String uncrypt(byte[] b)
{
byte key[]=keyword.getBytes();
byte ins[]=b;
int keyindex=key.length;
int insindex=ins.length;
byte orgs[]=new byte[insindex/2];
for (int i = 0 ;i<insindex ;i+=2 )
{
int index=(i/2)%keyindex;
int k=ins[i]-key[index];
if(k<0)
k=k+256;
orgs[i/2]=(byte)k;
}
String orgstr=new String(orgs);
return orgstr;
}
public static void main(String args[])
{
new fileuncrypt("f:\\okencrypt.txt","f:\\okuncrypt.txt");
}
}
//fileencrypt.java
import java.io.*;
public class fileencrypt{
private static String keyword="我高兴起来就加工工业 密它";
private String orgstr="this is the test!";
public fileencrypt(String fin,String fout){
String strMsgReturn = "";
try
{
File file = new File(fin);
byte buff[] = new byte[(int)file.length()];
InputStream in = new FileInputStream(file);
in.read(buff);
String srcdata=new String(buff);
System.out.print(srcdata);
buff = encrypt(srcdata);
File fileout=new File(fout); FileOutputStream out=new FileOutputStream(fileout); out.write(buff);
out.flush();
in.close();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return;
}
public byte[] encrypt(String instr)
{
byte key[]=keyword.getBytes();
byte ins[]=instr.getBytes();
int keyindex=key.length;
int insindex=ins.length;
byte outs[]=new byte[2*insindex];
for (int i=0;i<insindex ;i++ )
{
int index=i%keyindex;
int k=ins[i]+key[index];
int d=(int)(255*Math.random());
if(k>255)
k=k-255;
outs[2*i]=(byte)k;
outs[2*i+1]=(byte)d;
}
return outs;
}
public static void main(String args[])
{
new fileencrypt("D:\\11.txt","D:\\test1.txt");
}
}
//fileuncrypt.java
import java.io.*;
public class fileuncrypt{
private static String keyword="我高兴起来就加工工业 密它";
private String orgstr="this is the test!";
public fileuncrypt(String fin,String fout){
String strMsgReturn = "";
try
{
File file = new File(fin);
byte buff[] = new byte[(int)file.length()];
InputStream in = new FileInputStream(file);
in.read(buff);
String srcdata;
srcdata= uncrypt(buff);
File fileout=new File(fout);
FileOutputStream out=new FileOutputStream(fileout);
OutputStreamWriter o=new OutputStreamWriter(out);
System.out.print(srcdata);
o.write(srcdata);
o.flush();
in.close();
out.close();
o.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return;
}
public String uncrypt(byte[] b)
{
byte key[]=keyword.getBytes();
byte ins[]=b;
int keyindex=key.length;
int insindex=ins.length;
byte orgs[]=new byte[insindex/2];
for (int i = 0 ;i<insindex ;i+=2 )
{
int index=(i/2)%keyindex;
int k=ins[i]-key[index];
if(k<0)
k=k+256;
orgs[i/2]=(byte)k;
}
String orgstr=new String(orgs);
return orgstr;
}
public static void main(String args[])
{
new fileuncrypt("D:\\test1.txt","d:\\test2.txt");
}
}