Mayukh Nair Mayukh Nair - 6 months ago 16
Java Question

Simple encryption in java-no key/password

Say I have an IP address, 192.168.1.1

I want my program to create a random one-word string based on this IP address which can be easily decrypted without a key or password or additional security.

eg.


I enter 192.168.1.1

Program converts it to AzlQrEHCSD or some other random string

I enter this string in the program

It gets converted back to 192.168.1.1


Is there any simple algorithm that can do this without generating stuff like keys or additional passwords? I understand that keys and passwords are a must for encryption and decryption, but my scenario does not require it.

Answer

I know its overkill but i would use jasypt library since its realy easy to use. All you need is random seed to encrypt or decrpyt.

Here is the source code for encrypting data:

String seed = "ipNumber";
String myIpValue = "192.168.0.1";

StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(seed);
String encrypted= encryptor.encrypt(myIpValue);

And for data decryption:

StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(seed);

String decrypted = encryptor.decrypt(encrypted);

Or you could just encode or decode your string to base64 example is show here: Base64 Java encode and decode a string