-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipher.java
More file actions
146 lines (146 loc) · 5.72 KB
/
Cipher.java
File metadata and controls
146 lines (146 loc) · 5.72 KB
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
/**
* This program is used as an introduction to ciphers.
* This program checks the validity of inputs if they are lower case and
* satisfy the rest of the constraints. This program also encodes and decodes
* characters using the caesar cipher methods, as well as encodes and decodes
* strings using established keys in the vignere cipher methods.
**/
import java.util.*;
/**
This cipher class contains all of the cipher and validity checking methods.
It contains the main methods and other methods to decode and encode both
characters and strings using caesar ciphers and vignere ciphers
**/
public class Cipher {
/**
main method which was used to test the methods.
**/
public static void main(String[]args){
}
/**
This isLowerCase method checks to see if the provided character is lowecase
and not any special character
**/
public static boolean isLowerCase(char letter){
int asciiLetter = (int) letter;
int asciiSpace = (int)' ';
int asciiA = (int)'a';
int asciiZ = (int)'z';
//Ensures the ascii value of the letter is between the ascii values of a &z
if(asciiLetter>=asciiA && asciiLetter<=asciiZ){
return true;
}else{
return false;
}
}
/**
This isLowerCase method checks to see if the provided string is lowercase
and does not contain any special characters or upper case letters
**/
public static boolean isLowerCase(String str){
// invalid input
if(str==null){
return false;
}
//loops through string using the isLowerCase method to check each character
//if it is lowercase or not
for(int i=0; i<str.length(); i++){
if(isLowerCase(str.charAt(i))==false){
return false;
}
}
return true;
}
/**
This caesarShiftEncode method takes in a character and encodes its value
based on the key given. It is used to create secure messages.
**/
public static char caesarShiftEncode(char plaintext, char key){
int asciiPlainText = (int) plaintext;
int asciiKey = (int) key;
int asciiA = (int)'a';
int asciiZ = (int)'z';
//ensures the character and key are both lowercase before executing
if(isLowerCase(plaintext)==true&&isLowerCase(key)==true){
int numInAlphabet = 26;
// adds ascii value of plaintext values and key values finding the modulus
// to get the encodingVal and using modulus to find the ascii value added to
// the ascii value of A between a & z
int encodingVal = (int) plaintext +(int) key;
int remainderInt = (encodingVal%asciiA)%numInAlphabet;
char plaintextChar = (char)(remainderInt+asciiA);
return plaintextChar;
}
return plaintext;
}
/**
This caesarShiftDecode method takes in a character and decodes its value
based on the key given. It is used to decode messages that were encoded
by the caesarShiftDecode method
**/
public static char caesarShiftDecode(char ciphertext, char key){
int asciiCipherText = (int) ciphertext;
int asciiKey = (int) key;
int asciiA = (int)'a';
int asciiZ = (int)'z';
// ensures ciphertext and key characters are both lowercase
if(isLowerCase(ciphertext)==true&&isLowerCase(key)==true){
int numInAlphabet = 26;
int difference = (int)ciphertext - (int)key;
// determining if need to loop through the alphabet or introduction
// loops through alphabet subtracting the difference
if(difference<0){
char cipherTextChar = (char)(numInAlphabet+difference+asciiA);
return cipherTextChar;
}
// when key has a greater ascii value of ciphertext
char cipherChar = (char)(difference + asciiA);
return cipherChar;
}
return ciphertext;
}
/**
This vigenereEncode method encodes a string based on a key given in the
form of a string. It loops through each character in the string encoding
each character value to a different one based upon the corresponding
character in the key. If the key has less characters than the given
plaintext, the key will repeat itself to fill the remaining indexes
**/
public static String vigenereEncode(String plaintext, String key){
String ciphertext="";
// checks to see if plaintext and key are both lowercase
// makes sure key is not an empty string
if(isLowerCase(plaintext)&& isLowerCase(key)&&key!=""){
// loops through each character in the string using the caesarShiftEncode
// to encode each character with the corresponding key value character
for(int i=0;i<plaintext.length();i++){
ciphertext= ciphertext +
caesarShiftEncode(plaintext.charAt(i),key.charAt(i%key.length()));
}
return ciphertext;
}
return plaintext;
}
/**
This vigenereDecode method decodes a string based on a key given in the
form of a string. It loops through each character in the string decoding
each character value to its plaintext char based upon the corresponding
character in the key. If the key has less characters than the given
ciphertext, the key will repeat itself to fill the remaining indexes
**/
public static String vigenereDecode(String ciphertext, String key){
String plaintext="";
// checks to see if ciphertext and key are both lowercase
// makes sure key is not an empty string
if(isLowerCase(ciphertext)&& isLowerCase(key)&&key!=""){
// loops through each character in the string using the caesarShiftDecode
// to encode each character with the corresponding key value character
for(int i=0;i<ciphertext.length();i++){
plaintext= plaintext +
caesarShiftDecode(ciphertext.charAt(i),key.charAt(i%key.length()));
}
return plaintext;
}
return ciphertext;
}
}