Skip to content

Instantly share code, notes, and snippets.

@bitsnaps
Created June 29, 2017 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitsnaps/9cea5e16e09065d20c22db6a4d15cb65 to your computer and use it in GitHub Desktop.
Save bitsnaps/9cea5e16e09065d20c22db6a4d15cb65 to your computer and use it in GitHub Desktop.
encrypt & decrypt randomly strings
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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment