zoukankan      html  css  js  c++  java
  • 凯撒加密问题程序

    设计思想:加密的过程是将字母在字母表中的位置向后移两位,即A编程D,字符串+3,解密时字符串-3

    程序流程图:

    源代码:

    //王冶雯   凯撒问题加密和解密     关键点:加密是字符串+3,解密时-3

    package string11;

    import java.util.Scanner;

    public class jiami 

    {

    public static void main(String[] args)throws Exception

    {

    // TODO Auto-generated method stub

    System.out.println("[A 加密 ] [J 解密],Please Choose One");

    Scanner s=new Scanner(System.in);//创建Scanner对象

    String s1=s.nextLine();//获取本行字符串

    if (s1.equalsIgnoreCase("A"))//判断s1与A的大小

    {

    int key;

    System.out.println("请输入明文:");

    Scanner sc=new Scanner(System.in);

    String ss=sc.nextLine();

    System.out.println("请输入密钥:");

    Scanner scc=new Scanner(System.in);

    key=scc.nextInt();//将输入的字符转化成int型

    Encryption(ss,key);//调用Encryption方法

    }

    else if(s1.equalsIgnoreCase("J"));

    {

    int key;

    System.out.println("请输入密文:");

    Scanner sc=new Scanner(System.in);

    String ss =sc.nextLine();

    System.out.println("请输入密钥:");

    Scanner scc=new Scanner(System.in);

    key=scc.nextInt();

    Decrypt(ss,key);//调用Encryption方法

    }

    }

    //加密程序 

    public static void Encryption(String str,int t)

    {

    String string="";

    int i;

    char c;

    for(i=1;i<str.length();i++)

    {

    c=str.charAt(i);

    if(c>='a'&&c<='z')//如果字符中的某个字符是小写的

    {

    c+=t % 26;///移动26位

    if(c<'a')

    c+=26;//向左超界

    if(c>'z')

    c-=26;//向右超界

    }

    else if(c>='A'&&c<='Z')//如果字符中的某个字符是大写的

    {

    c+=t % 26;

    if(c<'A')

    c+=26;//向左超界

    if(c>'Z')

    c-=26;//向右超界

    }

    string +=c;//将加密后的字符连成字符串

    }

    System.out.println(str +"加密后为:" + string);

    }

    public static void Decrypt(String str,int n)

    {

    int t;

    t=Integer.parseInt("-" +n);

    String string="";

    int i;

    for(i=0;i<str.length();i++)

    {

    char c=str.charAt(i);

    if(c>='a'&&c<='z')//如果字符中的某个字符是小写的

    {

    c+=t % 26;//移动26位

    if(c<'a')

    c+=26;//向左超界

    if(c>'z')

    c-=26;//向右超界

    }

    else if(c>='A'&&c<='Z')//如果字符中的某个字符是大写的

    {

    c+=t % 26;

    if(c<'A')

    c+=26;//向左超界

    if(c>'Z')

    c-=26;//向右超界

    }

    string +=c;//将加密后的字符连成字符串

    }

    System.out.println(str +"解密后为:" + string);

    }

    }

    截图:

  • 相关阅读:
    struct
    python Hbase Thrift pycharm 及引入包
    python 性能鸡汤
    hbase 0.96 单机伪分布式配置文件及遇到的问题 find命令
    ubuntu下配置protobuf
    hadoop 2.2.0 eclipse 插件编译 及相关eclipse配置图解
    hadoop2.2.0 单机伪分布式(含64位hadoop编译) 及 eclipse hadoop开发环境搭建
    install ubuntu
    ubuntu wubi安装注意事项
    HBase eclipse开发环境搭建
  • 原文地址:https://www.cnblogs.com/jingjing0629/p/4908024.html
Copyright © 2011-2022 走看看