Simple String Encryption
HI,
I have been serching around on here for encyption methods but there seems to be soo many i not sure which ones would be good for me.
All i want to do is encypt and decrypt a String record. The encryption doesnt have to be particulary safe but must work for all possible charaters. Basically i am saving records to a text file and i dont want someone to be able to open the text file and read the contents.
I would like to make it as simple as possible so i can just use it like:
String record ="This is the record";
// Encrypted Record
String encRecord = Encryption.encrypt(record);
// Decripted Record
String decRecord = Encryption.decrypt(record);
Can anyone stear me in the right direction or give me some sample code i can study.
Thank you in advance
ed
Here is simple encryption routine that I just wrote that I bet would keep someone
stumped for a while.
/**
* This is a simple encryption class that uses simple character substitution.
* To keep the pattern from being obvious the shift factor is a function of the current character's
* position in the source string. This makes sure that all letters aren't encrypted to the same value.
*
* Usage: To encrypt a string just pass it to the encrypt method.
* String encryptedString = Encryptor.encrypt("Some string.");
*
*To decrypt a string just pass the encrypted string to the decrypt method.
* String decryptedString = Encryptor.decrypt(encryptedString);
*/
class Encryptor
{
static final byte [] shiftFactor = {2,5,7,9,13,17,19};
static final int modulo = shiftFactor.length;
static public String encrypt(String s)
{
char [] ba = s.toCharArray();
for (int i = 0; i < ba.length; i++)
{
ba[i] += shiftFactor[i%modulo];
}
return new String(ba);
}
static public String decrypt(String s)
{
char [] ba = s.toCharArray();
for (int i = 0; i < ba.length; i++)
{
ba[i] -= shiftFactor[i%modulo];
}
return new String(ba);
}
public static void main(String [] args)
{
String [] test = {"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz,./<>?;':\"[]{}-=_+!@#$%^&*()`~",
"Now is the time for all good men \n to come to the aid of their country."};
for (int i = 0; i < test.length; i++)
{
System.out.println(test[i]);
String encrypted = Encryptor.encrypt(test[i]);
System.out.println(encrypted);
System.out.println(Encryptor.decrypt(encrypted) + "\n");
}
}
}