小动作

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

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.如果在文件输出的时候,不换个文件输出,文件就会变空白(觉着这个问题很白痴,但是看着书才自学了一点点儿,基础不行嘛);

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,080评论 19 139
  • 看了大半天,总算将https的整个握手过程都看的差不多了,之前网上看过很多资料,描述的都差不多,也让我对于整个过程...
    coder_yu阅读 2,318评论 1 15
  • 一、 与分身术训练营的结缘和相识: 2016年10月读到彭小六的《让未来现在就来---成为高效能的行动派》,当时确...
    lovegym2010阅读 680评论 4 0
  • 1、“动态”变成“简友圈”,起初我误以为是建立了一个类似群组的聊天功能。原来还是看动态,只不过说法改成简友圈。好,...
    金刚女王阅读 1,042评论 13 13
  • 曾经不能接受, 不能接受的是心碎, 不能接受自己的无能为力, 不能接受自己这么失败... 内心的怒火升起,不服气在...
    恒昌心语阅读 200评论 0 0