使用java创建凯撒密码方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20206890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a caesar cipher method using java
提问by WeekzGod
I want to create a method that will use a phrase and a number taken from args. The number form args will shift the letters of the phrase from args the amount of letters the number is.
我想创建一个方法,该方法将使用从 args 中获取的短语和数字。数字形式 args 会将短语的字母从 args 移动到数字的字母数量。
example javac Caesar.java java Caesar abcd 1
示例 javac Caesar.java java Caesar abcd 1
the end result should print bcde
最终结果应该打印 bcde
my method is giving me problems.. help?
我的方法给我带来了问题..帮助?
private String encode(String num, int x)
{
char[] charnum = args[1];
for (x = 0; x <= charnum.length; x++)
{
charnum = charnum + x;
}
return new String(charnum);
}
What do I do? Similarly i have to write a decoder method. I was going to have the same set up except the effects of the for loop change to
我该怎么办?同样,我必须编写一个解码器方法。除了 for 循环更改为的效果之外,我将进行相同的设置
charnum = charnum - x;
My problem is that when I try to compile, I get the following errors
我的问题是,当我尝试编译时,出现以下错误
symbol : variable argslocation: class Lab041
char[] charnum = args[1];
^
Lab041.java:17: operator + cannot be applied to char[],int
charnum = charnum + x;
^
2 errors
how do i fix it? and is my assumption for the decode method correct?
我如何解决它?我对解码方法的假设是否正确?
采纳答案by Elliott Frisch
I would start with a simpler encode method, like this
我会从一个更简单的编码方法开始,像这样
private static String encode(String num, int x)
{
StringBuilder sb = new StringBuilder();
for (char c : num.toCharArray()) {
sb.append((char) (c + x));
}
return sb.toString();
}
Then to use it, I'd use a main method like this
然后要使用它,我会使用这样的主要方法
public static void main(String[] args)
{
if (args == null || args.length < 2) {
System.out.println("Not enough arguments.");
System.exit(1);
}
int val = Integer.valueOf(args[args.length - 1]);
for (int i = 0; i < args.length - 1; i++) {
if (i != 0) {
System.out.print(' ');
}
System.out.print(encode(args[i], val));
}
System.out.println();
}
回答by Anubian Noob
Charnum is a character array. You want to do:
Charnum 是一个字符数组。你想做:
for (i = 0; i <= charnum.length; i++) {
charnum[i] = (char) (charnum[i] + x);
}
Which will refer to an element of that array.
这将引用该数组的一个元素。
Also keep in mind that you don't have safe guards for if you go over 'z'
还要记住,如果你超过“z”,你就没有安全措施
Also check out this: Simple caesar cipher in java
另请查看:Java 中的简单凯撒密码
回答by gerrytan
java cmd line args is a string array, you can't simply assign it to a char array, instead String class has a method toCharArray()
java cmd line args 是一个字符串数组,你不能简单地将它分配给一个字符数组,而是 String 类有一个方法 toCharArray()
Also make use of the fact char data type can be manipulated by addition operator, eg:
还利用事实 char 数据类型可以通过加法运算符进行操作,例如:
char c = 'a';
c = (char)(c + 1); // c is now 'b'
回答by pavithra
class CaesarCipher {
类凯撒密码{
private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public String encrypt(String plainText,int shiftKey)
{
plainText = plainText.toLowerCase();
String cipherText="";
for(int i=0;i<plainText.length();i++)
{
int charPosition = ALPHABET.indexOf(plainText.charAt(i));
int keyVal = (shiftKey+charPosition)%26;
char replaceVal = this.ALPHABET.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
public String decrypt(String cipherText, int shiftKey)
{
cipherText = cipherText.toLowerCase();
String plainText="";
for(int i=0;i<cipherText.length();i++)
{
int charPosition = this.ALPHABET.indexOf(cipherText.charAt(i));
int keyVal = (charPosition-shiftKey)%26;
if(keyVal<0)
{
keyVal = this.ALPHABET.length() + keyVal;
}
char replaceVal = this.ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
return plainText;
}
}
}