Simple encryption in java-no key/password

41,489

Solution 1

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

Solution 2

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.

Solution 3

You can encode the ip String to base64, reverse the string, and then use a Caesar cipher:

public String easeyEncrypt(String ip) {
   String b64encoded = Base64.getEncoder().encode(ip);

   // 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(reverse.charAt(i) + OFFSET);
   }
   return tmp.toString();
}

To decrypt procede backwards:

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

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

Solution 4

This is simplest encrypt/decrypt code but it is not secure.

     String strip = "192.168.1.11";        
     byte[] encryptArray = Base64.encodeBase64(strip.getBytes());        
     String encstr = new String(encryptArray,"UTF-8");   
     System.out.println("Enc   >>  "+ encstr);


     String strDec = "MTkyLjE2OC4xLjEx";
     byte[] dectryptArray = strDec.getBytes();
     byte[] decarray = Base64.decodeBase64(dectryptArray);
     String decstr = new String(decarray,"UTF-8"); 
     System.out.println("Dec  >>>  "+ decstr);

For this you have to import org.apache.commons.codec.binary.Base64;

you can Download org-apache-commons-codec.jar file from Link

Solution 5

If the generated string would be "random", then your application would have to keep track of any generated string for all time. Probably not a good design.

A fast poor man "encryption" would be ROT47 ( http://rot47.net/ )

Share:
41,489

Related videos on Youtube

Mayukh Nair
Author by

Mayukh Nair

Still figuring out what I even specialize in. Interested in human-computer interaction, data, and machine learning.

Updated on July 05, 2022

Comments

  • Mayukh Nair
    Mayukh Nair almost 2 years

    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.

    • Scary Wombat
      Scary Wombat over 9 years
      How about Base64 encoding?
    • Alex Salauyou
      Alex Salauyou over 9 years
      It will be not encryption at all, just encoding. Convert your ip to bytes and apply base64 conversion
    • Mathemats
      Mathemats over 9 years
      xor encryption is fast and easy to implement.
  • Alex Salauyou
    Alex Salauyou over 9 years
    It's for letters, not for decimals
  • GhostCat
    GhostCat over 9 years
    Yes, you are correct. I actually thought of ROT47; but confused myself. ROT47 will work for numbers, too.
  • Jagoda Gorus
    Jagoda Gorus almost 7 years
    There is no variable key in here: key.charAt(i), didn't you mean secret.charAt(i) ?
  • Al-Mothafar
    Al-Mothafar almost 7 years
    @JagodaSokół yes, ty
  • Jagoda Gorus
    Jagoda Gorus almost 7 years
    What's more, I also changed: Base64.getDecoder() to Base64.getMimeDecoder(), due to Base64: java.lang.IllegalArgumentException: Illegal character error and included your functions in my small university project :)
  • Hristo Stoyanov
    Hristo Stoyanov about 6 years
    From the wikipedia link: As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security
  • Roberto Rodriguez
    Roberto Rodriguez over 5 years
    Simple and easy.. Thanks!!
  • lance.dolan
    lance.dolan over 4 years
    since java 8, you can use the Base64 that is part of the JDK. java.util.Base64.getEncoder().encode(string.getBytes());