User Tools

Site Tools


crypto_1

Crypto 1 (Vigenère)

Worksheet With Riddles

History of this Cipher Method

Crypto.java
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!");
    }
}

My TI-84 Basic Listing

CRYPTO.8xp (to send to your TI-84)

Back to New Labs

crypto_1.txt · Last modified: 2025/05/27 11:40 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki