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

    凯撒密码

    设计思想:加密过程是对小写字母的ASCLL码进行+3的操作,解密则与之相反。(xyz的加密则为abc,abc的解密则为xyz)

    流程图:

    源代码

    import java.util.Scanner;
     
    public class Password {
    public char Change(char a)
    {
        char c=0;
        if(a=='X')
            c='A';
        if(a=='Y')
            c='B';
        if(a=='Z')
            c='Z';
        if(a>='A'&&a<='W')
            c=(char) (a+3);
        return c;
             
    }
     
    public char jiemi(char a)
    {
        char c=0;
        if(a=='A')
            c='X';
        if(a=='B')
            c='Y';
        if(a=='C')
            c='Z';
        if(a>='D'&&a<='Z')
            c=(char) (a-3);
        return c;
             
    }
     
        public static void main(String[] args)
        {
                    String s1;
                    String s2="";
                    char c;
                    int w;
                    Scanner input=new Scanner(System.in);
                    Password p=new Password();
                    System.out.println("请选择1.加密2.解密");
                    w=input.nextInt();
            if(w==1)
            {
                        System.out.println("请输入一条消息:");
                        s1=input.next();
                        char a[]=s1.toCharArray();
                    for(int i=0;i<a.length;i++)
                    {
                        if(a[i]<'A'&&a[i]>'Z')
                        {
                            System.out.println("输入有误");
                            System.exit(0);
                        }
                        c=p.Change(a[i]);
                        s2=s2+c;
                         
                    }
            System.out.println("加密后的消息为:");
                   System.out.println(s2);
            }
            else
            {
                System.out.println("请输入要解密的句子");
                s1=input.next();
                char a[]=s1.toCharArray();
                for(int i=0;i<a.length;i++)
                {
     
                    if(a[i]<'A'&&a[i]>'Z')
                    {
                        System.out.println("输入有误");
                        System.exit(0);
                    }
                    c=p.jiemi(a[i]);
                    s2=s2+c;
                }
                System.out.println("解密后的消息为:");
                   System.out.println(s2);
            }
        }
     
    }
    

      

  • 相关阅读:
    校门外的树
    学生档案
    冒泡排序法
    寻找最大数序列
    初识结构体
    找零钱
    冒泡的应用
    关于数组的逆序重放
    关于质数
    字符串转换为日期格式
  • 原文地址:https://www.cnblogs.com/xiaojq/p/7744082.html
Copyright © 2011-2022 走看看