|
@@ -1,32 +1,108 @@
|
|
1
|
+//package com.zipcodewilmington.simplecrypt;
|
|
2
|
+
|
1
|
3
|
import static java.lang.Character.isLowerCase;
|
2
|
4
|
import static java.lang.Character.isUpperCase;
|
3
|
5
|
import static java.lang.Character.toLowerCase;
|
4
|
6
|
|
5
|
7
|
public class ROT13 {
|
|
8
|
+ /*
|
|
9
|
+ ROT13 - take the 26 letters of the alphabet and create a `String <- crypt(String)` method in the ROT13 class
|
|
10
|
+ * crypt("Why did the chicken cross the road?") should produce "Jul qvq gur puvpxra pebff gur ebnq?"
|
|
11
|
+ * crypt("Gb trg gb gur bgure fvqr!") should produce "To get to the other side!"
|
|
12
|
+* Make a constructor that takes two arguments to set the cipher correspondence. `ROT13 superSecure = new ROT13("a","m");`
|
|
13
|
+ * this defines the SHIFT of the two Character arrays.
|
|
14
|
+* Caesar - make a subclass of ROT13 that implements the famous caesar cipher.
|
|
15
|
+* Create you own cipher, using a different set of
|
|
16
|
+ */
|
|
17
|
+ private int shift;
|
6
|
18
|
|
7
|
19
|
ROT13(Character cs, Character cf) {
|
|
20
|
+ this.shift =cs-cf;
|
8
|
21
|
}
|
9
|
22
|
|
10
|
23
|
ROT13() {
|
|
24
|
+ shift=13;
|
11
|
25
|
}
|
12
|
26
|
|
13
|
27
|
|
14
|
28
|
public String crypt(String text) throws UnsupportedOperationException {
|
|
29
|
+ StringBuilder sb=new StringBuilder();
|
|
30
|
+ char[] ch=text.toCharArray();
|
|
31
|
+ for(int i=0;i<ch.length;i++ ) {
|
|
32
|
+ char c = text.charAt(i);
|
|
33
|
+ if (c >= 'a' && c <= 'm') {
|
|
34
|
+ c += 13;
|
|
35
|
+ } else if (c >= 'A' && c <= 'M') {
|
|
36
|
+ c += 13;
|
|
37
|
+ } else if (c >= 'n' && c <= 'z') {
|
|
38
|
+ c -= 13;
|
|
39
|
+ } else if (c >= 'N' && c <= 'Z') {
|
|
40
|
+ c-=13;
|
|
41
|
+ }
|
|
42
|
+ sb.append(c);
|
|
43
|
+ }
|
15
|
44
|
|
16
|
|
- return "";
|
|
45
|
+ return sb.toString() ;
|
17
|
46
|
}
|
18
|
47
|
|
19
|
48
|
public String encrypt(String text) {
|
20
|
|
- return text;
|
|
49
|
+
|
|
50
|
+ /* StringBuffer result= new StringBuffer();
|
|
51
|
+
|
|
52
|
+ for (int i=0; i<text.length(); i++)
|
|
53
|
+ {
|
|
54
|
+ if (Character.isUpperCase(text.charAt(i)))
|
|
55
|
+ {
|
|
56
|
+ char ch = (char)(((int)text.charAt(i) +
|
|
57
|
+ 13 - 65) % 26 + 65);
|
|
58
|
+ result.append(ch);
|
|
59
|
+ result.append(" ");
|
|
60
|
+ }
|
|
61
|
+ else
|
|
62
|
+ {
|
|
63
|
+ char ch = (char)(((int)text.charAt(i) +
|
|
64
|
+ 13 - 97) % 26 + 97);
|
|
65
|
+ result.append(ch);
|
|
66
|
+ result.append(" ");
|
|
67
|
+ }
|
|
68
|
+ }
|
|
69
|
+ return result.toString() ;*/
|
|
70
|
+ return crypt(text) ;
|
21
|
71
|
}
|
22
|
72
|
|
23
|
73
|
public String decrypt(String text) {
|
24
|
|
- return text;
|
|
74
|
+ /* StringBuffer result= new StringBuffer();
|
|
75
|
+
|
|
76
|
+ for (int i=0; i<text.length(); i++)
|
|
77
|
+ {
|
|
78
|
+ if (Character.isUpperCase(text.charAt(i)))
|
|
79
|
+ {
|
|
80
|
+ char ch = (char)(((int)text.charAt(i) +
|
|
81
|
+ 13 - 65) % 26 + 65);
|
|
82
|
+ result.append(ch);
|
|
83
|
+ }
|
|
84
|
+ else
|
|
85
|
+ {
|
|
86
|
+ char ch = (char)(((int)text.charAt(i) +
|
|
87
|
+ 13 - 97) % 26 + 97);
|
|
88
|
+ result.append(ch);
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+ return result.toString() ;*/
|
|
92
|
+ return crypt(text) ;
|
|
93
|
+
|
|
94
|
+
|
25
|
95
|
}
|
26
|
96
|
|
27
|
97
|
public static String rotate(String s, Character c) {
|
28
|
98
|
|
29
|
|
- return "";
|
|
99
|
+ /*int n=s.indexOf(c);
|
|
100
|
+ String strPrefix =s.substring(n,s.length());
|
|
101
|
+ String strsuffix=s.substring(0,n-1);*/
|
|
102
|
+ String strPrefix=s.substring(0,s.indexOf(c) ) ;
|
|
103
|
+ String strsuffix =s.substring(s.indexOf(c) );
|
|
104
|
+
|
|
105
|
+ return strsuffix+strPrefix ;
|
30
|
106
|
}
|
31
|
107
|
|
32
|
108
|
}
|