crypto_1
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
crypto_1 [2025/05/27 10:45] – frchris | crypto_1 [2025/05/27 11:40] (current) – frchris | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Crypto 1 (Vegnere) ====== | + | ====== Crypto 1 (Vigenère) ====== |
[[https:// | [[https:// | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <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 | ||
+ | |||
+ | /** | ||
+ | * Constructor for objects of class Crypto | ||
+ | */ | ||
+ | public Crypto() | ||
+ | { | ||
+ | key = " | ||
+ | } | ||
+ | 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(); | ||
+ | { | ||
+ | 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(); | ||
+ | { | ||
+ | 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(" | ||
+ | | ||
+ | Scanner kb = new Scanner(System.in); | ||
+ | System.out.print(" | ||
+ | String line = kb.nextLine(); | ||
+ | Crypto code = new Crypto(line); | ||
+ | | ||
+ | while (! line.equals(" | ||
+ | System.out.print(" | ||
+ | line = kb.nextLine().toUpperCase(); | ||
+ | char cmd = line.charAt(0); | ||
+ | switch ( cmd ) { | ||
+ | case ' | ||
+ | System.out.print(" | ||
+ | String newKey = kb.nextLine(); | ||
+ | code.setKey(newKey); | ||
+ | break; | ||
+ | case ' | ||
+ | System.out.print(" | ||
+ | String message = kb.nextLine(); | ||
+ | System.out.println(code.encode(message)); | ||
+ | break; | ||
+ | case ' | ||
+ | System.out.print(" | ||
+ | message = kb.nextLine(); | ||
+ | System.out.println(code.decode(message)); | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | [[https:// | ||
+ | |||
+ | |||
+ | [[https:// | ||
Back to [[New Labs]] | Back to [[New Labs]] |
crypto_1.1748357133.txt.gz · Last modified: 2025/05/27 10:45 by frchris