1

This is my first posted question on Stack Overflow. I have developed an application that stores a user's text in a simple encrypted form in a text file. The application also is meant to decrypt the text and display it on the screen at the user's demand. The code below shows my "UnEncrypt" method. This method effectively separates the encrypted text into four character intervals. It then runs these four character intervals through an encryption key method to convert the four characters into a single character. I also have this method run a "test" string. The value of the "test" string is "85jf" which corresponds to the character "t" in the "UnKey" method. Now for the problem: strings "c" and "test" both hold the exact same value; however, the "test" string is effectively converted into the character "t" and the "c" string returns an error ("E"). I have included evidence of this below: the "UnEncrypt" method. So to conclude here: strings "test" and "c" have the exact same value, however, those values were created different ways (as seen in the code). When "test" and "c" run through the same method "UnKey", two different values are returned. My question is why this is happening? I need "c" to return a valid character for my application to work. My only guess as to why this is happening is because when the for loop generates the string "c", it looks the same on the outside but some inner value is different and therefor incompatible with my method "UnKey". I have tried to be as clear as possible. Please ask me questions if needed. Thanks in advance for any help! Edit: string s being passed into "UnEncrypt" is "85jf"

    public static String UnEncrypt(String s){
    char one = 0;
    char two = 0;
    char three = 0;
    char four = 0;
    String c = "";
    String fnl = "";
    String test = "85jf";
    for(int i = 0; i<= ((s.length()/4)-1); i++){

        one = s.charAt((i*4)+0);
        c += one;
        two = s.charAt((i*4)+1);
        c += two;
        three = s.charAt((i*4)+2);
        c += three;
        four = s.charAt((i*4)+3);
        c += four;
        System.out.println(c);
        System.out.println(test);
        System.out.println(UnKey(test));
        System.out.println(UnKey(c));

        c = "";
    }
    return fnl;
}
public static char UnKey(String s){
    char rtrn = 0;
    if (s == "rtfg"){
        rtrn = 'a';
    }else if (s == "sefc"){
        rtrn = 'b';
    }else if (s == "escf"){
        rtrn = 'c';
    }else if (s == "wjvo"){
        rtrn = 'd';
    }else if (s == "wvke"){
        rtrn = 'e';
    }else if (s == "24fk"){
        rtrn = 'f';
    }else if (s == "35fs"){
        rtrn = 'g';
    }else if (s == "ceif"){
        rtrn = 'h';
    }else if (s == "5ue8"){
        rtrn = 'i';
    }else if (s == "544f"){
        rtrn = 'j';
    }else if (s == "09fj"){
        rtrn = 'k';
    }else if (s == "4f84"){
        rtrn = 'l';
    }else if (s == "34fr"){
        rtrn = 'm';
    }else if (s == "4ofo"){
        rtrn = 'n';
    }else if (s == "59e9"){
        rtrn = 'o';
    }else if (s == "fje3"){
        rtrn = 'p';
    }else if (s == "rewq"){
        rtrn = 'q';
    }else if (s == "3f55"){
        rtrn = 'r';
    }else if (s == "34kf"){
        rtrn = 's';
    }else if (s == "85jf"){
        rtrn = 't';
    }else if (s == "daf8"){
        rtrn = 'u';
    }else if (s == "5cr3"){
        rtrn = 'v';
    }else if (s == "34fr"){
        rtrn = 'w';
    }else if (s == "d390"){
        rtrn = 'x';
    }else if (s == "sef4"){
        rtrn = 'y';
    }else if (s == "54jf"){
        rtrn = 'z';
    }else if (s == "fr73"){
        rtrn = '1';
    }else if (s == "fr4r"){
        rtrn = '2';
    }else if (s == "seg7"){
        rtrn = '3';
    }else if (s == "u87i"){
        rtrn = '4';
    }else if (s == "436i"){
        rtrn = '5';
    }else if (s == "0et3"){
        rtrn = '6';
    }else if (s == "uti9"){
        rtrn = '7';
    }else if (s == "9i5t"){
        rtrn = '8';
    }else if (s == "675f"){
        rtrn = '9';
    }else if (s == "53d4"){
        rtrn = '0';
    }else if (s == "1432"){
        rtrn = ' ';
    }else{
        rtrn = 'E';
    }
    return rtrn;
}
Please enter the password...
pass
Select command from menu...
1. View Passwords
2. Create New Entry
1
85jf
85jf
t
E
  • 1
    Two possibilities: 1) `c` and `test` are different strings that simply display as identical (you can diagnose this by printing the result of `c.equals(test)`); 2) something in the call to `UnKey` changes state after the first call, so it does not return the same value when called again. Since you don't show us all the relevant code (not only `UnKey`, but also what value of `s` is being passed in), it's hard to say more. – Ted Hopp Apr 23 '14 at 03:29
  • Keep in mind that `c` and `test` represent the same text, but the objects do not have the same value (e.g. `c.equals(test)` will be true, but `c == test` will be false). Without seeing `Unkey` it's hard to say what's going on. – azurefrog Apr 23 '14 at 03:34
  • I made edits showing the method "UnKey" as well as the value of string s. I clearly don't understand how two strings can display the same text but hold different values. tI am still quite new to programming. – user3562761 Apr 23 '14 at 03:51
  • @ElliottFrisch - maybe I shouldn't have deleted my comments, as yours look a bit lonely now. I'm never sure about the correct etiquette in these situations. – Simon MᶜKenzie Apr 23 '14 at 03:56
  • 1
    @SimonMᶜKenzie I believe comments are meant to be post-its on the way to a **correct** answer. – Elliott Frisch Apr 23 '14 at 03:58
  • Thank you @ElliottFrisch, that's the solution to my problem right there – user3562761 Apr 23 '14 at 04:00
  • 2
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – takendarkk Apr 23 '14 at 04:11

1 Answers1

3

That seems like a terrible idea, I strongly urge you not to use a reversible transform with the password. If your passwords are stored in a reversible manner, then an attacker (with access to the file) can reverse your users' passwords. Instead, current best practices are to use a cryptographically secure hash and a SALT to prevent attacks with rainbow tables. See also the now ancient Shadow Suite.

Are you aware of the pigeonhole principle? Inconceivable? That's your cue Mr. Patinkin.

Edit Since you added code,

Don't use == to test String equality. With Object types that is for reference equality testing. You need to use .equals().

if ("a" != new String("a")) {
  System.out.println("It is known."); // <-- There a GoT reference too.
}
Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    @Takendarkk That Shadow Suite link includes this gem: "Since a 4GB hard drive can be had for under $1000.00, this is well within the means of most system crackers." And I remember those days. Of course, I can pick up a 4GB USB stick for $5 at the Target check-out lane now. Kids' today, with their loud music and such. – Elliott Frisch Apr 23 '14 at 03:42