【问题标题】:Write password to txt file with simple encryption with StringBuilder and PrintWriter, (Not StringWriter)使用 StringBuilder 和 PrintWriter 将密码写入 txt 文件(不是 StringWriter)
【发布时间】:2020-07-10 22:04:03
【问题描述】:

所以在我解释我的问题之前,我已经问过我是否可以在这段代码中使用 StringWriter,但是我们被告知必须使用 StringBuilder。当前的指令状态...

一旦密码匹配,使用 StringBuilder 将密码写入名为 Lab5.txt 的文本文件,但使用以下简单加密 - 为每个字符写入 ASCII 值,用逗号分隔。写入 0(零)表示密码结束。

我对它的工作原理有一个大致的了解,但是我完全不确定从这里可以去哪里,因为我对 StringBuilder 非常不熟悉,并且没有很多有用的教程,大多数关于这个的 stackoverflow 帖子建议使用 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)

我知道我错过了一些重要的代码部分,但是我找不到任何像这样一起使用 StringBuilder 和 PrintWriter 的帖子或示例,它们要么只是 PrintWriter,要么是 PrintWriter 和 StringWriter。任何形式的帮助将不胜感激。谢谢。

【问题讨论】:

  • 错误信息与您的代码不匹配。你粘贴错文件了吗?
  • 我很确定这是正确的文件,Project5_Part1_Final。您从哪里得到的印象是错误的文件?
  • 您在每次循环迭代中创建一个新的PrintWriter。另外,您并没有关闭它们。

标签: java stringbuilder printwriter


【解决方案1】:

以下内容只涉及加密,不涉及获取或验证密码。

据我了解您的问题,您需要将密码的每个字符替换为其 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 2014-09-02
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多