// Arup Guha
// 11/9/2025
// Example showing Vigenere Cipher

import java.util.*;

public class vigenere {

	public static String encrypt(String plain, String key) {
	
		// Build answer here.
		String res = "";
		
		// Go through the plaintext.
		for (int i=0; i<plain.length(); i++) {
		
			// A lot going on here. Use mod to get to the correct offset in key.
			// Also, need to both subtract and add 'A' when we're going back and forth between
			// Ascii values and 0 - 25 values for letters.
			char let = (char)((plain.charAt(i) - 'A' + key.charAt(i%key.length()) - 'A')%26 + 'A');
			
			// This is string concatenation. It's slow, but okay for this example.
			res = res + let;
		}
		
		return res;
	
	}
	
	// Decrypts the cipher text cipher using the key key for the Vigenere cipher.
	public static String decrypt(String cipher, String key) {
		
		// Store result here.
		String res = "";
		
		// Same as above but we subtract the appropriate key letter. Also, with mod, watch out,
		// if we go below 0, this doesn't work so we have to add a copy of 26.
		for (int i=0; i<cipher.length(); i++) {
			char let = (char)((cipher.charAt(i) - key.charAt(i%key.length()) + 26)%26 + 'A');
			res = res + let;
		}
		
		return res;
	
	}

	// Basic test of both encryption and decryption.
	public static void main(String[] args) {
		System.out.println(encrypt("SENDMORE", "ACM"));
		System.out.println(decrypt("SGZDOARG", "ACM"));
	}
}