Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Encryption using Caesar Cipher of shift 13 i.e. Rot13
- /*Assumptions:
- * The decryption programme '-' means space.
- * Any character from 52 to 64 from ASCII table is not in the text
- * Any character from 91 to 96 from ASCII table is not in the text
- * */
- //Need to import these for file reading
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.stream.Collectors;
- //For out.println
- import static java.lang.System.out;
- //This small programme assigns the variable 'rawText' to the text in test.txt file.
- public class ReadFile {
- public static void main(String[] args) throws IOException {
- String rawText = Files.lines(Paths.get("c:\\fileToBeEncrypted.txt")).collect(Collectors.joining(""));
- int lenOfText = rawText.length();
- int i , bigL, bigL2, smallL, smallL2;
- for (i = 0; i < lenOfText; i++) {
- //Refer to ASCII table
- //First two 'if' and 'else if' checks are for Capital letters
- if (rawText.charAt(i) + 0 <= 77) {
- bigL = rawText.charAt(i) + 13;
- //Convert number to character from ASCII table
- out.print((char) bigL);
- //In ASCII table 'N' is 78, if we add 13 to 78 then we get 91 which is '['.
- //However we want to go back to A, B, ... so we add 13 and subtract 26 which means overall we subtract 13.
- } else if (rawText.charAt(i) + 0 > 77 && rawText.charAt(i) + 0 <= 90) {
- bigL2 = rawText.charAt(i) - 13;
- out.print((char) bigL2);
- //Now we check for small letters and deal with them the same way as capital letters
- } else if (rawText.charAt(i) + 0 >= 97 && rawText.charAt(i) + 0 <= 109) {
- smallL = rawText.charAt(i) + 13;
- out.print((char) smallL);
- }else if (rawText.charAt(i) + 0 >= 110 && rawText.charAt(i) + 0 <= 122) {
- smallL2 = rawText.charAt(i) - 13;
- out.print((char) smallL2);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement