【发布时间】:2017-05-22 23:23:07
【问题描述】:
我有一个任务,我应该编写一个程序,该程序可以使用他们输入的“加密密钥”对用户输入的单词进行加密和解密。我想要做的是检查输入键中每个字母的值,与字母表相比(例如:a = 1,b = 2,c = 3),然后添加字母的值,然后用于移动他们分配的单词中的字符。 到目前为止,这是我的作业代码:
/* Validity Check from http://stackoverflow.com/questions/33965542/password-check-program-checking-capitals-lowercase-letters-digit-and-special
*
*
*
*
*/
import java.util.*;
public class SecretDecoderFinal
{
public static void main (String [] args)
{
optInput();
}
public static int optInput()
{
int opt = 0;
do {
String word = "";
String key = "";
System.out.println("Welcome to Seegson Security secure transmission terminal");
System.out.println("");
System.out.println("Enter an option for the terminal");
System.out.println("(1) to encrypt a word");
System.out.println("(2) to decrypt a word");
System.out.println("(0) to exit the terminal");
opt = In.getInt();
System.out.println("You selected: Option " +opt);
System.out.println("");
switch(opt)
{
case 1:
encryptWord(word, key);
break;
case 2:
decryptWord(word, key);
break;
case 0:
System.out.println("Thank you for using the encryption/decryption program");
break;
default:
System.err.println("Invalid option. Select 1 for encryption, 2 for decryption, or 0 to exit");
System.out.println("");
break;
}
}while (!isExit(opt));
return opt;
}
public static String keyInput(String key)
{
do
{
System.out.println("Enter the key you want for encryption/decryption");
key = In.getString();
System.out.println("Your key is: " +key);
System.out.println("");
}while (!isValid(key));
//printEncrypted(key);
return key;
}
public static String wordInput(String word)
{
do
{
System.out.println("Enter the word you want decrypted/encrypted");
word = In.getString();
System.out.println("You entered: " +word);
System.out.println("");
} while (!isValid(word));
//printEncrypted(word);
return word;
}
public static void encryptWord(String word, String key)
{
// String alphabet1 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
wordInput(word);
keyInput(key);
System.out.println("The word from the encryptWord metod " +word);
printEncrypted(word, key);
}
public static void decryptWord(String w, String k)
{
String alphabet1 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
wordInput(w);
keyInput(k);
}
这部分代码来自我的朋友
public static String printEncrypted(String word, String key)
{
System.out.println("This is the word from the printEncrypted method: " +word);
int shift = 0;
//Uses the key length to determine how much the alphabet will shift
for (int x = 0; x < key.length(); x++)
shift += key.charAt(x) - 96;
shift = shift % 26;
//Creates an array to perform the shift
char [] y = word.toCharArray();
for (int x = 0; x < y.length; x++)
//Uses the shift to shift the alphabet to decrypt the word.
for (int d = 0; d < shift; d++)
if (y[x] == 'z')
y[x] = 'a';
else {
y[x]--;
}
String newWord = new String(y);
System.out.println("Encrypted is " +newWord);
return newWord;
}
public static boolean isValid (String s)
{
String strSpecialChars="!@#$%&*()_+=-|<>?{}[]~";
//String alphabet2 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
boolean upCase = false;
boolean isDigit = false;
boolean spChar = false;
if (s.matches(".+[A-Z].+")){
upCase = true;
}
if (s.matches(".+[1-9].+")){
isDigit = true;
}
if (strSpecialChars.contains(s)){
spChar = true;
}
if (upCase|| isDigit || spChar)
{
System.err.println("The string cannot contain capital letters, special characters or numbers. Try again.");
System.out.println("");
return false;
}
else
{
return true;
}
}
public static boolean isExit (int option)
{
if (option == 0)
{
return true;
}
else
{
return false;
}
}
}
这就是我想要为我的角色转变做的事情:
public class LetterTester
{
public static void main (String []args)
{
String input="craig";
final String alphabet="abcdefghijklmnopqrstvwxyz";
int finalValue = 0;
int[] numbers;
for(int i=0;i<input.length();i++){
finalValue=(alphabet.indexOf(input.charAt(i))+1);
System.out.print(finalValue);
}
}
}
但是,我不知道如何创建变量并让我的 for 循环在每次运行时将其输出添加到变量中。 LetterTester 仅用于测试。在我的实际作业中,输入将从另一种方法中获取,然后进行测试。因此,例如,如果键是“abc”,它将确定每个字母的值,因此 a = 1,b = 2,c = 3。然后我希望将它一起添加到一个变量中我可以用。 因此,当输入的计算完成时,变量应该等于 6。
另外,不确定我是否应该为此提出第二个问题,但在我的作业代码中,我无法将我的单词和键输入的值从它们各自的方法(keyInput 和 wordInput)传递给一个名为 encryptWord 的方法,当我尝试从 encryptWord 方法测试它时,它显示单词为空白。
如果有人想知道我为输入做了什么,我正在使用 In 类,我从这里得到:http://www.ecf.utoronto.ca/~jcarter/
到目前为止,我的导师从一开始就教我们使用 In 类。
【问题讨论】:
-
WRT 到
wordInput()问题,您必须为该方法的返回值分配一些东西,例如String wordToEncrypt = wordInput();。不需要传递参数,因为它是从用户那里读取输入并返回值。 -
我不明白包含所有注释掉的代码的目的是什么。为什么会有这么多额外的空行?请整理您的代码。
-
@KevinO 谢谢!我会试试的。
-
@LiranFunaro 谢谢,我清理了。
-
@KevinO 你能澄清一下我可以改变什么吗?我已经分配了调用 wordInput(); 的变量 String word和调用 keyInput() 的可变字符串键。你能给我举个例子吗?而且,我的 isValid 检查器和 wordInput 都给了我一个错误,说找不到变量词,即使我在上面声明了它。
标签: java encryption caesar-cipher