我的代码应该从文本文档中获取文本并通过将每个字符加 1 来对其进行加密。我希望输出为:qbttx(密码的加密。)但它只是输出:q任何人都知道如何解决这个问题? 保存应该加密的文本文档也包含文本“passw”。
import java.io.*;
public class EncryptionMain {
public static void main(String args[]) {
File file = new File("c:\\Visual Basic Sign in\\password.txt");
StringBuilder contents = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
// repeat until all lines is read
while ((text = reader.readLine()) != null) {
contents.append(text)
.append(System.getProperty(
"line.separator"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
int first = 0, second = 1;
for (int i = 0; i < contents.length(); i++)
{
char[] arr = new char[7];
contents.getChars(first, second, arr, 0); // get chars 0,1 to get 1st char
char ch = arr[i];
if (ch >= 'A' && ch < 'Z') ch++;
else if (ch == 'Z') ch = 'A';
else if (ch >= 'a' && ch < 'z') ch++;
else if (ch == 'z') ch = 'a';
// show encrypted contents here
System.out.println(ch);
first++;
second++;
}
}
}