Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. class SimpleStringEncryption {
  2.  
  3. public static String encrypt(String str){
  4. int code;
  5. String result = "";
  6. for (int i = 0; i < str.length(); i++) {
  7. code = Math.round((float) Math.random()*8+1);
  8. result += code + Integer.toHexString( ((int) str.charAt(i) ) ^ code )+"-";
  9. }
  10. return result.substring(0, result.lastIndexOf("-"));
  11. }
  12.  
  13. public static String decrypt(String str){
  14. str = str.replace("-", "");
  15. String result = "";
  16. for (int i = 0; i < str.length(); i+=3) {
  17. String hex = str.substring(i+1, i+3);
  18. result += (char) (Integer.parseInt(hex, 16) ^ (Integer.parseInt(String.valueOf(str.charAt(i)))));
  19. }
  20. return result;
  21. }
  22.  
  23. public static void main (String[] args) {
  24. String e = "some text to encrypt";
  25. String encrypted = encrypt(e);
  26. System.out.println(encrypted);
  27. System.out.println(decrypt(encrypted));
  28. }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement