====== Crypto 1 (Vigenère) ====== [[https://www.mathorama.com/gsp/Crypto%20WS.pdf|Worksheet]] With Riddles [[https://en.wikipedia.org/wiki/Vigenère_cipher|History of this Cipher Method]] import java.util.Scanner; /** * Write a description of class Crypto here. * * @author Chris Thiel, OFMCap * @version 23 May 2025 */ public class Crypto { private String key; private final String ALPHA="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /** * Constructor for objects of class Crypto */ public Crypto() { key = "UNIKNOWN"; } public void setKey(String key){ this.key = key.toUpperCase(); } public Crypto(String key) { setKey ( key ); } public String getKey(){ return key; } public String encode(String message) { String result =""; for (int i = 0; i< message.length(); i++) { char k = key.charAt( i % key.length() ); char m = message.toUpperCase().charAt(i); int kIndex = ALPHA.indexOf(k); int mIndex = ALPHA.indexOf(m); result += ALPHA.charAt( (kIndex + mIndex) % 26); } return result; } public String decode(String message) { String result =""; for (int i = 0; i< message.length(); i++) { char k = key.charAt( i % key.length() ); char m = message.toUpperCase().charAt(i); int kIndex = ALPHA.indexOf(k); int mIndex = ALPHA.indexOf(m); result += ALPHA.charAt( (26+mIndex-kIndex) % 26); } return result; } public static void main(String[] args) { System.out.println("Vigenere Cyptography ver 1.0 "); Scanner kb = new Scanner(System.in); System.out.print("What is the key? "); String line = kb.nextLine(); Crypto code = new Crypto(line); while (! line.equals("Q") ){ System.out.print("Key is "+ code.key +" E)ncode, D)ecode, C)hange Key, Q)uit Commend: "); line = kb.nextLine().toUpperCase(); char cmd = line.charAt(0); switch ( cmd ) { case 'C' : System.out.print("What is the key? "); String newKey = kb.nextLine(); code.setKey(newKey); break; case 'E' : System.out.print("Encode what message?"); String message = kb.nextLine(); System.out.println(code.encode(message)); break; case 'D' : System.out.print("Decode what message?"); message = kb.nextLine(); System.out.println(code.decode(message)); break; } } System.out.println("Bye!"); } } [[https://www.mathorama.com/ti/CRYPTO.png|My TI-84 Basic Listing]] [[https://www.mathorama.com/ti/CRYPTO.8xp|CRYPTO.8xp]] (to send to your TI-84) Back to [[New Labs]]