-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenereCipher.java
More file actions
82 lines (65 loc) · 1.94 KB
/
VigenereCipher.java
File metadata and controls
82 lines (65 loc) · 1.94 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
public class VigenereCipher{
static String generateKey(String str, String key)
{
int x = str.length();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.length() == str.length())
break;
key+=(key.charAt(i));
}
return key;
}
static String cipherText(String str, String key)
{
String cipher_text="";
for (int i = 0; i < str.length(); i++)
{
int x = (str.charAt(i) + key.charAt(i)) %26;
x += 'A';
cipher_text+=(char)(x);
}
return cipher_text;
}
static String originalText(String cipher_text, String key)
{
String orig_text="";
for (int i = 0 ; i < cipher_text.length() &&
i < key.length(); i++)
{
int x = (cipher_text.charAt(i) -
key.charAt(i) + 26) %26;
x += 'A';
orig_text+=(char)(x);
}
return orig_text;
}
static String LowerToUpper(String s)
{
StringBuffer str =new StringBuffer(s);
for(int i = 0; i < s.length(); i++)
{
if(Character.isLowerCase(s.charAt(i)))
{
str.setCharAt(i, Character.toUpperCase(s.charAt(i)));
}
}
s = str.toString();
return s;
}
public static void main(String[] args)
{
String Str = "PROJECTX";
String Keyword = "BLAST";
String str = LowerToUpper(Str);
String keyword = LowerToUpper(Keyword);
String key = generateKey(str, keyword);
String cipher_text = cipherText(str, key);
System.out.println("Ciphertext : "
+ cipher_text + "\n");
System.out.println("Original/Decrypted Text : "
+ originalText(cipher_text, key));
}
}