To protect passwords from hackers and other dishonest people, passwords are normally store in encrypted form. This means nobody can actually retrieve the password by reading the file. For example, here is an encrypted password: NCJMH . Can you guess what the real password is?
Maybe the java code for the encryption algorithm will help:
public class Crypto
{ public static void main(String[] args)
{
System.out.println(encrypt("testing"));
}
public static String encrypt(String s)
{
String answer = "";
for (int x = 0; x < s.length(); x++)
{
char c = s.charAt(x);
int code = (int)(c + x + 1);
char nc = (char)code;
answer = answer + nc;
}
return answer;
}
}
(1) Read the algorithm and see if you can guess what it will do to "testing".
(2) Run the program and check whether you were correct. If not, think through the algorithm again until you understand the result.
(3) Write the corresponding decrypt algorithm. Check that it works correctly.
Here are some other possibilities for encryption algorithms. Choose one that you understand and write the encrypt and decrypt methods. Check that they work correctly.
(A)
Ask the user what number they wish to use (between 1 and 9)
Add that
number to each ASCII code
(B)
take the first letter
calculate the ASCII code of the letter
Calculate
256-(ascii code)
Change this number back to a char
append this
char to the result String
continue to the next letter
(C)
Scramble the order of the letters by doing the following:
Take the
last letter.
Move backward (left) two positions.
Take this letter.
Move
backward (left) two positions.
Take this letter.
Continue back to
the beginning of the String.
Then copy the remaining letters backward
to the String.
For example: "TRAVEL" -->
"LVREAT"