BAEL-7496: Vigenère Cipher in Java (#16072)

This commit is contained in:
Graham Cox 2024-03-09 10:18:55 +00:00 committed by GitHub
parent 8ca61a8c38
commit e1a2eed0d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 132 additions and 0 deletions

View File

@ -0,0 +1,69 @@
package com.baeldung.algorithms.vigenere;
public class VigenereCipher {
private final String characters;
public VigenereCipher() {
this("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
public VigenereCipher(String characters) {
this.characters = characters;
}
public String encode(String input, String key) {
String result = "";
int keyPosition = 0;
for (char c : input.toCharArray()) {
char k = key.charAt(keyPosition % key.length());
int charIndex = characters.indexOf(c);
int keyIndex = characters.indexOf(k);
if (charIndex >= 0) {
if (keyIndex >= 0) {
int newCharIndex = (charIndex + keyIndex + 1) % characters.length();
c = characters.charAt(newCharIndex);
}
keyPosition++;
}
result += c;
}
return result;
}
public String decode(String input, String key) {
String result = "";
int keyPosition = 0;
for (char c : input.toCharArray()) {
char k = key.charAt(keyPosition % key.length());
int charIndex = characters.indexOf(c);
int keyIndex = characters.indexOf(k);
if (charIndex >= 0) {
if (keyIndex >= 0) {
int newCharIndex = charIndex - keyIndex - 1;
if (newCharIndex < 0) {
newCharIndex = characters.length() + newCharIndex;
}
c = characters.charAt(newCharIndex);
}
keyPosition++;
}
result += c;
}
return result;
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.algorithms.vigenere;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class VigenereCipherUnitTest {
@Test
void encodeBaeldung() {
VigenereCipher cipher = new VigenereCipher();
String output = cipher.encode("BAELDUNG", "HELLO");
Assertions.assertEquals("JFQXSCSS", output);
}
@Test
void encodeBaeldungMixedCharacters() {
VigenereCipher cipher = new VigenereCipher("JQFVHPWORZSLNMKYCGBUXIEDTA");
String output = cipher.encode("BAELDUNG", "HELLO");
Assertions.assertEquals("DERDPTZV", output);
}
@Test
void encodeArticleTitle() {
VigenereCipher cipher = new VigenereCipher();
String output = cipher.encode("VEGENERE CIPHER IN JAVA", "BAELDUNG");
Assertions.assertEquals("XFLQRZFL EJUTIM WU LBAM", output);
}
@Test
void encodeArticleTitleMoreCharacters() {
VigenereCipher cipher = new VigenereCipher("ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
String output = cipher.encode("VEGENERE CIPHER IN JAVA", "BAELDUNG");
Assertions.assertEquals("XFLQRZELBDNALZEGKOEVEPO", output);
}
@Test
void decodeBaeldung() {
VigenereCipher cipher = new VigenereCipher();
String output = cipher.decode("JFQXSCSS", "HELLO");
Assertions.assertEquals("BAELDUNG", output);
}
@Test
void decodeBaeldungMixedCharacters() {
VigenereCipher cipher = new VigenereCipher("JQFVHPWORZSLNMKYCGBUXIEDTA");
String output = cipher.decode("DERDPTZV", "HELLO");
Assertions.assertEquals("BAELDUNG", output);
}
@Test
void decodeArticleTitleMoreCharacters() {
VigenereCipher cipher = new VigenereCipher("ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
String output = cipher.decode("XFLQRZELBDNALZEGKOEVEPO", "BAELDUNG");
Assertions.assertEquals("VEGENERE CIPHER IN JAVA", output);
}
}