-
Notifications
You must be signed in to change notification settings - Fork 0
/
ass3.txt
235 lines (118 loc) · 4.45 KB
/
ass3.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
Explanation:
//CryptoManager.java
public class CryptoManager {
static int LOWER_BOUND=32;
static int UPPER_BOUND=95;
/*This method determines if a string is within the allowable bounds of ASCII
codes according to the LOWER_BOUND and UPPER_BOUND characters. The parameter
plainText is the string to be encrypted. The method returns true if all
characters are within the allowable bounds, false if any character is outside.*/
public static boolean stringInBounds (String plainText)
{
boolean flag=true;
//determines if a string is within the allowable bounds of ASCII
//codes according to the LOWER_BOUND and UPPER_BOUND characters.
for(int i=0;i<plainText.length();i++)
{
if(!((int)plainText.charAt(i)>=LOWER_BOUND && (int)plainText.charAt(i)<=UPPER_BOUND))
{ //false if any character is outside the bounds
flag=false;
break;
}
}
//returns true if all characters are within the allowable bounds
return flag;
}
/*This method encrypts a string according to the Caesar Cipher. The integer key
specifies an offset and each character in plainText is replaced by the character
the specified distance away from it. The parameter plainText is an uppercase
string to be encrypted. The parameter key is an integer that specifies the
offset of each character. The method returns the encrypted string.*/
public static String encryptCaesar(String plainText, int key)
{
//Wrap around the key, if it is greater than the UPPER_BOUND
key=Wrap_around(key);
//encrypted text
String res="";
//encryption
for(int i=0;i<plainText.length();i++)
{
res+=Character.toString((char) ((int)plainText.charAt(i)+key));
}
//return result
return res;
}
/* This method decrypts a string according to the Caesar Cipher. The integer
key specifies an offset and each character in encryptedText is replaced by
the character "offset" characters before it. This is the inverse of the
encryptCaesar method. The parameter encryptedText is the encrypted string
to be decrypted, and key is the integer used to encrypt the original text.
The method returns the original plain text string.*/
public static String decryptCaesar(String encryptedText, int key){
//Wrap around the key, if it is greater than the UPPER_BOUND
key=Wrap_around(key);
//decrypted text
String org="";
//encryption
for(int i=0;i<encryptedText.length();i++)
{
org+=Character.toString((char) ((int)encryptedText.charAt(i)-key));
}
//return result
return org;
}
public static int Wrap_around(int key)
{
while(key>UPPER_BOUND)
{
key-=(UPPER_BOUND-LOWER_BOUND);
}
return key;
}
/* This method encrypts a string according to the Bellaso Cipher. Each character
in plainText is offset according to the ASCII value of the corresponding
character in bellasoStr, which is repeated to correspond to the length of
plaintext. The method returns the encrypted string.*/
public static String encryptBellaso(String plainText, String bellasoStr)
{
//encrypted text
String res="";
//Adjust length of bellasoStr to plainText
while(bellasoStr.length()<plainText.length())
{
bellasoStr+=bellasoStr.substring(0,(plainText.length()-bellasoStr.length()));
}
//encryption
for(int i=0;i<plainText.length();i++)
{
char c=(char)Wrap_around((int)plainText.charAt(i)+(int)bellasoStr.charAt(i) );
res+=Character.toString(c);
}
//return result
return res;
}
/*
This method decrypts a string according to the Bellaso Cipher. Each character
in encryptedText is replaced by the character corresponding to the character in
bellasoStr, which is repeated to correspond to the length of plainText. This is
the inverse of the encryptBellaso method. The parameter encryptedText is the
encrypted string to be decrypted, and bellasoStr is the string used to encrypt
the original text. The method returns the original plain text string.*/
public static String decryptBellaso(String encryptedText, String bellasoStr)
{
//decrypted text
String res="";
//Adjust length of bellasoStr to plainText
while(bellasoStr.length()<encryptedText.length())
{
bellasoStr+=bellasoStr.substring(0,(encryptedText.length()-bellasoStr.length()));
}
//decryption
for(int i=0;i<encryptedText.length();i++)
{
char c=(char)Wrap_around((int)encryptedText.charAt(i)-(int)bellasoStr.charAt(i) );
res+=Character.toString(c);
}
//return result
return res;
}