Kaynağa Gözat

almost finished

Nicholas Maidanos 6 yıl önce
ebeveyn
işleme
55a1fe5584
4 değiştirilmiş dosya ile 57 ekleme ve 54 silme
  1. 29
    39
      Crypto/src/main/java/ROT13.java
  2. 14
    0
      sonnet18.dec
  3. 14
    14
      sonnet18.enc
  4. 0
    1
      sonnet18dec.txt

+ 29
- 39
Crypto/src/main/java/ROT13.java Dosyayı Görüntüle

@@ -11,14 +11,21 @@ public class ROT13  {
11 11
     int rotateBy = 13;
12 12
 
13 13
     private char[] rotatedAlphbet;
14
-    private char[] alphChars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
14
+
15
+    private String alphbet = "abcdefghijklmnopqrstuvwxyz";
16
+    private char[] alphChars = alphbet.toCharArray();
17
+
18
+
15 19
 
16 20
     ROT13(Character cs, Character cf) {
17 21
         rotateBy = numbersByCount(cs, cf);
18 22
         rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz").toCharArray();
19 23
     }
20 24
 
21
-    ROT13() {}
25
+    ROT13() {
26
+        rotateBy = 13;
27
+        rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz").toCharArray();
28
+    }
22 29
 
23 30
     public static int numbersByCount(Character cs, Character cf){
24 31
         String alphabet = "abcdefghijklmnopqrstuvwxyz";
@@ -41,40 +48,26 @@ public class ROT13  {
41 48
     }
42 49
 
43 50
     public static void main(String[] args){
44
-        ROT13 rot13 = new ROT13('a','n');
51
+        ROT13 rot13 = new ROT13('a', 'm');
52
+        System.out.println(rot13.rotateBy);
45 53
         rot13.encryptFile("sonnet18.txt");
46 54
         rot13.decryptFile("sonnet18.enc");
47
-        //System.out.println(new ROT13().crypt("Gb trg gb gur bgure fvqr!"));
48 55
     }
49 56
 
50 57
     public String crypt(String text) throws UnsupportedOperationException {
51
-        StringBuilder sb = new StringBuilder();
52 58
         char[] cars = text.toCharArray();
59
+        StringBuilder sb =  new StringBuilder();
53 60
 
54 61
         for(char car: cars){
55 62
             if(Character.isLetter(car)){
56
-                if(Character.isUpperCase(car)){
57
-                    sb.append(Character.toUpperCase(charShift(car)));
58
-                }else {
59
-                    sb.append(charShift(car));
60
-                }
63
+                sb.append(this.rotate(this.alphbet, car).toCharArray()[rotateBy]);
61 64
             } else{
62 65
                 sb.append(car);
63 66
             }
64 67
         }
65
-        return sb.toString();
66 68
 
67
-    }
69
+        return sb.toString();
68 70
 
69
-    private Character charShift(char c){
70
-        Character car = null;
71
-        for(int i = 0; i < this.alphChars.length; i++) {
72
-            if(this.alphChars[i] == Character.toLowerCase(c)){
73
-                car = this.rotatedAlphbet[i];
74
-                break;
75
-            }
76
-        }
77
-        return car;
78 71
     }
79 72
 
80 73
     public String encrypt(String text) {
@@ -82,25 +75,19 @@ public class ROT13  {
82 75
     }
83 76
 
84 77
     public String decrypt(String text) {
85
-
86 78
         char[] cars = text.toCharArray();
87
-
88 79
         StringBuilder sb =  new StringBuilder();
89 80
 
90
-        for(int i = 0; i < cars.length; i++){
91
-            if(Character.isLetter(cars[i])){
92
-                sb.append(cars[i]);
93
-                if(rotateBy - i < 0){
94
-                    System.out.println(rotateBy -1);
95
-                    sb.append(rotatedAlphbet[(rotateBy - i) + (rotatedAlphbet.length -1)]);
96
-                } else {
97
-                    sb.append(rotatedAlphbet[rotateBy - i]);
98
-                }
99
-            } else {
100
-                sb.append(cars[i]);
81
+        for(char car: cars){
82
+            if(Character.isLetter(car)){
83
+                sb.append(this.rotate(this.alphbet, car).toCharArray()[this.alphChars.length - rotateBy]);
84
+            } else{
85
+                sb.append(car);
101 86
             }
102 87
         }
88
+
103 89
         return sb.toString();
90
+
104 91
     }
105 92
 
106 93
     public String rotate(String s) {
@@ -119,11 +106,14 @@ public class ROT13  {
119 106
         return new String(cars);
120 107
     }
121 108
 
122
-    public static String rotate(String s, Character c) {
109
+    public String rotate(String s, Character c) {
110
+        char temp;
111
+        boolean isUpper = false;
123 112
         char[] cars = s.toCharArray();
113
+        isUpper = Character.isUpperCase(c) ? true : false;
114
+        char car = Character.toLowerCase(c);
124 115
 
125
-        char temp;
126
-        while(cars[0] != c){
116
+        while(cars[0] != car){
127 117
             temp = cars[0];
128 118
             for(int i = 0; i < cars.length -1; i++){
129 119
                 cars[i] = cars[i +1];
@@ -131,7 +121,7 @@ public class ROT13  {
131 121
             cars[cars.length -1] = temp;
132 122
         }
133 123
 
134
-        return new String(cars);
124
+        return isUpper == true ? new String(cars).toUpperCase() : new String(cars);
135 125
     }
136 126
 
137 127
     public void encryptFile(String fileName){
@@ -180,7 +170,7 @@ public class ROT13  {
180 170
             String decryptFile = this.decrypt(sb.toString());
181 171
 
182 172
             try {
183
-                File file = new File("sonnet18dec.txt");
173
+                File file = new File("sonnet18.dec");
184 174
                 FileWriter fileWriter = new FileWriter(file);
185 175
                 fileWriter.write(decryptFile);
186 176
                 fileWriter.flush();

+ 14
- 0
sonnet18.dec Dosyayı Görüntüle

@@ -0,0 +1,14 @@
1
+Shall I compare thee to a summer’s day?
2
+Thou art more lovely and more temperate:
3
+Rough winds do shake the darling buds of May,
4
+And summer’s lease hath all too short a date;
5
+Sometime too hot the eye of heaven shines,
6
+And often is his gold complexion dimm'd;
7
+And every fair from fair sometime declines,
8
+By chance or nature’s changing course untrimm'd;
9
+But thy eternal summer shall not fade,
10
+Nor lose possession of that fair thou ow’st;
11
+Nor shall death brag thou wander’st in his shade,
12
+When in eternal lines to time thou grow’st:
13
+   So long as men can breathe or eyes can see,
14
+   So long lives this, and this gives life to thee.

+ 14
- 14
sonnet18.enc Dosyayı Görüntüle

@@ -1,14 +1,14 @@
1
-Funyy V pbzcner gurr gb n fhzzre’f qnl?
2
-Gubh neg zber ybiryl naq zber grzcrengr:
3
-Ebhtu jvaqf qb funxr gur qneyvat ohqf bs Znl,
4
-Naq fhzzre’f yrnfr ungu nyy gbb fubeg n qngr;
5
-Fbzrgvzr gbb ubg gur rlr bs urnira fuvarf,
6
-Naq bsgra vf uvf tbyq pbzcyrkvba qvzz'q;
7
-Naq rirel snve sebz snve fbzrgvzr qrpyvarf,
8
-Ol punapr be angher’f punatvat pbhefr hagevzz'q;
9
-Ohg gul rgreany fhzzre funyy abg snqr,
10
-Abe ybfr cbffrffvba bs gung snve gubh bj’fg;
11
-Abe funyy qrngu oent gubh jnaqre’fg va uvf funqr,
12
-Jura va rgreany yvarf gb gvzr gubh tebj’fg:
13
-   Fb ybat nf zra pna oerngur be rlrf pna frr,
14
-   Fb ybat yvirf guvf, naq guvf tvirf yvsr gb gurr.
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.

+ 0
- 1
sonnet18dec.txt Dosyayı Görüntüle

@@ -1 +0,0 @@
1
-enczb  yynuF