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);
    }
    }
    }

    结果截图:

  • 相关阅读:
    重构手法之简化函数调用【5】
    netstat命令
    Python使用wxpy模块实现微信两两群组消息同步
    format函数格式化显示的方法
    scrapy介绍及使用
    Linux常用命令复习
    Django实现博客项目
    Django中CKEditor富文本编译器的使用
    Django-admin站点管理的详细使用
    电脑修改密码后,git push 报错unable to access
  • 原文地址:https://www.cnblogs.com/chengez/p/4898579.html
Copyright © 2011-2022 走看看