User Tools

Site Tools


crypto_1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
crypto_1 [2025/05/27 10:45] frchriscrypto_1 [2025/05/27 11:40] (current) frchris
Line 1: Line 1:
-====== Crypto 1 (Vegnere) ======+====== Crypto 1 (Vigenère) ======
  
 [[https://www.mathorama.com/gsp/Crypto%20WS.pdf|Worksheet]] With Riddles [[https://www.mathorama.com/gsp/Crypto%20WS.pdf|Worksheet]] With Riddles
 +
 +[[https://en.wikipedia.org/wiki/Vigenère_cipher|History of this Cipher Method]]
 +
 +<code java 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!");
 +    }
 +}
 +
 +</code>
 +
 +
 +[[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]] Back to [[New Labs]]
crypto_1.1748357133.txt.gz · Last modified: 2025/05/27 10:45 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki