Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. //Sakshi Lende
  2. //Cryptography
  3. //Spec: Shift alphabet forwards or backwards depending on inputted key
  4.  
  5. public class Cryptography
  6. {
  7. public int key;
  8. public String ciphertext, plaintext = "";
  9. public char position;
  10. //public String message, encryptedMessage = "";
  11. public char ch;
  12.  
  13. public Cryptography()
  14. {
  15. key = 0;
  16. plaintext = "";
  17. ciphertext = "";
  18. }
  19.  
  20. public Cryptography(int k, String message)
  21. {
  22. key = k;
  23. plaintext = message;
  24. }
  25.  
  26. public String getEncrypt()
  27. {
  28. if (key > 25)
  29. {
  30. return "Please enter a key between 1 and 25";
  31. }
  32. else
  33. {
  34. for(int i = 0; i < plaintext.length(); i++)
  35. {
  36. position = plaintext.charAt(i);
  37.  
  38. if(position >= 'a' && position <= 'z')
  39. {
  40. position = (char)(position + key);
  41.  
  42. if(position > 'z')
  43. {
  44. position = (char)(position - 'z' + 'a' - 1);
  45. }
  46. ciphertext += position;
  47. }
  48.  
  49. else
  50. {
  51. ciphertext += position;
  52. }
  53. }
  54. return ciphertext;
  55. }
  56. }
  57.  
  58. public String getDecrypt()
  59. {
  60. if (key > 25)
  61. {
  62. return "Please enter a key between 1 and 25";
  63. }
  64. else
  65. {
  66. for(int i = 0; i < plaintext.length(); i++)
  67. {
  68. position = plaintext.charAt(i);
  69.  
  70. if(position >= 'a' && position <= 'z')
  71. {
  72. position = (char)(position - key);
  73.  
  74. if(position > 'z')
  75. {
  76. position = (char)(position + 'z' - 'a' + 1);
  77. }
  78. ciphertext += position;
  79. }
  80.  
  81. else
  82. {
  83. ciphertext += position;
  84. }
  85. }
  86. return ciphertext.substring(4);
  87.  
  88. }
  89. }
  90.  
  91. public String test()
  92. {
  93. for(int i = 0; i < plaintext.length(); ++i)
  94. {
  95. ch = plaintext.charAt(i);
  96.  
  97. if(ch >= 'a' && ch <= 'z')
  98. {
  99. ch = (char)(ch + key);
  100.  
  101. if(ch > 'z')
  102. {
  103. ch = (char)(ch - 'z' + 'a' - 1);
  104. }
  105. ciphertext += ch;
  106. }
  107.  
  108. else if(ch >= 'A' && ch <= 'Z')
  109. {
  110. ch = (char)(ch + key);
  111.  
  112. if(ch > 'Z')
  113. {
  114. ch = (char)(ch - 'Z' + 'A' - 1);
  115. }
  116. ciphertext += ch;
  117. }
  118. else
  119. {
  120. ciphertext += ch;
  121. }
  122. }
  123. return ciphertext;
  124. }
  125.  
  126. public String pleaseWork()
  127. {
  128. for(int i = 0; i < plaintext.length(); i++){
  129. ch = plaintext.charAt(i);
  130.  
  131. if(ch >= 'a' && ch <= 'z'){
  132. ch = (char)(ch + key);
  133.  
  134. if(ch > 'z'){
  135. ch = (char)(ch - 'z' + 'a' - 1);
  136. }
  137.  
  138. ciphertext += ch;
  139. }
  140. else {
  141. ciphertext += ch;
  142. }
  143. }
  144.  
  145. return ciphertext.substring(4);
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement