zoukankan      html  css  js  c++  java
  • 凯撒加密(key为3)

    基本思想:

    加密是把字符串中的每一个字符+3解密是每一个字符-3

    源代码:

    package demo;
    import java.util.Scanner;

    class Encryption{
    String a;
    public void encryption(String q){
    String string= "";
    for(int i = 0;i < q.length();i++)
    {
    char b=q.charAt(i);
    if(b >= 'a'&&b <='z')
    {
    b += 3;
    if(b > 'z')
    b-=26;
    if(b < 'a')
    b=+26;
    }
    else if(b >= 'A'&&b <= 'Z')
    {
    b+=3;
    if(b > 'Z')
    b-=26;
    if(b < 'A')
    b+=26;
    }
    string += b;
    }
    System.out.println("加密后为:"+string);
    }

    }
    class Decryption{
    String a;
    public void decryption(String q){
    String string= "";
    for(int i = 0;i < q.length();i++)
    {
    char b=q.charAt(i);
    if(b >= 'a'&&b <='z')
    {
    b -= 3;
    if(b > 'z')
    b-=26;
    if(b < 'a')
    b+=26;
    }
    else if(b >= 'A'&&b <= 'Z')
    {
    b-=3;
    if(b > 'Z')
    b-=26;
    if(b < 'A')
    b+=26;
    }
    string += b;
    }
    System.out.println("解密后为:"+string);
    }

    }
    public class Password{
    public static void main(String[] args){
    System.out.println("a。加密 b.解密");
    Scanner choose = new Scanner(System.in);
    String a = choose.nextLine();
    if(a.equalsIgnoreCase("a"))
    {
    Scanner c = new Scanner(System.in);
    System.out.println("请输入明文:");
    String b = c.nextLine();
    Encryption en = new Encryption();
    en.encryption(b);
    }
    else if(a.equalsIgnoreCase("b")) // equals 用来比较两个对象所表示的字符是否相同
    { // equalsIgnoreCase 用来比较对象所代表的字符与括号中字符是否相同
    Scanner c = new Scanner(System.in);
    System.out.println("请输入密码:");
    String b = c.nextLine();
    Decryption de = new Decryption();
    de.decryption(b);
    }
    }
    }

    结果截图:

  • 相关阅读:
    盘的转——使用缓动函数完成动画
    espnet环境配置(window)
    2021.9.8 Hadoop集群
    2021.9.7 开学第一课
    RS-422与RS-485
    70 进程创建的优化设计 下
    RGB液晶接口
    matlab2018a安装激活教程
    sed初级教程
    centos无法添加默认网关
  • 原文地址:https://www.cnblogs.com/chengez/p/4898579.html
Copyright © 2011-2022 走看看