Java,如何实现Shift密码(Caesar Cipher) [英] Java, How to implement a Shift Cipher (Caesar Cipher)

查看:168
本文介绍了Java,如何实现Shift密码(Caesar Cipher)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我收到这个错误:


我想要实现一个Caesar密码移位来增加一个字符串中的每个字母。

 可能的精度损失需要char;发现int 

这是我的代码到目前为止:

  import java.util.Scanner; 
import java.io. *;

public class CaesarCipher
{
public static void main(String [] args){

char [] letters = {'a' b','c','d','e','f','g','h','i','j',
'k' ,'o','o','p','q','r','s','t','u','v',
'w','x' y','z'};

char [] message =onceuponatime.toCharArray();
char [] eMessage = new char [message.length];
char shift = 3;

//加密消息
for(int i = 0; i< = message.length; ++ i)
{
eMessage [i] =( message [i] + shift)%(char)letters.length;
System.out.println(x);
}
}
}

是什么原因导致此错误?如何实现一个caesar Cipher shift来增加一个字符串中的每个字母3?

解决方案

Java Shift Caesar Cipher by code> shift 空格



限制:


  1. 只能在shift参数中使用正数。

  2. 仅适用于小于26的班次。

  3. 是否会将电脑倒塌的文本长度超过几千个字符

  4. 将字符转换为字符,所以它会失败,除了ascii字母。

  5. 只允许字母a到z。不能处理空格,数字,符号或unicode。

  6. 代码违反了DRY(不要重复自己)的原则,重复计算超过它。

伪代码


  1. 字符串中的字符。

  2. 向字符添加转移,如果它从字母表的末尾掉落,则从字母表中的字母数减去shift(26)

  3. 如果转换不会使字符从字母表的尾部脱落,则将该位移添加到字符。

  4. 将字符附加到新字符串上。返回字符串。

功能:

  String cipher(String msg,int shift){
String s =;
int len = msg.length(); (int x = 0; x char c =(char)(msg.charAt(x)+ shift);

if(c>'z')
s + =(char)(msg.charAt(x) - (26-shift));
else
s + =(char)(msg.charAt(x)+ shift);
}
return s;
}

如何调用:

  System.out.println(cipher(abc,3)); // print def 
System.out.println(cipher(xyz,3)); //打印abc


I want to implement a Caesar Cipher shift to increase each letter in a string by 3.

I am receiving this error:

possible loss of precision required char; found int

Here is my code so far:

import java.util.Scanner;
import java.io.*;

public class CaesarCipher
{
    public static void main (String [] args) {

        char[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
            'w', 'x', 'y', 'z'};

        char[] message = "onceuponatime".toCharArray();
        char[] eMessage = new char[message.length];
        char shift = 3;

        //encrypting message
        for(int i = 0; i <= message.length; ++i)
        {
            eMessage[i] = (message[i] + shift) % (char) letters.length;
            System.out.println(x);               
        }              
    }
}

What causes this error? How can I implement a caesar Cipher shift to increase each letter in a string by 3?

解决方案

Java Shift Caesar Cipher by shift spaces.

Restrictions:

  1. Only works with a positive number in the shift parameter.
  2. Only works with shift less than 26.
  3. Does a += which will bog the computer down for bodies of text longer than a few thousand characters.
  4. Does a cast number to character, so it will fail with anything but ascii letters.
  5. Only tolerates letters a through z. Cannot handle spaces, numbers, symbols or unicode.
  6. Code violates the DRY (don't repeat yourself) principle by repeating the calculation more than it has to.

Pseudocode:

  1. Loop through each character in the string.
  2. Add shift to the character and if it falls off the end of the alphabet then subtract shift from the number of letters in the alphabet (26)
  3. If the shift does not make the character fall off the end of the alphabet, then add the shift to the character.
  4. Append the character onto a new string. Return the string.

Function:

String cipher(String msg, int shift){
    String s = "";
    int len = msg.length();
    for(int x = 0; x < len; x++){
        char c = (char)(msg.charAt(x) + shift);
        if (c > 'z')
            s += (char)(msg.charAt(x) - (26-shift));
        else
            s += (char)(msg.charAt(x) + shift);
    }
    return s;
}

How to invoke it:

System.out.println(cipher("abc", 3));  //prints def
System.out.println(cipher("xyz", 3));  //prints abc

这篇关于Java,如何实现Shift密码(Caesar Cipher)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆