fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. static String encrypt(String key) {
  12. String encrypHexa = "";
  13. try (Scanner x = new Scanner("new new new new" + System.lineSeparator() + "old old old old");) {
  14. int keyItr = 0;
  15. while (x.hasNext()) {
  16. String a = x.nextLine();
  17.  
  18. if (x.hasNext()) {
  19. a += System.lineSeparator();
  20. }
  21.  
  22. for (int i = 0; i < a.length(); i++) {
  23. // XOR
  24. int temp = a.charAt(i) ^ key.charAt(keyItr);
  25.  
  26. encrypHexa += String.format("%02x", (byte) temp);
  27. keyItr++;
  28. if (keyItr == key.length()) {
  29. keyItr = 0;
  30. }
  31. }
  32. }
  33. }
  34. System.out.println("Encrypted is: " + encrypHexa);
  35. return encrypHexa;
  36. }
  37.  
  38. static void decrypt(String f, String key) {
  39. String hexiToDeci = "";
  40. String decrypText = "";
  41. try (Scanner x = new Scanner(f)) {
  42. while (x.hasNext()) {
  43.  
  44. String a = x.nextLine();
  45.  
  46. for (int i = 0; i < a.length() - 1; i += 2) {
  47.  
  48. String output = a.substring(i, i + 2);
  49.  
  50. int decimal = Integer.parseInt(output, 16);
  51.  
  52. hexiToDeci += (char) decimal;
  53.  
  54. }
  55. // Decrypt with XOR
  56. int keyItr = 0;
  57. for (int i = 0; i < hexiToDeci.length(); i++) {
  58. // XOR
  59. int temp = hexiToDeci.charAt(i) ^ key.charAt(keyItr);
  60. decrypText += (char) temp;
  61. keyItr++;
  62. if (keyItr == key.length()) {
  63. keyItr = 0;
  64. }
  65. }
  66. }
  67. }
  68. System.out.println("Decrypted is: " + decrypText);
  69. }
  70.  
  71. public static void main (String[] args) throws java.lang.Exception
  72. {
  73. String key = "Qwertyuiop[123$4$567]";
  74. String x = encrypt(key);
  75. decrypt(x, key);
  76. }
  77. }
Success #stdin #stdout 0.07s 2184192KB
stdin
Standard input is empty
stdout
Encrypted is: 3f1212521a1c024901152c115c56533e4b595217323d13451d181d55060314
Decrypted is: new new new new
old old old old