gpt4 book ai didi

java - 为什么我的密码打印不正确?

转载 作者:行者123 更新时间:2023-12-01 10:53:27 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,该程序接受输入文件密码,对其进行加密,然后将其保存到另一个文件中,同时仍处于加密状态。到目前为止,我已经获得了创建文件的程序,并且对于密码中的每个字母,它将替换该字母。问题是对于密码中的每个字母,我打印出密码而不是字母表中的字符。我想将我的密码“淘汰”,并且对于每个字母,更改为不同的随机字母。例如“fallout”到“dfljsor”。我认为问题出在我的打印行上,我该如何解决它?

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class WriteFile {

public static void main(String [] args) throws IOException {
java.io.File inputFile = new java.io.File("inputFile.txt");
java.io.File outputFile = new java.io.File("encrypted.txt");

//create files and write to files
try(Scanner input = new Scanner(inputFile);
PrintWriter output = new PrintWriter(outputFile);) {
//creating array that holds the abc's
//password is tombraider
String key[] = { "q","w","r","t","u","i","o",
"p","a","s","d","f","g","h",
"j","k","l","z","x","c","v",
"b","n","m" };
String key1[] = { "qwertyuiopasdfghjklzxcvbnm" };

while (input.hasNext()) {
String x = input.nextLine();

//select a random char in key
double number = Math.random() * 27;
String index = key1.toString();
char sort = index.charAt((int) number);

// for each letter in the password, it is to be replaced with sort
for (int i = 0; i < x.length(); i++) {
char select = ((char) i);
output.print(x.replace(select, sort));
}
}
}
}
}

最佳答案

我没有足够高的代表来发表评论(我是故意的)。但看来您正在使用x.replace(select, sort)。您已将 x 设置为整行。您可能想要做的事情是类似 Character.toString(x.charAt(i)).replace(select, sort)) 或类似的事情。这样你就可以得到每个字符,而不是整行。

关于java - 为什么我的密码打印不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33729982/

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