gpt4 book ai didi

java - Java中的旋转量和方向加密

转载 作者:行者123 更新时间:2023-12-01 11:37:51 25 4
gpt4 key购买 nike

我有一个不久前创建的程序,它从用户那里获取一个字符串并对其进行加密。我正在开发该程序的第二个版本,该程序从用户处获取文本文件,让用户输入移位值,让用户输入移位方向(左或右),然后将加密消息输出到一个新文件。

由于其他成员的帮助,我现在可以让该程序正常运行,但这不是我想要的方式。现在它获取文件并加密为文件,但不使用移位或方向。例如,如果用户选择移位 3 和向右方向,则程序应读取文本文件的第一个字母并将键数组旋转 3 个空格并打印该字符。然后程序查看下一个字母,将数组向右旋转 3 个空格,然后打印该字符。我真的很困惑如何让数组改变方向。我知道我需要发送对从文件创建的字符串、移位方向和移位量进行加密的方法,然后使用循环来完成所有操作,但这有点超出了我的理解范围。任何有关该过程的帮助将不胜感激!抱歉代码很长,但我希望您能够看到正在发生的一切。

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 - 'A' + shiftValue) % 26) + 'A';
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();
}

}
}

这是我之前用来加密字符串的方法,无需用户输入移位量或方向:

public static String doEncryption(String s)
{
char alphabet[] = { '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' }; //hold the characters in alphabet array
char key[] = { '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', 'a' }; //holds characters in the key array

char encoded[] = new char[(s.length())];
String encrypted=""; //initialize the string for the new message
int j=0; //count variable
for(int i=0; i<s.length(); i++){ //begin for loop to run through string
boolean isFound = false;
j = 0;
while (j < alphabet.length && !isFound){


if (alphabet[j]==s.charAt(i)){
encrypted=encrypted+key[j];
isFound=true;
}
j++;
}
}

任何人都可以帮助我理解每当数组到达文件中的新字母时我需要做什么来旋转数组?谢谢!

最佳答案

您可以使用 ArrayList<String>而不是char[] 。替换key[j]key.get(j) 。旋转ArrayList通过使用 for 循环并调用 String s = key.remove(0)key.add(s) .

关于java - Java中的旋转量和方向加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29808904/

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