Advertisement
Guest User

Untitled

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