Browse Source

Finished and tweaked

Nicholas Maidanos 6 years ago
parent
commit
75a214e1b4
2 changed files with 17 additions and 42 deletions
  1. 3
    28
      Crypto/src/main/java/ROT13.java
  2. 14
    14
      sonnet18.enc

+ 3
- 28
Crypto/src/main/java/ROT13.java View File

@@ -15,16 +15,14 @@ public class ROT13  {
15 15
     private String alphbet = "abcdefghijklmnopqrstuvwxyz";
16 16
     private char[] alphChars = alphbet.toCharArray();
17 17
 
18
-
19
-
20 18
     ROT13(Character cs, Character cf) {
21 19
         rotateBy = numbersByCount(cs, cf);
22
-        rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz").toCharArray();
20
+        rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz", cf).toCharArray();
23 21
     }
24 22
 
25 23
     ROT13() {
26 24
         rotateBy = 13;
27
-        rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz").toCharArray();
25
+        rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz", 'n').toCharArray();
28 26
     }
29 27
 
30 28
     public static int numbersByCount(Character cs, Character cf){
@@ -48,8 +46,7 @@ public class ROT13  {
48 46
     }
49 47
 
50 48
     public static void main(String[] args){
51
-        ROT13 rot13 = new ROT13('a', 'm');
52
-        System.out.println(rot13.rotateBy);
49
+        ROT13 rot13 = new ROT13('a', 'b');
53 50
         rot13.encryptFile("sonnet18.txt");
54 51
         rot13.decryptFile("sonnet18.enc");
55 52
     }
@@ -57,7 +54,6 @@ public class ROT13  {
57 54
     public String crypt(String text) throws UnsupportedOperationException {
58 55
         char[] cars = text.toCharArray();
59 56
         StringBuilder sb =  new StringBuilder();
60
-
61 57
         for(char car: cars){
62 58
             if(Character.isLetter(car)){
63 59
                 sb.append(this.rotate(this.alphbet, car).toCharArray()[rotateBy]);
@@ -65,9 +61,7 @@ public class ROT13  {
65 61
                 sb.append(car);
66 62
             }
67 63
         }
68
-
69 64
         return sb.toString();
70
-
71 65
     }
72 66
 
73 67
     public String encrypt(String text) {
@@ -77,7 +71,6 @@ public class ROT13  {
77 71
     public String decrypt(String text) {
78 72
         char[] cars = text.toCharArray();
79 73
         StringBuilder sb =  new StringBuilder();
80
-
81 74
         for(char car: cars){
82 75
             if(Character.isLetter(car)){
83 76
                 sb.append(this.rotate(this.alphbet, car).toCharArray()[this.alphChars.length - rotateBy]);
@@ -85,25 +78,7 @@ public class ROT13  {
85 78
                 sb.append(car);
86 79
             }
87 80
         }
88
-
89 81
         return sb.toString();
90
-
91
-    }
92
-
93
-    public String rotate(String s) {
94
-        char[] cars = s.toCharArray();
95
-
96
-        int looper = rotateBy;
97
-        char temp;
98
-        while(looper != 0){
99
-            temp = cars[0];
100
-            for(int i = 0; i < cars.length -1; i++){
101
-                cars[i] = cars[i +1];
102
-            }
103
-            cars[cars.length -1] = temp;
104
-            looper--;
105
-        }
106
-        return new String(cars);
107 82
     }
108 83
 
109 84
     public String rotate(String s, Character c) {

+ 14
- 14
sonnet18.enc View File

@@ -1,14 +1,14 @@
1
-Etmxx U oaybmdq ftqq fa m egyyqd’e pmk?
2
-Ftag mdf yadq xahqxk mzp yadq fqybqdmfq:
3
-Dagst iuzpe pa etmwq ftq pmdxuzs ngpe ar Ymk,
4
-Mzp egyyqd’e xqmeq tmft mxx faa etadf m pmfq;
5
-Eayqfuyq faa taf ftq qkq ar tqmhqz etuzqe,
6
-Mzp arfqz ue tue saxp oaybxqjuaz puyy'p;
7
-Mzp qhqdk rmud rday rmud eayqfuyq pqoxuzqe,
8
-Nk otmzoq ad zmfgdq’e otmzsuzs oagdeq gzfduyy'p;
9
-Ngf ftk qfqdzmx egyyqd etmxx zaf rmpq,
10
-Zad xaeq baeeqeeuaz ar ftmf rmud ftag ai’ef;
11
-Zad etmxx pqmft ndms ftag imzpqd’ef uz tue etmpq,
12
-Itqz uz qfqdzmx xuzqe fa fuyq ftag sdai’ef:
13
-   Ea xazs me yqz omz ndqmftq ad qkqe omz eqq,
14
-   Ea xazs xuhqe ftue, mzp ftue suhqe xurq fa ftqq.
1
+Tibmm J dpnqbsf uiff up b tvnnfs’t ebz?
2
+Uipv bsu npsf mpwfmz boe npsf ufnqfsbuf:
3
+Spvhi xjoet ep tiblf uif ebsmjoh cvet pg Nbz,
4
+Boe tvnnfs’t mfbtf ibui bmm upp tipsu b ebuf;
5
+Tpnfujnf upp ipu uif fzf pg ifbwfo tijoft,
6
+Boe pgufo jt ijt hpme dpnqmfyjpo ejnn'e;
7
+Boe fwfsz gbjs gspn gbjs tpnfujnf efdmjoft,
8
+Cz dibodf ps obuvsf’t dibohjoh dpvstf vousjnn'e;
9
+Cvu uiz fufsobm tvnnfs tibmm opu gbef,
10
+Ops mptf qpttfttjpo pg uibu gbjs uipv px’tu;
11
+Ops tibmm efbui csbh uipv xboefs’tu jo ijt tibef,
12
+Xifo jo fufsobm mjoft up ujnf uipv hspx’tu:
13
+   Tp mpoh bt nfo dbo csfbuif ps fzft dbo tff,
14
+   Tp mpoh mjwft uijt, boe uijt hjwft mjgf up uiff.