Skip to content

Instantly share code, notes, and snippets.

@corusm
Last active November 21, 2018 23:28
Show Gist options
  • Save corusm/a47a97fea31548cf1b84e8e19bbc181c to your computer and use it in GitHub Desktop.
Save corusm/a47a97fea31548cf1b84e8e19bbc181c to your computer and use it in GitHub Desktop.
Method for Encryption and Decryption with the Viginere Cipher
public class vigi {
public static String vigenere(String text, String key, int mode) {
text = text.toUpperCase();
text = text.replaceAll(" ", "");
key = key.toUpperCase();
String returnText = "";
for (int i = 0, j = 0; i < text.length(); i++, j++) {
//i: Zähler für text; j: Zähler für key
if (j == key.length()) {
j = 0;
//Wenn Schlüsselwort durchlaufen, dann wieder von vorne
}
if (mode == 1){
returnText += (char) ((text.charAt(i) + key.charAt(j) - 130) % 26 + 65);
//Verschlüsselung
}
if (mode == 0) {
returnText += (char) ((text.charAt(i) - key.charAt(j) + 130) % 26 + 65);
//Entschlüsselung
}
}
return returnText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment