package vigenere; import java.util.Scanner; public class VigenereCipher { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please enter the key (ONLY CAPITAL LETTERS WITHOUT SPACE)"); String key = input.nextLine(); System.out.println("Please enter the text"); String plainText = input.nextLine(); String e = encryption(plainText, key); System.out.println("encryption text is:"); System.out.println(e); System.out.println("decryption text is:"); System.out.println(decryption(e, key)); } static String encryption(String plainText, String key) { String empty = ""; plainText = plainText.toUpperCase(); for (int i = 0, j = 0; i < plainText.length(); i++) { char x = plainText.charAt(i); if (x < 'A' || x > 'Z') {}; empty += (char)((x + key.charAt(j) - 2 * 'A') % 26 + 'A'); j = ++j % key.length(); } return empty; } static String decryption(String plainText, String key) { String empty = ""; plainText = plainText.toUpperCase(); for (int i = 0, j = 0; i < plainText.length(); i++) { char x = plainText.charAt(i); if (x < 'A' || x > 'Z') {}; empty += (char)((x - key.charAt(j) + 26) % 26 + 'A'); j = ++j % key.length(); } return empty; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)