Guest User

Untitled

a guest
Jun 18th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1.  
  2. public class Encrypt
  3. {
  4. //instance field
  5. String keyword; //"elvispresley"
  6. String alphabet;
  7. String plaintext; //"all your base are belong to us"
  8. int encrypted[];
  9. int length;
  10. public char ch;
  11. //constructor
  12. public Encrypt(String k, String p)
  13. { //assigning the Strings
  14. keyword = "elvispresley";
  15. putKeyword();
  16. String alphabet = "e l v i s p r y a b c d f g h j k m n o p q r t u w x z";
  17. buildAlphabet();
  18. plaintext = "all your base are belong to us";
  19. encrypted = new int[plaintext.length()];
  20. encrypt();
  21. }
  22.  
  23.  
  24. public void putKeyword()
  25. {
  26. StringBuffer alpha = new StringBuffer();
  27. alpha.append(keyword.charAt(0));
  28. for(int i = 1; i < keyword.length(); i++ )
  29. {
  30. boolean found = false;
  31. for(int j = 0; j < alpha.length(); j++)
  32. {
  33. if(keyword.charAt(j) == keyword.charAt(i))
  34. {
  35. found = true;
  36. }
  37. }
  38. if(!found)
  39. {
  40. alpha.append(keyword.charAt(i));
  41. }
  42.  
  43. }
  44. alpha.toString();
  45. alphabet = alpha.toString();
  46. }
  47.  
  48. public void buildAlphabet()
  49. {
  50. StringBuffer alpha = new StringBuffer();
  51. int len = alpha.length();
  52. for(char ch = 'a'; ch <= 'z'; ch++)
  53. {
  54. boolean found = false;
  55. for(int j = 0; j < len; j++)
  56. {
  57. if(alpha.charAt(j) == ch)
  58. {
  59. found = true;
  60. }
  61. else
  62. {
  63. alpha.append(keyword.charAt(ch));
  64. }
  65. alphabet = alpha.toString();
  66. }
  67. }
  68. }
  69.  
  70. public void encrypt()
  71. {
  72. for(int i = 0; i < plaintext.length(); i++)
  73. {
  74. encrypted[i] = getIndex(plaintext.charAt(i));
  75. }
  76. }
  77. public int getIndex(char ch)
  78. {
  79. int o = 0;
  80. for(int i = 0; i < alphabet.length(); i++)
  81. {
  82.  
  83. if(ch == alphabet.charAt(i))
  84. o = i;
  85. }
  86. return o;
  87. }
  88. public String getAlphabet()
  89. {
  90. return alphabet;
  91. }
  92.  
  93. public int[] getEncrypted()
  94. {
  95. return encrypted;
  96. }
  97.  
  98. public void printEncrypted()
  99. {
  100. final StringBuffer buf = new StringBuffer();
  101. buf.append("Array");
  102. for (int i = 0; i < length; i++)
  103. {
  104. if (i != 0) buf.append(", ");
  105. buf.append(encrypted[i]);
  106. }
  107. }
  108. }
Add Comment
Please, Sign In to add comment