-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVernumCipherCode
More file actions
69 lines (61 loc) · 2.49 KB
/
VernumCipherCode
File metadata and controls
69 lines (61 loc) · 2.49 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
package monoalphabeticcipher;
/**
*
* @author Md Ali Shaikh
*/
public class VernamCipher {
public static String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public String getCipheredText(String plainText, String oneTimePad){
plainText = plainText.toLowerCase();
oneTimePad = oneTimePad.toLowerCase();
String cipheredText = "";
boolean doExit = false;
if(plainText.length() == oneTimePad.length()){
String[] pTArr = plainText.split(" ");
String[] oTPArr = oneTimePad.split(" ");
if(pTArr.length == oTPArr.length){
for (int i = 0; i < pTArr.length; i++) {
if(pTArr[i].length() == oTPArr[i].length()){
String plainTextWord = pTArr[i], oneTimePadWord = oTPArr[i];
int len = plainTextWord.length();
for (int j = 0; j < len; j++) {
String ptChar = String.valueOf(plainTextWord.charAt(j));
String otpChar = String.valueOf(oneTimePadWord.charAt(j));
int ptPos = ALPHABET.indexOf(ptChar);
int otpPos = ALPHABET.indexOf(otpChar);
int initial = ptPos + otpPos;
if(initial>25){
initial = initial - 26;
}
cipheredText += ALPHABET.charAt(initial);
}
cipheredText += " ";
}else{
doExit = true;
break;
}
}
}
else{
doExit = true;
}
}else{
doExit = true;
}
if (doExit) {
System.out.println("The sequence for the plain text and OTP is not equal..");
System.exit(0);
}
return cipheredText;
}
public static void main(String[] args) {
VernamCipher vc = new VernamCipher();
String plainText = "i am fine thanku";
String oneTimePad = "x mp ypgn hnlpqr";
//System.out.println(plainText + " + " + oneTimePad );
System.out.println("plainText: "+plainText);
System.out.println("oneTimePad: "+oneTimePad);
String cipheredText = vc.getCipheredText(plainText, oneTimePad);
System.out.println("cipheredText: "+cipheredText);
}
}