
package file_java2123; import java.io.*; public class ByteArrayStream { //密文 static char[] password={'T','O','I','A','N','D','E','G','H','Z','B','K','F','J','M','C','L','P','Y','V','X','Q','R','W','U','S'}; //加密 public void encrypt(InputStream in,OutputStream out){ try{ int c=0; int ec=0; while((c=in.read())!=-1){ if(c>='a'&&c<='z'){ ec=password[c-'a']+32; } else if(c>='A'&&c<='Z'){ ec=password[c-'A']; //System.out.println("password:"+ec); } else ec=c; out.write(ec); } } catch(IOException ioe){ ioe.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } } //解密 public void dencrypt(InputStream in,OutputStream out){ try{ int c=0; int ec=0; int i=0; while((c=in.read())!=-1){ i=0; while((c!=password[i])&&(c!=(password[i]+32))){ i++; } if(c>='a'&&c<='z') ec='a'+i; else if(c>='A'&&c<='Z') ec='A'+i; else ec=c; out.write(ec); } } catch(IOException ioe){ ioe.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) { try{ File f1=new File("t.txt"); FileOutputStream fos=new FileOutputStream(f1); String info="ABCDEFGHIJKabcdefghijk"; System.out.println("原始字符串:"+info); ByteArrayInputStream bais=new ByteArrayInputStream(info.getBytes()); ByteArrayOutputStream baos=new ByteArrayOutputStream(); ByteArrayStream bas=new ByteArrayStream(); //加密 bas.encrypt(bais, baos); String info2=baos.toString(); System.out.println("加密后的字符串:"+info2); fos.write(info2.getBytes()); bais.close(); baos.close(); //解密 bais=new ByteArrayInputStream(info2.getBytes()); baos=new ByteArrayOutputStream(); bas.dencrypt(bais, baos); String info3=baos.toString(); System.out.println("解密后字符串:"+info3); bais.close(); baos.close(); } catch(IOException ioe){ ioe.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } } }

package file_java2123; import java.io.*; public class ByteArrayStream { //密文 static char[] password={'T','O','I','A','N','D','E','G','H','Z','B','K','F','J','M','C','L','P','Y','V','X','Q','R','W','U','S'}; //加密 public void encrypt(InputStream in,OutputStream out){ try{ int c=0; int ec=0; while((c=in.read())!=-1){ if(c>='a'&&c<='z'){ ec=password[c-'a']+32; } else if(c>='A'&&c<='Z'){ ec=password[c-'A']; //System.out.println("password:"+ec); } else ec=c; out.write(ec); } } catch(IOException ioe){ ioe.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } } //解密 public void dencrypt(InputStream in,OutputStream out){ try{ int c=0; int ec=0; int i=0; while((c=in.read())!=-1){ i=0; while((c!=password[i])&&(c!=(password[i]+32))){ i++; } if(c>='a'&&c<='z') ec='a'+i; else if(c>='A'&&c<='Z') ec='A'+i; else ec=c; out.write(ec); } } catch(IOException ioe){ ioe.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) { try{ String info="YZED"; System.out.println("原始字符串:"+info); ByteArrayInputStream bais=new ByteArrayInputStream(info.getBytes()); ByteArrayOutputStream baos=new ByteArrayOutputStream(); ByteArrayStream bas=new ByteArrayStream(); //加密 bas.encrypt(bais, baos); String info2=baos.toString(); System.out.println("加密后的字符串:"+info2); bais.close(); baos.close(); //解密 bais=new ByteArrayInputStream(info2.getBytes()); baos=new ByteArrayOutputStream(); bas.dencrypt(bais, baos); String info3=baos.toString(); System.out.println("解密后字符串:"+info3); bais.close(); baos.close(); } catch(IOException ioe){ ioe.printStackTrace(); } catch(Exception e){ e.printStackTrace(); } } }
把密码存在文件中