凯撒加密

凯撒加密:

通过移动字节的位置进行加密

  public static void main(String[] args) {
    String input="hello world";
    //向右移动
    int key =3;

    //转换字节数组
      char[] chars=input.toCharArray();
      //装密文
      StringBuilder sb = new StringBuilder();
      for (char c :chars ) {
          int d=c;
          d = d+key;
          char newd= (char) d;
          sb.append(newd);

      }
    System.out.println("密文:"+sb.toString());

      //反转
      StringBuilder bs = new StringBuilder();
      String output =sb.toString();
      char[] outchars=output.toCharArray();
      for (char c : outchars) {
          int d=c;
          d= d-key;
          char newd= (char) d;
          bs.append(newd);

      }
    System.out.println("明文:" + bs.toString());
  }

  

posted @ 2020-06-25 20:31  Money131  阅读(159)  评论(0编辑  收藏  举报