小动作

作者: 实在想不出昵称丶 | 来源:发表于2016-11-15 11:34 被阅读1次

    -----好无聊,得写点什么...

    168023916108376898.jpg

    正在学java,然后就有了一堆堆的烂想法,周末小做了一个加密小程序,写下来记录记录。

    简单的加密,初始想到的是(在某本书中曾看过的)将26个字母的顺序改变,即:

    public class Hello{
     public static void main(String[] args){
       int password=args[0];
       int p;
       String s=args[1];
       for(int i=0;i<s.length();i++){
           char c =s.charAt(i);//从字符串中获得每个字符
           p=password%26;
            c+=p;
           if(c<'A'||c>'z'){
             c-=26; 
          }  
            System.out.print(c);
        }
        System.out.print("\n");
     }
    }
    /**out:java Hello 4 hello
    *>>lipps
    *java Hello -4 lipps
    *>>hello
    *java Hello 4 们
    *>>仰
    *java Hello -4 仰
    *>>们
    */
    

    然后就往好看点包装。

    import java.io.*;
    public class Hello{
      private static int Password=4;
      private static final String Suffix=".enc";
      public void doEncrypt(String path){
        int dex=path.lastIndexOf(Suffix);
        FileInputStream in=null;
        FileOutputStream out=null;
        try{
          int temp=0;
          String name=path.substring(0,dex);
          File inFile=new File(path);
          File outFile=new File(name);
          in =new FileInputStream(inFile);
          out =new FileOutputStream(outFile);
          while((temp=in.read())!=-1){
            char c=(char)temp;
            c+=((-Password)%26);
            temp=(int)c;
            out.write(temp);
          }
        }
        catch(FileNotFoundException e){
          System.out.println("文件未找到,请重试");
        }
        catch(IOException e){
          System.out.println("读取错误");
        }
        finally {
          try{
            if(in!=null){
              in.close();
            }
            if(out!=null){
              out.close();
            }
          }
          catch(IOException e){
            System.out.println("关闭失败");
          }
        }
    
      }
      public void doDecrypt(String path){
        int dex=path.lastIndexOf(Suffix);
        FileInputStream in=null;
        FileOutputStream out=null;
        try{
          int temp=0;
          String name=path.substring(0,dex);
          File inFile=new File(path);
          File outFile=new File(name);
          in =new FileInputStream(inFile);
          out =new FileOutputStream(outFile);
          while((temp=in.read())!=-1){
            char c=(char)temp;
            c+=(Password%26);
            temp=(int)c;
            out.write(temp);
          }
        }
        catch(FileNotFoundException e){
          System.out.println("文件未找到,请重试");
        }
        catch(IOException e){
          System.out.println("读取错误");
        }
        finally {
          try{
            if(in!=null){
              in.close();
            }
            if(out!=null){
              out.close();
            }
          }
          catch(IOException e){
            System.out.println("关闭失败");
          }
        }
    
      }
      public static void main(String[] args){
        Encrypt e=new Encrypt();
        if(args.length!=2){
          System.out.println("错误,实例:java Hello Encrypt E:\秘密.txt");
          return;
        }
        if(args[0].equalsIgnoreCase("Encrypt")){
          e.doEncrypt(args[1]);
        }
        if(args[0].equalsIgnoreCase("Decrypt")){
          e.doDecrypt(args[1]);
        }
      }
    }
    /**out:
    *加密:
    *java Hello Encrypt E:\秘密.txt
    *解密
    *java Hello decrypt E:\秘密.txt.enc
    */
    
    

    明天再试着做个界面。
    问题:1.当我把args【1】作为password时,老是出错。
    于是就想:main是static修饰的,如果在main里定义password,并将args【1】赋值给password,然后在其他方法里直接调用是否可以?(明天试试!!)
    2.如果在文件输出的时候,不换个文件输出,文件就会变空白(觉着这个问题很白痴,但是看着书才自学了一点点儿,基础不行嘛);

    清醒小刻
    没错,我就是抄书的!!!

    相关文章

      网友评论

        本文标题:小动作

        本文链接:https://www.haomeiwen.com/subject/qpdkpttx.html