Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. //Encryption using Caesar Cipher of shift 13 i.e. Rot13
  2. /*Assumptions:
  3. * The decryption programme '-' means space.
  4. * Any character from 52 to 64 from ASCII table is not in the text
  5. * Any character from 91 to 96 from ASCII table is not in the text
  6. * */
  7.  
  8.  
  9. //Need to import these for file reading
  10. import java.io.IOException;
  11. import java.nio.file.Files;
  12. import java.nio.file.Paths;
  13. import java.util.stream.Collectors;
  14.  
  15. //For out.println
  16. import static java.lang.System.out;
  17.  
  18. //This small programme assigns the variable 'rawText' to the text in test.txt file.
  19. public class ReadFile {
  20. public static void main(String[] args) throws IOException {
  21. String rawText = Files.lines(Paths.get("c:\\fileToBeEncrypted.txt")).collect(Collectors.joining(""));
  22.  
  23. int lenOfText = rawText.length();
  24.  
  25. int i , bigL, bigL2, smallL, smallL2;
  26. for (i = 0; i < lenOfText; i++) {
  27.  
  28. //Refer to ASCII table
  29.  
  30. //First two 'if' and 'else if' checks are for Capital letters
  31. if (rawText.charAt(i) + 0 <= 77) {
  32. bigL = rawText.charAt(i) + 13;
  33.  
  34. //Convert number to character from ASCII table
  35. out.print((char) bigL);
  36.  
  37.  
  38. //In ASCII table 'N' is 78, if we add 13 to 78 then we get 91 which is '['.
  39. //However we want to go back to A, B, ... so we add 13 and subtract 26 which means overall we subtract 13.
  40. } else if (rawText.charAt(i) + 0 > 77 && rawText.charAt(i) + 0 <= 90) {
  41. bigL2 = rawText.charAt(i) - 13;
  42. out.print((char) bigL2);
  43.  
  44.  
  45. //Now we check for small letters and deal with them the same way as capital letters
  46. } else if (rawText.charAt(i) + 0 >= 97 && rawText.charAt(i) + 0 <= 109) {
  47. smallL = rawText.charAt(i) + 13;
  48.  
  49.  
  50. out.print((char) smallL);
  51.  
  52.  
  53. }else if (rawText.charAt(i) + 0 >= 110 && rawText.charAt(i) + 0 <= 122) {
  54. smallL2 = rawText.charAt(i) - 13;
  55.  
  56. out.print((char) smallL2);
  57. }
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement