Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Caesar {
  4. static Random r;
  5. public static void main(String args[]) {
  6. r = new Random();
  7. String text, plaintext, padded, ciphertext, blocked;
  8. String pangram1 = "The quick brown fox jumps over the lazy dog.";
  9. String pangram2 = "Few black taxis drive up major roads on quiet hazy nights.";
  10. String digits = "12345678901234567890123456789012345678901234567890";
  11. String alphabetIn = lexabet('A','Z');
  12.  
  13. System.out.println(rotate("",0)); // nochange
  14. System.out.println(rotate("computer",0)); // nochange
  15. System.out.println(rotate("computer",1));
  16. System.out.println(rotate("computer",8));
  17. System.out.println();
  18.  
  19. System.out.println(removePunctuation(pangram1));
  20. System.out.println(removePunctuation(pangram2));
  21. System.out.println();
  22.  
  23. System.out.println(pad(digits, alphabetIn, 1)); // nochange
  24. System.out.println(pad(digits, alphabetIn, 3));
  25. System.out.println(pad(digits, alphabetIn, 5));
  26. System.out.println(pad(digits, alphabetIn, 10));
  27. System.out.println();
  28.  
  29.  
  30. testIt(pangram1, 5);
  31. testIt(pangram2, 5);
  32. testIt("Attack at dawn.", 5);
  33. testIt("Attack at dawn.", 3);
  34. testIt("When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation. We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.--That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed, --That whenever any Form of Government becomes destructive of these ends, it is the Right of the People to alter or to abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in such form, as to them shall seem most likely to effect their Safety and Happiness. Prudence, indeed, will dictate that Governments long established should not be changed for light and transient causes; and accordingly all experience hath shewn, that mankind are more disposed to suffer, while evils are sufferable, than to right themselves by abolishing the forms to which they are accustomed. But when a long train of abuses and usurpations, pursuing invariably the same Object evinces a design to reduce them under absolute Despotism, it is their right, it is their duty, to throw off such Government, and to provide new Guards for their future security.", 5);
  35.  
  36. // Extra Credit
  37. // System.out.println(keyword(alphabetIn, "MICHIGAN"));
  38. }
  39.  
  40. public static void testIt(String text, int blockSize) {
  41. String alphabetIn = lexabet('A','Z');
  42. String alphabetOut = rotate(alphabetIn, 3);
  43.  
  44. String upper = text.toUpperCase(); // FIXME: Convert to upper case
  45. System.out.println(upper);
  46.  
  47. String plaintext = removePunctuation(upper);
  48. System.out.println(plaintext);
  49.  
  50. String padded = pad(plaintext, alphabetIn, blockSize);
  51. System.out.println(padded);
  52.  
  53. String ciphertext = encode(padded, alphabetIn, alphabetOut);
  54. System.out.println(ciphertext);
  55.  
  56. String decoded = encode(padded, alphabetOut, alphabetIn);
  57. System.out.println(decoded);
  58.  
  59. String blocked = block(ciphertext, blockSize);
  60. System.out.println(ciphertext);
  61.  
  62. output(blocked, 4*(blockSize+1)); // 4 blocks per line
  63. }
  64.  
  65.  
  66. // return a string containing the ASCII characters from start to stop
  67. public static String lexabet(int start, int stop) {
  68. String s = "";
  69. for (int i = start; i <= stop; i++) {
  70. s += (char) i;
  71. }
  72. return s;
  73. }
  74.  
  75.  
  76. // from Lab 5
  77. public static String removePunctuation(String s) {
  78. String t = "";
  79. for(int i=0;i<s.length();i++){
  80. if (s.charAt(i)<='Z' && s.charAt(i)>='A' || s.charAt(i)<='z' && s.charAt(i) >='a'){
  81. t = t + s.charAt(i);
  82. }
  83. }
  84. return t;
  85. }
  86.  
  87.  
  88. // rotate String n places
  89. // code below is a rotation of 1
  90. // FIX THIS CODE
  91. public static String rotate(String s, int n) {
  92. String s$ = "";
  93. String s$$ = "";
  94. String s$$$ = "";
  95. s$ = s.substring(0,(n));
  96. s$$ = s.substring(n,s.length());
  97. s$$$ = s$$ + s$;
  98. return s$$$;
  99. }
  100.  
  101.  
  102. // Pad input string to an even multiple of n
  103. // using random letters from the string alphabet
  104. // See Lab 5
  105. public static String pad(String s, String alphabet, int n) {
  106. String s$ = s;
  107. int m = s.length()%n;
  108. int rem = n-m;
  109. if (m!=0){
  110. for(int i = 0; i<rem; i++){
  111. int pinecone = r.nextInt(alphabet.length());
  112. char a = alphabet.charAt(pinecone);
  113. s$ += a;
  114. }
  115. }
  116. return s$;
  117. }
  118.  
  119.  
  120. // substitution cipher, replacing letters from the
  121. // input alphabet with letters from output alphabet
  122. public static String encode(String s, String alphabetIn, String alphabetOut) {
  123. String s$ = "";
  124. String s$$ = s;
  125. for (int i = 0; i<s.length(); i++){
  126. for (int j = 0; j<alphabetIn.length(); j++){
  127. if (s.charAt(i) == alphabetIn.charAt(j)){
  128. s$ = s$.replace(s.charAt(i), alphabetOut.charAt(j));
  129. }
  130. }
  131. s$$ = s$;
  132. }
  133. return s$$;
  134. }
  135.  
  136.  
  137. // add a space between blocks of n characters
  138. public static String block(String s, int n) {
  139. String s$ = "";
  140. for(int i = 0; i<s.length(); i++){
  141. if(i%n == 0 && i!=0){
  142. s$ += " ";
  143. }
  144. s$ += s.charAt(i);
  145. }
  146. return s$;
  147. }
  148.  
  149.  
  150. // print n characters per line
  151. public static void output(String s, int n) {
  152. int placehold = 0;
  153. for(int i = 0; i<s.length(); i++){
  154. if (i%n == 0 && i!=0){
  155. System.out.println(s.substring(placehold,i));
  156. placehold = i;
  157. }
  158. }
  159. }
  160.  
  161. // Extra Credit
  162. // implementa keyword cipher table
  163. public static String keyword(String alphabet, String keyword) {
  164. return alphabet;
  165. }
  166.  
  167.  
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement