-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenereCipher.java
More file actions
25 lines (24 loc) · 1 KB
/
VigenereCipher.java
File metadata and controls
25 lines (24 loc) · 1 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
import java.util.*;
public class VigenereCipher {
public static void main(String[] args) {
System.out.println("----Vigenere Cipher----");
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Plain Text: ");
String text = scan.next();
System.out.println("Enter the Key Text: ");
String key = scan.next();
key = key.repeat(text.length() / key.length()) + key.substring(0, (text.length() % key.length()));
System.out.println("KEY : "+key);
String encrypted = "";
for (int i = 0; i < text.length(); i++) {
encrypted += (char) ((key.charAt(i) + text.charAt(i)) % 26 + 65);
}
System.out.println("Encrypted Text : " + encrypted);
String Decrypted = "";
for (int i = 0; i < encrypted.length(); i++) {
Decrypted += (char) ((encrypted.charAt(i) - key.charAt(i) + 26) % 26 + 65);
}
System.out.println("Decrypted Text : " + Decrypted);
scan.close();
}
}