Crypt - String key = PIN; while (key.length() <...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.io.PrintStream; import java.util.Scanner; i public class Crypt { public static void main(String[] args) { final String ALPHABET = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"; final int LENGTH = ALPHABET.length(); final String PIN = "1956"; final String INVERSE = "[^A-Z]+"; final String SPACE = " "; PrintStream out = System.out; Scanner in = new Scanner(System.in); out.print("Enter Command: "); String buffer = in.nextLine(); out.println(buffer); String command = buffer.substring(0,1); String message = buffer.substring(2); message = message.toUpperCase(); message = message.replaceAll(INVERSE, SPACE);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: String key = PIN; while (key.length() < message.length()) { key = key + key; } String cipher = ""; for (int index = 0; index < message.length(); index++) { int position = ALPHABET.indexOf(message.charAt(index)); int shift = Integer.parseInt(String.valueOf(key.charAt(index))); final int REVERSE = (position - shift) % LENGTH; final String ENCRYPT = "[eE]+"; position = command.matches(ENCRYPT) ? (position + shift) % LENGTH : (REVERSE < 0) ? REVERSE + LENGTH : REVERSE; cipher += ALPHABET.charAt(position); } out.println(key); out.println(cipher); } }...
View Full Document

This note was uploaded on 09/16/2011 for the course CSE CSE 1020 taught by Professor Burton,ma during the Fall '10 term at York University.

Ask a homework question - tutors are online