Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 471
Mayukh Nair Avatar asked Mar 24 '15 07:03

Mayukh Nair


People also ask

Which type of encryption has no keys?

Something outside of the thing being encrypted needs to be used to do encryption, if only because you need that thing to decrypt it later. This external thing is a key. There is no useful encryption without a key. There is only hashing.

Do you need a password for encryption?

So, using a strong password for all your online accounts is essential for your security. Encryption is more secure than passwords because sensitive information or data is encrypted or hidden using an algorithm and a key. The message can only be decrypted using the correct key and a cipher is a key to the code.

What is the best way to encrypt passwords in Java?

Password-Based Encryption using Salt and Base64: The password-based encryption technique uses plain text passwords and salt values to generate a hash value. And the hash value is then encoded as a Base64 string. Salt value contains random data generated using an instance of Random class from java. util package.


2 Answers

Almost the same as higuaro solutions but with a lot of fixes to make it work, the following code tested and working since higuaro not working well like characters went into numbers and when you reverse its get single number and damage everything:

public String caesarCipherEncrypt(String plain) {
   String b64encoded = Base64.getEncoder().encodeToString(plain.getBytes());

   // Reverse the string
   String reverse = new StringBuffer(b64encoded).reverse().toString();

   StringBuilder tmp = new StringBuilder();
   final int OFFSET = 4;
   for (int i = 0; i < reverse.length(); i++) {
      tmp.append((char)(reverse.charAt(i) + OFFSET));
   }
   return tmp.toString();
}

To de-crypt procede backwards:

public String caesarCipherDecrypte(String secret) {
   StringBuilder tmp = new StringBuilder();
   final int OFFSET = 4;
   for (int i = 0; i < secret.length(); i++) {
      tmp.append((char)(secret.charAt(i) - OFFSET));
   }

   String reversed = new StringBuffer(tmp.toString()).reverse().toString();
   return new String(Base64.getDecoder().decode(reversed));
}

I hope its helpful.

like image 142
Al-Mothafar Avatar answered Oct 05 '22 18:10

Al-Mothafar


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

like image 21
Kiki Avatar answered Oct 05 '22 18:10

Kiki