Advertisement
Guest User

Cipher

a guest
Jul 24th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. Scanner keyboard = new Scanner(System.in); //DAD'S CODE
  2.  
  3. String userText;
  4. int shift = 0;
  5. String newText = null;
  6.  
  7. userText = keyboard.nextLine();
  8. shift = keyboard.nextInt();
  9. int userTextnum = userText.length();
  10. System.out.println(userTextnum);
  11. String encrypted = null;
  12. String decrypted = null;
  13. char [] userTextChars = userText.toCharArray();
  14. char [] userTextChars2 = userText.toCharArray();
  15. if (userText != null) {
  16.  
  17.  
  18. for (int k = 0; ; k++) {
  19.  
  20. char userChar = userText.charAt(k);
  21. System.out.println(userChar);
  22. int usernum = (int) userChar;
  23. System.out.println(usernum);
  24. int rem = (usernum + shift) % 126;
  25.  
  26. System.out.println(rem);
  27. userTextChars[k] = (char) rem;
  28. userText = String.valueOf(userTextChars);
  29. encrypted = userText;
  30. if (k == userTextnum -1 ) {
  31. for (int l = 0; ; l++) {
  32. char userChar2 = userText.charAt(l);
  33. int usernum2 = (int) userChar2;
  34. System.out.println(userChar2);
  35. System.out.println(usernum2 - shift);
  36. usernum2 = ((((usernum2 - shift) % 126) * 126) / 126) - shift;
  37. userTextChars[l] = (char) usernum2;
  38. userText = String.valueOf(userTextChars);
  39. decrypted = userText;
  40. if (l == userTextnum - 1) {
  41. break;
  42. }
  43. }
  44. break;
  45.  
  46. }
  47. }
  48. }
  49. System.out.println(encrypted);
  50. System.out.println(decrypted);
  51. _______________________________________________________________________________________________________________________________________
  52.  
  53. Scanner keyboard = new Scanner(System.in); //MY CODE
  54.  
  55. char [] cipher = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  56.  
  57. String userText;
  58. int shift = 0;
  59.  
  60.  
  61. userText = keyboard.nextLine();
  62. shift = keyboard.nextInt();
  63.  
  64. char [] userTextChars = userText.toCharArray();
  65. if (userText != null) {
  66.  
  67. for (int i = 0; i <= 25; i++) {
  68.  
  69. if (i == userText.length()) {
  70. break;
  71.  
  72. } else {
  73. for (int k = 0; ; k++) {
  74.  
  75. char userChar = userText.charAt(i);
  76.  
  77. if (userChar == cipher[k]) {
  78.  
  79. int recycle = (k + shift) % cipher.length;
  80. userTextChars[i] = cipher[recycle];
  81. userText = String.valueOf(userTextChars);
  82.  
  83. break;
  84.  
  85. }
  86. }
  87.  
  88. }
  89. }
  90. System.out.println(userText);
  91.  
  92.  
  93. }
  94. _______________________________________________________________________________________________________________________________________
  95. Scanner keyboard = new Scanner(System.in); #NEW CODE I WILL USE (SIMPLIFIED)
  96.  
  97. String [] alphabet = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
  98. String wordToNum;
  99. String numToWord;
  100. String encrypt = null;
  101. String decrypt = null;
  102.  
  103. wordToNum = keyboard.nextLine();
  104. numToWord = keyboard.nextLine();
  105.  
  106. if (wordToNum != null) {
  107.  
  108. for (int i = 0; i <= 25; i++){
  109. String loopNum = Integer.toString(i);
  110.  
  111. wordToNum = wordToNum.replaceAll(alphabet[i], loopNum);
  112.  
  113. }
  114.  
  115. }
  116.  
  117. if (numToWord != null) {
  118.  
  119. for (int l = 0; l <= 25; l++){
  120. String loopNum2 = Integer.toString(l);
  121.  
  122. numToWord = numToWord.replaceAll(loopNum2, alphabet[l]);
  123.  
  124. }
  125.  
  126. }
  127.  
  128. System.out.println(wordToNum);
  129. System.out.println(numToWord);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement