Reputation: 653
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.
Upvotes: 16
Views: 49680
Reputation: 16039
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);
}
Upvotes: 4
Reputation: 1195
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));
}
}
hope this helps.
Upvotes: 0
Reputation: 8219
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.
Upvotes: 10
Reputation: 1421
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
Upvotes: 2
Reputation: 2253
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
Upvotes: 17
Reputation: 140553
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/ )
Upvotes: 1