使用 StringBuilder 和 PrintWriter(不是 StringWriter)简单加密将密码写入 txt 文件
Write password to txt file with simple encryption with StringBuilder and PrintWriter, (Not StringWriter)
所以在我解释我的问题之前,我已经问过我是否可以为这段代码使用 StringWriter,但是我们被告知我们必须使用 StringBuilder。当前指令状态...
密码匹配后,使用 StringBuilder,将密码写入名为 Lab5.txt 的文本文件,但使用以下简单加密 – 为每个字符写入 ASCII 值,以逗号分隔。写一个0(零)表示密码结束。
我对这将如何运作有一个大致的了解,但是我完全不确定从这里到哪里去,因为我对 StringBuilder 非常不熟悉,而且没有很多有用的教程,而且大多数 Whosebug posts 关于这个建议使用 StringWriter。
我当前的代码...
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.List;
import java.io.PrintWriter;
public class Project5_Part1_Final {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter password : ");
String pass = in.nextLine();
System.out.print("Please re-enter password: ");
String confirm = in.nextLine();
List<String> errorList = new ArrayList<String>();
while (!isValid(pass, confirm, errorList)) {
System.out.println("The password entered here is invalid");
for (String error : errorList) {
System.out.println(error);
}
System.out.print("Please enter password : ");
pass = in.nextLine();
System.out.print("Please re-enter password: ");
confirm = in.nextLine();
}
System.out.println("your password is: " + pass);
}
public static boolean isValid(String password, String confirm, List<String> errorList) {
int passLength = password.length();
int i;
Pattern specailChar = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Pattern digitCase = Pattern.compile("[0-9 ]");
errorList.clear();
boolean testing = true;
if (!password.equals(confirm)) {
errorList.add("password and confirm password does not match");
testing = false;
}
if (password.length() < 8) {
errorList.add("Invalid Password - Password must be at least 8 characters");
testing = false;
}
if (!digitCase.matcher(password).find()) {
errorList.add("Invalid password - Password must have at least 1 number");
testing = false;
}
if (!specailChar.matcher(password).find()) {
errorList.add("Invalid password - Password must have at least one special character");
testing = false;
}
for (i = 0; i < passLength; i++) {
char character = password.charAt(i);
int ascii = (int) character;
System.out.print(ascii + ",");
PrintWriter output = new PrintWriter("Lab5.txt");
StringBuilder sb = new StringBuilder();
sb.append(ascii);
output.println(sb.toString());
}
System.out.print(" 0 ");
return testing;
}
}
我当前的错误信息...
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
PrintWriter cannot be resolved to a type
PrintWriter cannot be resolved to a type
at Project5_Part1_Final.isValid(Project5_Part1_Final.java:66)
at Project5_Part1_Final.main(Project5_Part1_Final.java:18)
我知道我遗漏了一些重要的代码部分,但是我无法找到任何 post 或像这样一起使用 StringBuilder 和 PrintWriter 的示例,它们要么只是 PrintWriter,要么就是 PrintWriter和字符串编写器。任何形式的帮助将不胜感激。谢谢。
以下仅涉及加密,不涉及密码的获取和验证
据我了解您的问题,您需要用 ASCII 代码替换密码的每个字符,例如,您需要数字 65(十进制)而不是 A。此外,您需要用逗号分隔每个字母的 ASCII 码,最后您需要用数字 0(零)终止编码密码。
下面的代码实现了这一点。
String str = "George$Best11";
char[] letters = str.toCharArray();
StringBuilder sb = new StringBuilder();
for (char letter : letters) {
int code = (int) letter;
sb.append(code);
sb.append(',');
}
sb.append(0);
System.out.println(sb);
try (PrintWriter pw = new PrintWriter("Lab5.txt")) {
pw.printf("%s%n", sb);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
示例密码为George$Best11,符合您的合法密码条件,即
- 至少 8 个字符
- 至少一个特殊字符($)
- 至少一位数字
我得到了密码字符串的字符数组。我将每个字符都转换为 int
。我将 int
附加到 StringBuilder
并紧跟在 int
之后我附加了一个逗号。最后,在附加所有字母后,我附加了终止零。
以上代码显示如下:
71,101,111,114,103,101,36,66,101,115,116,49,49,0
所以在我解释我的问题之前,我已经问过我是否可以为这段代码使用 StringWriter,但是我们被告知我们必须使用 StringBuilder。当前指令状态...
密码匹配后,使用 StringBuilder,将密码写入名为 Lab5.txt 的文本文件,但使用以下简单加密 – 为每个字符写入 ASCII 值,以逗号分隔。写一个0(零)表示密码结束。
我对这将如何运作有一个大致的了解,但是我完全不确定从这里到哪里去,因为我对 StringBuilder 非常不熟悉,而且没有很多有用的教程,而且大多数 Whosebug posts 关于这个建议使用 StringWriter。
我当前的代码...
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.List;
import java.io.PrintWriter;
public class Project5_Part1_Final {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter password : ");
String pass = in.nextLine();
System.out.print("Please re-enter password: ");
String confirm = in.nextLine();
List<String> errorList = new ArrayList<String>();
while (!isValid(pass, confirm, errorList)) {
System.out.println("The password entered here is invalid");
for (String error : errorList) {
System.out.println(error);
}
System.out.print("Please enter password : ");
pass = in.nextLine();
System.out.print("Please re-enter password: ");
confirm = in.nextLine();
}
System.out.println("your password is: " + pass);
}
public static boolean isValid(String password, String confirm, List<String> errorList) {
int passLength = password.length();
int i;
Pattern specailChar = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Pattern digitCase = Pattern.compile("[0-9 ]");
errorList.clear();
boolean testing = true;
if (!password.equals(confirm)) {
errorList.add("password and confirm password does not match");
testing = false;
}
if (password.length() < 8) {
errorList.add("Invalid Password - Password must be at least 8 characters");
testing = false;
}
if (!digitCase.matcher(password).find()) {
errorList.add("Invalid password - Password must have at least 1 number");
testing = false;
}
if (!specailChar.matcher(password).find()) {
errorList.add("Invalid password - Password must have at least one special character");
testing = false;
}
for (i = 0; i < passLength; i++) {
char character = password.charAt(i);
int ascii = (int) character;
System.out.print(ascii + ",");
PrintWriter output = new PrintWriter("Lab5.txt");
StringBuilder sb = new StringBuilder();
sb.append(ascii);
output.println(sb.toString());
}
System.out.print(" 0 ");
return testing;
}
}
我当前的错误信息...
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
PrintWriter cannot be resolved to a type
PrintWriter cannot be resolved to a type
at Project5_Part1_Final.isValid(Project5_Part1_Final.java:66)
at Project5_Part1_Final.main(Project5_Part1_Final.java:18)
我知道我遗漏了一些重要的代码部分,但是我无法找到任何 post 或像这样一起使用 StringBuilder 和 PrintWriter 的示例,它们要么只是 PrintWriter,要么就是 PrintWriter和字符串编写器。任何形式的帮助将不胜感激。谢谢。
以下仅涉及加密,不涉及密码的获取和验证
据我了解您的问题,您需要用 ASCII 代码替换密码的每个字符,例如,您需要数字 65(十进制)而不是 A。此外,您需要用逗号分隔每个字母的 ASCII 码,最后您需要用数字 0(零)终止编码密码。
下面的代码实现了这一点。
String str = "George$Best11";
char[] letters = str.toCharArray();
StringBuilder sb = new StringBuilder();
for (char letter : letters) {
int code = (int) letter;
sb.append(code);
sb.append(',');
}
sb.append(0);
System.out.println(sb);
try (PrintWriter pw = new PrintWriter("Lab5.txt")) {
pw.printf("%s%n", sb);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
示例密码为George$Best11,符合您的合法密码条件,即
- 至少 8 个字符
- 至少一个特殊字符($)
- 至少一位数字
我得到了密码字符串的字符数组。我将每个字符都转换为 int
。我将 int
附加到 StringBuilder
并紧跟在 int
之后我附加了一个逗号。最后,在附加所有字母后,我附加了终止零。
以上代码显示如下:
71,101,111,114,103,101,36,66,101,115,116,49,49,0