zoukankan      html  css  js  c++  java
  • 字串加密

    古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报

    import java.util.Scanner;

    public class CaeserCipher {

     private String SS; // 定义密钥字母表 

     private int key; // 定义密钥key 

     public CaeserCipher(String SS, int key) {

      // 根据不同的字母表和不同的密钥生成一个新的凯撒算法,达到通用的目的     

    super();//调用父类的初始化方法,其实就是调用父类中的public CaeserCipher()方法    

     this.SS = SS;    this.key = key;   

     } 

     public String cc(String from) {  

     //凯撒加密算法,传入明文字符串,返回一个密文字符串  

      String to = "";    

     for (int i = 0;i < from.length(); i++)   

    {   

         to += SS.charAt((SS.indexOf(from.charAt(i))+key)%SS.length()); 

      }       

     return to;   

     }   

     public static void main(String[] args) {

           CaeserCipher caeser = new  CaeserCipher("ABCDEFHIJKLMNOPQRSTUVWXZ", 3);  

           Scanner scanner = new Scanner(System.in);//构造函数,对参数进行初始化   

           System.out.println("请输入要加密的字符串"); 

           String str =scanner.nextLine(); //输入字符串 security 

           String result = caeser.cc(str); //调用加密方法进行加密   

           System.out.print(result); // 可得结果

      } 

    }

      

  • 相关阅读:
    Vuex的使用
    vue的props属性,vue的插槽
    ES6 Promise对象
    ES6 Map对象以及Set对象
    函数作用域以及块级作用域
    组件之间的传值-$refs&$parent
    Vue中父子组件的传值
    v-on 以及v-model的修饰符以及vue的常用指令
    时间线
    readline和xreadline的区别
  • 原文地址:https://www.cnblogs.com/1995-qxl/p/4904201.html
Copyright © 2011-2022 走看看