function:RenderSidePart pageleftbodycaption pageleftbody sidenote Main.CryptographyLab-SideNote Main.SideNote Site.SideNote

# How does it work?

public static String encode (String m, String k){
String result="";
for (int i=0; i<m.length(); i++){
int offset=nextChar(k, i);
String letter = m.substring(i,i+1);
int newLetterIndex= alphabet.indexOf(letter)+offset;
newLetterIndex=newLetterIndex % 26;
result+=alphabet.substring( newLetterIndex, newLetterIndex+1);
}
return result;
}


A couple of things are going on... we need to convert any letter into a number and vice-versa there is a string called alphabet: private static final String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

# Getting a number from a letter

If we use the String method indexOf, we can make the connection between the letter A with 0, and B with 1, C with 2, ...

int number = alphabet.indexOf("D");


above, the int number would contain a 3.

# Changing the number from the letter to encode the letter

The next trick is the offset. For a simple Caesar cypher it is always the same offset. For example, in a Rot-13 cypher, we add 13 to the letter's number, so the letter "D" (index 3) would be converted to the letter "Q" (index 16) since 3+13=16;

To do this in java we use the String method substring:

String code=alphabet.substring( number+13, number+13+1);


the String instance code would now contain the letter "Q"

# Making the change more difficult to decypher

But we don't want to use the same offset every time. It would we easy to break that cypher! Instead the offset keeps changing, based on our key word. That is what the nextChar method does. It returns the offset we should use, based on the letter's index in our key word k:

 private static int nextChar(String k, int i){
i=i%k.length();
String letter=k.substring(i,i+1);
return alphabet.indexOf(letter);
}



# VignereCipherStartingCode

import java.util.Scanner;
/**
* Simple Polyalphabetic Cryptography
*
* @Chris Thiel
* @28 Feb 2009
*/
public class VignereCipherStartingCode
{
private static final String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static int nextChar(String k, int i){
i=i%k.length();
String letter=k.substring(i,i+1);
return alphabet.indexOf(letter);
}
public static String rot13(String s)
{
String in=s.toUpperCase();
String out="";

return out;

}

public static String encode (String m, String k){
String result="";
for (int i=0; i<m.length(); i++){
int offset=nextChar(k, i);
String letter = m.substring(i,i+1);
int newLetterIndex= alphabet.indexOf(letter)+offset;
newLetterIndex=newLetterIndex % 26;
result+=alphabet.substring( newLetterIndex, newLetterIndex+1);
}
return result;
}
public static String decode (String m, String k){
String result="";
return result;
}
public static void main(String[] args)
{
Scanner kb=new Scanner(System.in);
System.out.println("Rot13 Method:");
System.out.print("Type a message: ");
String r13=kb.nextLine();
r13=r13.toUpperCase();
String r13encoded=rot13(r13);
System.out.println("Rot 13 encoded is  "+r13encoded);
System.out.println("Decoded is  "+rot13(r13encoded));

System.out.println("Vignere Method:");
System.out.print("Type a key: ");
String key=kb.nextLine();
key=key.toUpperCase();
key=key.replace(" ","");//omit spaces
System.out.print("Type a message: ");
String message=kb.nextLine();
message=message.toUpperCase();
message=message.replace(" ","");
System.out.println("Using the key \""+key+"\" on \""+message+"\":");
String codedMessage=encode(message,key);
System.out.println("Coded="+codedMessage);
System.out.println("Decoded="+decode(codedMessage,key));
System.out.println("Using the key 'LOVE' we encode 'BOOHOO' we should get 'MCJLZC' : "+encode("BOOHOO","LOVE"));
System.out.println("Using the key 'QUICK' we decode 'TOKMXEQ' we should get 'DUCKNOW': "+decode("TOKMXEQ","QUICK"));

}
}


Complete the decode method so it can decipher the Vignere Cipher