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 single 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

The program converts it to AzlQrEHCSD or some other random string

I enter this line into the program

Returns to 192.168.1.1

Is there any simple algorithm that can do this without creating things like keys or extra passwords? I understand that keys and passwords are required for encryption and decryption, but my script does not require this.

+7
java string algorithm random encryption
source share
6 answers

I know its excess, but I would use the jasypt library, since it is very easy to use. All you need is a random seed for encryption or decrpyt.

Here is the source code for data encryption:

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

And to decrypt the data:

 StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword(seed); String decrypted = encryptor.decrypt(encrypted); 

Or you can simply encode or decode your string in a base64 example, here is shown: Base64 Java encodes and decodes a string

+8
source share

Almost the same as higuaro solutions, but with a lot of fixes to make it work, the following code tested and working with higuaro doesn’t work very well, as the characters fall into numbers and when you cancel it, you get one 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 cancel the procedure back:

 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 this will be helpful.

+6
source share

You can encode ip String in base64 , flip the string and then use Caesar's 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, go back:

 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); } 
+4
source share

This is the simplest encryption / decryption 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 need to import org.apache.commons.codec.binary.Base64;

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

+2
source share

If the generated line is "random", your application will have to keep track of any generated line for the whole time. This is probably not a very good design.

Fast poor human "encryption" will be ROT47 ( http://rot47.net/ )

+1
source share

Here is a simple random encryption:

 class SimpleStringEncryption { public static String encrypt(String str){ int code; String result = ""; for (int i = 0; i < str.length(); i++) { code = Math.round((float) Math.random()*8+1); result += code + Integer.toHexString( ((int) str.charAt(i) ) ^ code )+"-"; } return result.substring(0, result.lastIndexOf("-")); } public static String decrypt(String str){ str = str.replace("-", ""); String result = ""; for (int i = 0; i < str.length(); i+=3) { String hex = str.substring(i+1, i+3); result += (char) (Integer.parseInt(hex, 16) ^ (Integer.parseInt(String.valueOf(str.charAt(i))))); } return result; } public static void main (String[] args) { String e = "some text to encrypt"; String encrypted = encrypt(e); System.out.println(encrypted); System.out.println(decrypt(encrypted)); } } 

I hope this helps.

0
source share

All Articles