Difference between revisions of "CIS 3020 Project 5"
Jump to navigation
Jump to search
(→Code) |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
==Code== | ==Code== | ||
===Cipher.java=== | ===Cipher.java=== | ||
+ | <pre> | ||
import java.lang.String; | import java.lang.String; | ||
Line 66: | Line 67: | ||
} | } | ||
} | } | ||
+ | </pre> | ||
+ | ===CipherDemo.java=== | ||
+ | <pre> | ||
+ | public class CipherDemo { | ||
+ | |||
+ | public static void main(String[] args){ | ||
+ | |||
+ | //first message to be encoded | ||
+ | String message = "'whats it going to be then, eh? there was me, that is alex | ||
+ | , and my three droogs, that is pete, georgie, and dim, dim being really dim, and | ||
+ | we sat in the korova milkbar making up our rassoodocks what to do with the even | ||
+ | ing, a flip dark chill winter bastard though dry."; | ||
+ | //copy of message sent to uppercase | ||
+ | final String MESSAGE = message.toUpperCase(); | ||
+ | |||
+ | //second message to be encoded | ||
+ | String message2 = "Gavin dopedrifted through the sensemadness of karnival li | ||
+ | ke a molecule enslaved in one of the savages amplifiers, vibrating with the chor | ||
+ | ds of the bass guitar, beaten from side to side by the hammering drum, darting w | ||
+ | ith the stringplay of the lead, in unrelenting, irrelevant motion.... throb, boo | ||
+ | m-boom, tinkle, twinkle, plink..."; | ||
+ | //copy of message sent to uppercase | ||
+ | final String MESSAGE2 = message2.toUpperCase(); | ||
+ | |||
+ | |||
+ | //original letters | ||
+ | final String ENCODE = "1234567890 .,()?!-:;ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
+ | //encoded letters | ||
+ | final String KEY = "Z G1HODIWQX50CSYT,(:6M-J8)?U2;7EAF!VK3L4NBP9R."; | ||
+ | |||
+ | //new instances of cipher | ||
+ | Cipher encoder = new Cipher(); | ||
+ | |||
+ | //original message | ||
+ | System.out.println("Original Message: " + MESSAGE); | ||
+ | //encode | ||
+ | encoder.encipher(ENCODE, KEY, MESSAGE); | ||
+ | //print output | ||
+ | System.out.println("Encoded Message: " + encoder.getOutput()); | ||
+ | //decode! (note interchanged KEY, ENCODE) | ||
+ | encoder.encipher(KEY, ENCODE, encoder.getOutput()); | ||
+ | //print output | ||
+ | System.out.println("Decoding of Message: " + encoder.getOutput()); | ||
+ | |||
+ | //return carriage | ||
+ | System.out.println(""); | ||
+ | |||
+ | //original message | ||
+ | System.out.println("Original Message: " + MESSAGE2); | ||
+ | //show that encipher returns a string | ||
+ | System.out.println("Encoded Message: " + encoder.encipher(ENCODE, KEY, MESSA | ||
+ | GE2)); | ||
+ | //decode returning string | ||
+ | System.out.println("Decoding of Message: " + encoder.encipher(KEY, ENCODE, e | ||
+ | ncoder.getOutput())); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </pre> |
Latest revision as of 12:51, 5 November 2007
Code
Cipher.java
import java.lang.String; public class Cipher { //output string that holds encrypted message private String output = new String(); //replacement letter for encryption (changes often!) private String replacement = new String(); //how to know when to stop trying to replace letters private int keyLen = 0; private void clearOutput(){ //clear this.output this.output = ""; } public String getOutput(){ //return output return(this.output); } public String encipher (String encode, String key, String message){ //reset this.output clearOutput(); //determine key length this.keyLen = (key.length()-1); //return the encoded message return(encode(encode, key, message)); } private String encode(String encode, String key, String message) { //is there anything left to encode? if (message.length() != 0) { // Get the first character in the initiator string. //the following two lines (3 lines if you really want to, //but letterReplacement would have to return this.replacement) //could have been combined but weren't for clarity. String letter = message.substring(0, 1); //replace letter with encrypted letter letterReplacement(letter, encode, key, 0); //concatenate output with encoded letter this.output = this.output.concat(this.replacement); //repeat for next letter encode(encode, key, message.substring(1)); } //end if //end big if (length != 0) return (this.output); }//end next generation private void letterReplacement(String letter, String encode, String key, int n){ //don't go out of bounds! if (n<=this.keyLen){ //try to replace letter with encoded character this.replacement = letter.replace(encode.charAt(n),key.charAt(n)); //did replacement succeed? if (this.replacement.equals(letter)){ //if not, try again letterReplacement(letter, encode, key, n+1); } } } }
CipherDemo.java
public class CipherDemo { public static void main(String[] args){ //first message to be encoded String message = "'whats it going to be then, eh? there was me, that is alex , and my three droogs, that is pete, georgie, and dim, dim being really dim, and we sat in the korova milkbar making up our rassoodocks what to do with the even ing, a flip dark chill winter bastard though dry."; //copy of message sent to uppercase final String MESSAGE = message.toUpperCase(); //second message to be encoded String message2 = "Gavin dopedrifted through the sensemadness of karnival li ke a molecule enslaved in one of the savages amplifiers, vibrating with the chor ds of the bass guitar, beaten from side to side by the hammering drum, darting w ith the stringplay of the lead, in unrelenting, irrelevant motion.... throb, boo m-boom, tinkle, twinkle, plink..."; //copy of message sent to uppercase final String MESSAGE2 = message2.toUpperCase(); //original letters final String ENCODE = "1234567890 .,()?!-:;ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //encoded letters final String KEY = "Z G1HODIWQX50CSYT,(:6M-J8)?U2;7EAF!VK3L4NBP9R."; //new instances of cipher Cipher encoder = new Cipher(); //original message System.out.println("Original Message: " + MESSAGE); //encode encoder.encipher(ENCODE, KEY, MESSAGE); //print output System.out.println("Encoded Message: " + encoder.getOutput()); //decode! (note interchanged KEY, ENCODE) encoder.encipher(KEY, ENCODE, encoder.getOutput()); //print output System.out.println("Decoding of Message: " + encoder.getOutput()); //return carriage System.out.println(""); //original message System.out.println("Original Message: " + MESSAGE2); //show that encipher returns a string System.out.println("Encoded Message: " + encoder.encipher(ENCODE, KEY, MESSA GE2)); //decode returning string System.out.println("Decoding of Message: " + encoder.encipher(KEY, ENCODE, e ncoder.getOutput())); } }