Advertisement
Hugo-99

Untitled

Jan 20th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class Cipher {
  5.  
  6. public static final int TOTAL_CHAR = 27;
  7. public static final char[] ALPHABET = {' ','a','b','c','d',
  8. 'e','f','g','h','i','j','k',
  9. 'l','m','n','o','p','q','r',
  10. 's','t','u','v','w','x','y','z'};
  11. public static void main(String[] args) {
  12. boolean finished = true;
  13. do {
  14. System.out.print("Please select the following action('decrpyt'/'encrpyt') or enter 'quit' to exit:");
  15. Scanner myScanner = new Scanner(System.in);
  16. String selectedAction = myScanner.next();
  17. char[] cipher = createCipher(ALPHABET);
  18. if(selectedAction.equals("encrpyt"))
  19. {
  20. System.out.print("Enter the word you need to encrpyt:");
  21. myScanner = new Scanner(System.in);
  22. String enteredString = myScanner.nextLine().toLowerCase();
  23. String encrpytedString = encrpyt(enteredString,cipher);
  24. System.out.println(encrpytedString);
  25. }
  26. else if(selectedAction.equals("decrpyt"))
  27. {
  28. System.out.print("Enter the word you need to decrpyt:");
  29. myScanner = new Scanner(System.in);
  30. String enteredString = myScanner.nextLine();
  31. String encrpytedString = decrpyt(enteredString,cipher);
  32. System.out.println(encrpytedString);
  33. }
  34. else if(selectedAction.equals("quit"))
  35. {
  36. finished = false;
  37. }
  38. else
  39. {
  40. System.out.print("Please enter 'decrpyt' or 'encrpyt' to select your action.");
  41. }
  42. }while(finished);
  43.  
  44. }
  45.  
  46. public static char[] createCipher(char[] alphabet)
  47. {
  48. Random generator = new Random(TOTAL_CHAR-1);
  49. int[] indexList = new int[TOTAL_CHAR];
  50. char[] cipher = new char[TOTAL_CHAR];
  51. int cipherIndex,count = 0;
  52. while(count<TOTAL_CHAR)
  53. {
  54. cipherIndex = generator.nextInt(TOTAL_CHAR);
  55. if(indexList[cipherIndex]==0)
  56. {
  57. indexList[cipherIndex] = 1;
  58. cipher[count] = alphabet[cipherIndex];
  59. count++;
  60. }
  61. }
  62. return cipher;
  63. }
  64.  
  65. public static String encrpyt(String word,char[] cipher)
  66. {
  67. char[] characterArray1 = word.toCharArray();
  68. char[] encrpytedCharacter = new char[characterArray1.length];
  69. int tempIndex,index = 0;
  70. while(index<characterArray1.length)
  71. {
  72. boolean done = true;
  73. for(int count = 0; ((count<ALPHABET.length)&&(done));count++)
  74. {
  75. if(characterArray1[index] == ALPHABET[count])
  76. {
  77. tempIndex = count;
  78. done = false;
  79. encrpytedCharacter[index] = cipher[count];
  80. }
  81. }
  82. index++;
  83. }
  84. String encrpytedString = new String(encrpytedCharacter);
  85. return encrpytedString;
  86. }
  87.  
  88. public static String decrpyt(String encrpytWord,char[] cipher)
  89. {
  90. char[] characterArray2 = encrpytWord.toCharArray();
  91. char[] decrpytedCharacter = new char[characterArray2.length];
  92. int tempIndex,index = 0;
  93. while(index<characterArray2.length)
  94. {
  95. boolean done = true;
  96. for(int count = 0; ((count<cipher.length)&&(done));count++)
  97. {
  98. if(characterArray2[index] == cipher[count])
  99. {
  100. tempIndex = count;
  101. done = false;
  102. decrpytedCharacter[index] = ALPHABET[count];
  103. }
  104. }
  105. index++;
  106. }
  107. String decrpytedString = new String(decrpytedCharacter);
  108. return decrpytedString;
  109. }
  110.  
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement