-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoManager.java
168 lines (98 loc) · 3.65 KB
/
CryptoManager.java
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
public class CryptoManager extends {
private static final char LOWER_BOUND = 32;
private static final char UPPER_BOUND = 95;
private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1;
/**
* This method determines if a string is within the allowable bounds of ASCII codes
* according to the LOWER_BOUND and UPPER_BOUND characters
* @param plainText a string to be encrypted, if it is within the allowable bounds
* @return true if all characters are within the allowable bounds, false if any character is outside
*/
public static boolean stringInBounds (String plainText) {
{
boolean flag=true;
for(int i=0;i<plainText.length();i++)
{
if(!((int)plainText.charAt(i)>=LOWER_BOUND && (int)plainText.charAt(i)<=UPPER_BOUND)){
flag=false;
break;
}
}
return flag;}
}
/**
* Encrypts a string according to the Caesar Cipher. The integer key specifies an offset
* and each character in plainText is replaced by the character \"offset\" away from it
* @param plainText an uppercase string to be encrypted.
* @param key an integer that specifies the offset of each character
* @return the encrypted string
*/
public static String encryptCaesar(String plainText, int key) {
key=Wrap_around(key);
String result="";
for(int i=0;i<plainText.length();i++)
{
result+=Character.toString((char) ((int)plainText.charAt(i)+key));
}
return result;
}
public static String decryptCaesar (String encryptedText, int key) {
key=Wrap_around(key);
String orgn="";
for(int i=0;i<encryptedText.length();i++)
{
orgn+=Character.toString((char) ((int)encryptedText.charAt(i)-key));
}
return orgn;
}
public static int Wrap_around(int keyb)
{
while(keyb>UPPER_BOUND)
{
keyb-=(UPPER_BOUND-LOWER_BOUND);
}
return keyb;
}
/**
* Encrypts a string according 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
* @param plainText an uppercase string to be encrypted.
* @param bellasoStr an uppercase string that specifies the offsets, character by character.
* @return the encrypted string
*/
public static String encryptBellaso(String plainText, String bellasoStr) {
String result="";
while(bellasoStr.length()<plainText.length())
{
bellasoStr+=bellasoStr.substring(0,(plainText.length()-bellasoStr.length()));
}
for(int i=0;i<plainText.length();i++)
{
char c=(char)Wrap_around((int)plainText.charAt(i)+(int)bellasoStr.charAt(i) );
result+=Character.toString(c);
}
return result;
}
/**
* 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.
* @param encryptedText an encrypted string to be decrypted.
* @param key an integer that specifies the offset of each character
* @return the plain text string
*/
public static String decryptBellaso(String encryptedText, String bellasoStr) {
String result="";
while(bellasoStr.length()<encryptedText.length())
{
bellasoStr+=bellasoStr.substring(0,(encryptedText.length()-bellasoStr.length()));
}
for(int i=0;i<encryptedText.length();i++)
{
char c=(char)Wrap_around((int)encryptedText.charAt(i)-(int)bellasoStr.charAt(i) );
result+=Character.toString(c);
}
return result;
}
}