public class JobaCaesarChiffre
{
private String inputString;
private StringBuilder encString;
private StringBuilder decString;
/** Konstruktor */
JobaCaesarChiffre( String flag, String instr )
{
this.inputString = instr;
if ( flag == "encode")
{
this.computeEncryptedString();
}
if ( flag == "decode")
{
this.computeDecryptedString();
}
}
/** Chiffre aus Klartext bilden */
private void computeEncryptedString()
{
// Ein Buchstabe muss im Alphabet drei Plätze nach vorn verschoben werden
// also Klartextzeichen | Chiffrezeichen
this.encString = new StringBuilder(this.inputString);
for ( int i = 0; i < this.inputString.length(); i++)
{
char currentLetter = this.inputString.charAt(i);
if (currentLetter == 'a') { currentLetter = 'd'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'b') { currentLetter = 'e'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'c') { currentLetter = 'f'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'd') { currentLetter = 'g'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'e') { currentLetter = 'h'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'f') { currentLetter = 'i'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'g') { currentLetter = 'j'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'h') { currentLetter = 'k'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'i') { currentLetter = 'l'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'j') { currentLetter = 'm'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'k') { currentLetter = 'n'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'l') { currentLetter = 'o'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'm') { currentLetter = 'p'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'n') { currentLetter = 'q'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'o') { currentLetter = 'r'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'p') { currentLetter = 's'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'q') { currentLetter = 't'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'r') { currentLetter = 'u'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 's') { currentLetter = 'v'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 't') { currentLetter = 'w'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'u') { currentLetter = 'x'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'v') { currentLetter = 'y'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'w') { currentLetter = 'z'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'x') { currentLetter = 'a'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'y') { currentLetter = 'b'; this.encString.setCharAt(i, currentLetter); }
if (currentLetter == 'z') { currentLetter = 'c'; this.encString.setCharAt(i, currentLetter); }
}
}
/** Klartext aus Chiffre bilden */
private void computeDecryptedString()
{
this.decString = new StringBuilder(this.inputString);
for ( int i = 0; i < this.inputString.length(); i++)
{
char currentLetter = this.inputString.charAt(i);
if (currentLetter == 'a') { currentLetter = 'x'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'b') { currentLetter = 'y'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'c') { currentLetter = 'z'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'd') { currentLetter = 'a'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'e') { currentLetter = 'b'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'f') { currentLetter = 'c'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'g') { currentLetter = 'd'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'h') { currentLetter = 'e'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'i') { currentLetter = 'f'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'j') { currentLetter = 'g'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'k') { currentLetter = 'h'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'l') { currentLetter = 'i'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'm') { currentLetter = 'j'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'n') { currentLetter = 'k'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'o') { currentLetter = 'l'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'p') { currentLetter = 'm'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'q') { currentLetter = 'n'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'r') { currentLetter = 'o'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 's') { currentLetter = 'p'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 't') { currentLetter = 'q'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'u') { currentLetter = 'r'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'v') { currentLetter = 's'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'w') { currentLetter = 't'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'x') { currentLetter = 'u'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'y') { currentLetter = 'v'; this.decString.setCharAt(i, currentLetter); }
if (currentLetter == 'z') { currentLetter = 'w'; this.decString.setCharAt(i, currentLetter); }
}
}
/** Verschlüsselten String ausliefern */
public StringBuilder getEncryptedString()
{
return this.encString;
}
/** Klartext String aus Chiffre liefern */
public StringBuilder getDecryptedString()
{
return this.decString;
}
}