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

    }

    }

    截图:

  • 相关阅读:
    2016.01.04接触spring一年开始读spring源码
    hibernate 各历史版本下载 spring各历史版本下载
    mongodb 安装使用遇到的问题记录
    EmEditor处理大文本文件
    linux的常用易忘命令
    签名的html
    添加用户-查看用户列表-禁止默认root登陆
    今天领导分享了一个探测端口的命令-linux下提示bash:command not found
    【原创】java 获取十个工作日之前或之后的日期(算当天)-完美解决-费元星
    Oracle 完全理解connect by-详细脚本-可实战
  • 原文地址:https://www.cnblogs.com/jingjing0629/p/4908024.html
Copyright © 2011-2022 走看看