Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. package cryptovigenere;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CryptoVigenere {
  6.  
  7. private String inputString;
  8. private String passString;
  9. //tablica składa sie ze 123 elementów ponieważ odpowiednikiem znaku jest jego wartość jako integer np a=97 z=123
  10. //bez znaków specjalnych jedynie spacja.
  11. private char[][] alphabetTable = new char['z' + 1]['z' + 1];
  12. private Scanner console = new Scanner(System.in);
  13.  
  14. /**
  15. * Glowne wejscie do programu
  16. */
  17. public static void main(String[] args) {
  18. CryptoVigenere app = new CryptoVigenere();
  19. app.createAhabetTable();
  20. app.getDataFromConsole();
  21.  
  22. }
  23.  
  24. /**
  25. * Tworzy tablice Vigenera
  26. */
  27. public void createAhabetTable() {
  28. System.out.println("Tworze tablice Vigenera.");
  29.  
  30. for (char a = 'a'; a <= 'z'; a++) {
  31. System.out.println("");
  32. char b = a;
  33. for (int z = 'a'; z <= 'z'; z++) {
  34. if (b == 'z' + 1) {
  35. b = 'a';
  36. }
  37. alphabetTable[a][z] = b;
  38. System.out.print(alphabetTable[a][z] + ",");
  39. b++;
  40.  
  41. }
  42.  
  43. }
  44. }
  45.  
  46. /**
  47. * Pobiera dane od uzytkownika
  48. */
  49. public void getDataFromConsole() {
  50. System.out.println("\n\n Wybierz opcje: \n\n 1. Szyfruj\n 2. Deszyfruj\n 3. Koniec\n");
  51. System.out.print("#:");
  52. String option = console.nextLine();
  53. if ("1".equals(option)) {
  54. System.out.println("Podaj text do szyfrowania (max 255 znakow)");
  55. System.out.print("  text:");
  56. inputString = console.nextLine();
  57. System.out.println("\nPodaj Haslo nie krrotsze niz " + inputString.length() + "(max 255 znakow)");
  58. System.out.print("  haslo:");
  59. passString = console.nextLine();
  60. crypt(inputString, passString);
  61. getDataFromConsole();
  62. } else if ("2".equals(option)) {
  63. System.out.println("Podaj text do odszyfrowania (max 255 znakow)");
  64. System.out.print("  text:");
  65. inputString = console.nextLine();
  66. System.out.println("\nPodaj Haslo nie krrotsze niz " + inputString.length() + "(max 255 znakow)");
  67. System.out.print("  haslo:");
  68. passString = console.nextLine();
  69. decrypt(inputString, passString);
  70. getDataFromConsole();
  71. } else if ("3".equals(option)) {
  72. System.out.println("Koniec.....");
  73. } else {
  74. getDataFromConsole();
  75. }
  76.  
  77. }
  78.  
  79. /**
  80. * Szyfruj dane
  81. */
  82. public void crypt(String inputString, String passString) {
  83. char[] textCharTable = inputString.toCharArray();
  84. char[] passCharTable = passString.toCharArray();
  85. System.out.print("\n    Rezultat:");
  86. for (int index = 0; index < textCharTable.length; index++) {
  87. System.out.print(alphabetTable[textCharTable[index]][passCharTable[index]]);
  88. }
  89.  
  90. }
  91.  
  92. /**
  93. * Deszyfruj dane
  94. */
  95. public void decrypt(String inputString, String passString) {
  96. char[] textCharTable = inputString.toCharArray();
  97. char[] passCharTable = passString.toCharArray();
  98.  
  99. System.out.print("\n    Rezultat:");
  100. for (int index = 0; index < textCharTable.length; index++) {
  101. if (textCharTable[index] != ' ') {
  102. for (int z = 'a'; z <= 'z'; z++) {
  103. if (textCharTable[index] == alphabetTable[passCharTable[index]][z]) {
  104. System.out.print(alphabetTable['a'][z]);
  105.  
  106. }
  107. }
  108. } else {
  109. System.out.print(" ");
  110. }
  111. }
  112.  
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement