Reputation: 11
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
Upvotes: 1
Views: 75
Reputation: 201527
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.
}
Upvotes: 3