Created
June 29, 2017 08:59
-
-
Save bitsnaps/9cea5e16e09065d20c22db6a4d15cb65 to your computer and use it in GitHub Desktop.
encrypt & decrypt randomly strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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