gpt4 book ai didi

java - 程序未正确加密

转载 作者:行者123 更新时间:2023-11-30 07:01:40 24 4
gpt4 key购买 nike

我正在开发一个接受用户输入的程序(输入 txt 文件、要移动的空格数、移动方向以及他们要创建的 output.txt 文件的名称)。我的程序正在编译,当我运行它时,它会创建一个输出文件,但是结果与它们应该的结果相去甚远。例如,如果我将它设置为使用 3 的移位进行加密并且方向是正确的,则单词 The 应该更改为 WKH。目前,我还没有实现这个方向,因为我似乎无法弄清楚我将如何做到向左移动。任何人都可以看看我的代码并帮助指导我走向正确的方向吗?非常感谢您的宝贵时间!

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

public class CaeserCipher {

public static void main(String[] args)throws IOException {

String originalText="";
String inputFile;
String outputFile = "";
String shiftDirection;
int shiftValue;
Scanner keyboard = new Scanner(System.in);

//Prompt user for input file name
Scanner in = new Scanner(System.in);
System.out.print("What is the filename?: ");
inputFile = in.nextLine();


//make sure file does not exist
File file = new File(inputFile);
if (!file.exists())
{
System.out.println("\nFile " + inputFile + " does not exist. File could not be opened.");

System.exit(0);
}

//send the filename to be read into String

originalText = readFile(inputFile);


//Prompt user for shift value
System.out.print("\nWhat is the shift value? ");
shiftValue = keyboard.nextInt();

//Prompt user for shift direction
Scanner sc = new Scanner(System.in);
System.out.print("What direction would you like to shift? Press L for left or R for right: ");

//validate the input
while (!sc.hasNext("[LR]")) {
System.out.println("That's not a valid form of input! Please enter only the letter 'L' or 'R': ");
sc.next();
shiftDirection = sc.next(); //stores the validated direction
}//end while

shiftDirection = sc.next(); //stores the validated direction

//Return encrypted string
String encryptedText = encrypt(originalText , shiftValue);

//get the outputfile name
System.out.print("What is the name of the output file you want to create?: ");
outputFile = in.nextLine();

//make sure file does not exist
File file2 = new File(outputFile);
if (file2.exists())
{
System.out.println("\nFile " + outputFile + " already exists. Output not written.");

System.exit(0);
}

try {
File file3 = new File(outputFile);
BufferedWriter output = new BufferedWriter(new FileWriter(file3));
output.write(encryptedText);
output.close();
} catch ( IOException e ) {
e.printStackTrace();
}
System.out.println("\nOutput written to " + outputFile);

} //end main

//rotate and change chars
public static String rotate(String userString, int shiftValue) {

String convertedText = "";
for(int i = 0; i < userString.length(); i++){
char lowerLetter = userString.charAt(i);

//Convert to uppercase
char upperLetter = Character.toUpperCase(lowerLetter);
int charNumber = upperLetter;

//Apply shift, remembering to wrap text
int rotateShift = (charNumber + shiftValue) % 26;
char shiftLetter = (char) rotateShift;

//Create new string of shifted chars
convertedText += shiftLetter;
}
return convertedText;
}

//encrypt
public static String encrypt(String userString, int shiftValue) {
String encryptedString = rotate(userString , shiftValue);
return encryptedString;
}

private static String readFile(String inputFile) throws java.io.IOException {
File file = new File(inputFile);
StringBuilder fileContents = new StringBuilder((int) file.length());
Scanner scanner = new Scanner(new BufferedReader(new FileReader(file)));
String lineSeparator = System.getProperty("line.separator");

try {
if (scanner.hasNextLine()) {
fileContents.append(scanner.nextLine());
}
while (scanner.hasNextLine()) {
fileContents.append(lineSeparator + scanner.nextLine());
}

return fileContents.toString();
}


finally {
scanner.close();
}

}
}

最佳答案

您的问题是 %26。你没有考虑如何ASCII角色工作。您需要这样做:

int rotateShift = ((charNumber - 'A' + shiftValue) % 26) + 'A';

您之前的操作是错误的,因为 char 是一个 ASCII 值。这意味着 'A' == 65,因此要将字符表示形式转换为数字,您应该首先从字符值中减去 'A'。这映射 A->0, B->1, C->2, ...。然后当您完成 Caesar Shift 时,您需要将 'A' 的值加回整数以将其转回 ASCII 字符。

您可能还会遇到 Java 的 % 运算符的另一个问题。 Java 的模块化运算符操作如下:

-4 % 5 == -4

因此我会写一个加密的 mod 函数:

public int crypto_mod(int num, int mod)
{
num %= mod;
if(num < 0) num += mod;
return num;
}

这应该会产生您要查找的字符。

关于java - 程序未正确加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29708274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com