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); // 可得结果

      } 

    }

      

  • 相关阅读:
    Python-05知识-01Python优缺点
    Python-02进阶-06代码优化工具
    Python-02进阶-04多进程多线程
    Python-02进阶-03生成器
    Python-02进阶-02装饰器
    Python-01基础-13功能模块
    Python-01基础-12常用命令
    Python-01基础-11基础知识
    console.dir有很多浏览器,系统的兼容性问题,不要随便用!
    微信支付-签名错误
  • 原文地址:https://www.cnblogs.com/1995-qxl/p/4904201.html
Copyright © 2011-2022 走看看