weixin_45710228 2019-10-04 08:18 采纳率: 0%
浏览 375
已采纳

java 问题,有没有大神知道凯撒密码怎么做?

写一个加密和解密的method,还有用户输入的密钥,密钥 +就是顺着移动(如果+2,就是则字母A将变为C,字母B将变为D),- 就是倒着移动。
1 是加密
2 是解密
下面是给出的代码
import java.util.Scanner;

public class CaesarCypher {

// Write an encode method

// Write a decode method



public static void main(String[] args) {    

    Scanner in = new Scanner(System.in);

    String message = "";




    System.out.println("Do you have a message to (1) encode or (2) decode");
    int option = in.nextInt();
    in.nextLine();

    if (option==1){


      System.out.println("Type your message to encode");
      message = in.nextLine();


    }

    if (option==2){

      System.out.println("Type your message to decode");

      message = in.nextLine(); 

    }


    }

}
  • 写回答

2条回答 默认 最新

  • threenewbee 2019-10-04 10:05
    关注
    import java.util.Scanner;
    
    public class CaesarCypher {
    
    // Write an encode method
    
    // Write a decode method
    
        public static String Decrypt(String str, int n) {
            // TODO Auto-generated method stub
            //解密
            int k=Integer.parseInt("-"+n);
            String string="";
            for(int i=0;i<str.length();i++) {
                char c=str.charAt(i);
                if(c>='a'&&c<='z')//如果字符串中的某个字符是小写字母
                {
                    c+=k%26;//移动key%26位
                    if(c<'a')
                        c+=26;//向左超界
                    if(c>'z')
                        c-=26;//向右超界
                }else if(c>='A'&&c<='Z')//如果字符串中的某个字符是大写字母
                {
                    c+=k%26;//移动key%26位
                    if(c<'A')
                        c+=26;//向左超界
                    if(c>'Z')
                        c-=26;//向右超界
                }
                string +=c;//将解密后的字符连成字符串
            }
            return string;
        }
    
        public static String Encryption(String str, int k) {
            // TODO Auto-generated method stub
            //加密
            String string="";
            for(int i=0;i<str.length();i++) {
                char c=str.charAt(i);
                if(c>='a'&&c<='z')//如果字符串中的某个字符是小写字母
                {
                    c+=k%26;//移动key%26位
                    if(c<'a')
                        c+=26;//向左超界
                    if(c>'z')
                        c-=26;//向右超界
                }else if(c>='A'&&c<='Z')//如果字符串中的某个字符是大写字母
                {
                    c+=k%26;//移动key%26位
                    if(c<'A')
                        c+=26;//向左超界
                    if(c>'Z')
                        c-=26;//向右超界
                }
                string +=c;//将解密后的字符连成字符串
            }
            return string;
        }
    
    
    public static void main(String[] args) {    
    
        Scanner in = new Scanner(System.in);
    
        String message = "";
    
        int key = 1; //你的密钥
    
    
        System.out.println("Do you have a message to (1) encode or (2) decode");
        int option = in.nextInt();
        in.nextLine();
    
        if (option==1){
    
    
          System.out.println("Type your message to encode");
          message = in.nextLine();
        String r = Encryption(message, key);
            System.out.println(r);
    
        }
    
        if (option==2){
    
          System.out.println("Type your message to decode");
    
          message = in.nextLine(); 
            String r = Decrypt(message, key);
            System.out.println(r);
    
        }
    
    
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常