Last active
November 21, 2018 23:28
-
-
Save corusm/a47a97fea31548cf1b84e8e19bbc181c to your computer and use it in GitHub Desktop.
Method for Encryption and Decryption with the Viginere Cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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