Skip to content

Instantly share code, notes, and snippets.

@codelance
Created December 2, 2012 01:29
Show Gist options
  • Select an option

  • Save codelance/4186425 to your computer and use it in GitHub Desktop.

Select an option

Save codelance/4186425 to your computer and use it in GitHub Desktop.
Ceasar Cipher Break
public static void main(String[] args) {
String ciphertext = "HSPAASLJPWOLYALEAJVBSKILHWYVISLTIBAUVAPMPAPZHZOPMAJPWOLY";
for(int shiftKey = 0; shiftKey < 26; shiftKey++)
{
String plainText = "";
for (int i=0; i<ciphertext.length(); i++)
{
int asciiValue = (int) ciphertext.charAt(i);
int alphabetIndex = asciiValue - 65;
int newshiftedAsciiValue = -1000;
if (alphabetIndex - shiftKey < 0){
newshiftedAsciiValue = 90 - (shiftKey - alphabetIndex) + 1;
}
else{
newshiftedAsciiValue = 65 + (alphabetIndex - shiftKey);
}
plainText += (char) newshiftedAsciiValue;
}
System.out.println(plainText);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment