0

I added a password checker for my program, I thought it works fine since I could save the password in a file (encoded) and could enter the password in the password field and it let me into the main program without problems. However, today I was testing more and I found out that some passwords do not work and I have no idea why that is the case. I included both my methods, one does encode the password, the other one does decode it. The verify method I included is the one that reads from the password file, decodes the password and checks if the entered password equals the saved one. I couldnt find out what types of passwords do not work, its not the length, more like the characters which were used. Thanks in advance

public static char[] encode(int offset, char[] charArray) {

    char[] arrEnc = new char[charArray.length];

    for (int i = 0; i < charArray.length; i++) {

        int verschiebung = (charArray[i] + offset) % 128;

        arrEnc[i] = (char) (verschiebung);

    }
    return arrEnc;

}

public static char[] decode(int offset, char[] charArray) {

    char[] arrEnc = new char[charArray.length];

    int verschiebung;

    for (int i = 0; i < charArray.length; i++) {

        if (charArray[i] - offset < 0) {
            verschiebung = charArray[i] - offset + 128;

        } else {
            verschiebung = (charArray[i] - offset) % 128;

            arrEnc[i] = (char) (verschiebung);
        }
    }
    return arrEnc;

}

private void verify() {
    try {
        FileReader fr = new FileReader(pws);
        BufferedReader br = new BufferedReader(fr);
        char[] arr = br.readLine().toCharArray();
        char[] newArr = decode(arr.length, arr);
        String pw = new String(newArr);
        String masterPw = "Kassa";
        if (passwordField.getText().equals(pw) || 
                  passwordField.getText().equals(masterPw)) {
            setVisible(false);
            starto.setVisible(true);
            br.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Any password should be able to be saved and decoded

1 Answers1

0

You have a mistake in the code.

if (charArray[i] - offset < 0) {
        verschiebung = charArray[i] - offset + 128;
        ...

Here you forgot to put

arrEnc[i] = (char) (verschiebung);

So you should assign verschiebung to arrEnc[i] in the first condition block in the decode method.

Anar Amrastanov
  • 385
  • 1
  • 3
  • 13