Monday, October 7, 2013

Mono-alphabetic in Java -- by Babak Hoseini

Today I have another special guest post.  Babak Hoseini has been happy to write about encryption.  He has written a cool little source code that will do some encryption for you.  Thank you Babak for writing a guest post for me.  As always, I appreciate your input.  --Shane Killen

Mono-alphabetic in Java   -- by Babak Hoseini
A mono-alphabetic cipher is a simple substitution cipher wherein each letter of the plaintext is replaced by another letter in the ciphertext. An example of a mono-alphabetic cipher key follows:

Plain  : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Cipher : z y x w v u t s r q p o n m l k j i h g f e d c b a

This key means that any 'M' in the plaintext will be replaced by a 'n' in the ciphertext, any 'P' in the plaintext will be replaced by a 'k' in the ciphertext, and so on.

For instance, plaintext is  : “meet me at the party” and after encrypt you get “nvvg1nv1zg1gsv1kzigb
I wrote the program below in Java that will encrypt plaintext to ciphertext:

package encryption;

public class Encryption {

public static void main(String[] args) {
// TODO Auto-generated method stub
String plaintext = "meet me at the party";
char[] myarr = plaintext.toCharArray();
for(int count=0 ; count < myarr.length ; count++){
char cipher = myarr[count];
switch (cipher) {
case 'a':
System.out.print("z");
break;
case 'b':
System.out.print("y");
break;
case 'c':
System.out.print("x");
break;
case 'd':
System.out.print("w");
break;
case 'e':
System.out.print("v");
break;
case 'f':
System.out.print("u");
break;
case 'g':
System.out.print("t");
break;
case 'h':
System.out.print("s");
break;
case 'i':
System.out.print("r");
break;
case 'j':
System.out.print("q");
break;
case 'k':
System.out.print("p");
break;
case 'l':
System.out.print("o");
break;
case 'm':
System.out.print("n");
break;
case 'n':
System.out.print("m");
break;
case 'o':
System.out.print("l");
break;
case 'p':
System.out.print("k");
break;
case 'q':
System.out.print("j");
break;
case 'r':
System.out.print("i");
break;
case 's':
System.out.print("h");
break;
case 't':
System.out.print("g");
break;
case 'u':
System.out.print("f");
break;
case 'v':
System.out.print("e");
break;
case 'w':
System.out.print("d");
break;
case 'x':
System.out.print("c");
break;
case 'y':
System.out.print("b");
break;
case 'z':
System.out.print("a");
break;
case ' ':
System.out.print("1");
break;
case '1':
System.out.print(" ");
}
}
}

}


No comments:

Post a Comment

Your comment will be reviewed for approval. Thank you for submitting your comments.