Updates for BAEL-7496 (#16150)

* Moved vigenere cipher code to a new module

* New test case for encoding and decoding together
This commit is contained in:
Graham Cox
2024-03-21 05:34:48 +00:00
committed by GitHub
parent 1d289c30b9
commit 30a5cddce0
4 changed files with 37 additions and 7 deletions
@@ -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;
}
}
@@ -0,0 +1,76 @@
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("VIGENERE CIPHER IN JAVA", "BAELDUNG");
Assertions.assertEquals("XJLQRZFL EJUTIM WU LBAM", output);
}
@Test
void encodeArticleTitleMoreCharacters() {
VigenereCipher cipher = new VigenereCipher("ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
String output = cipher.encode("VIGENERE CIPHER IN JAVA", "BAELDUNG");
Assertions.assertEquals("XJLQRZELBDNALZEGKOEVEPO", 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("XJLQRZELBDNALZEGKOEVEPO", "BAELDUNG");
Assertions.assertEquals("VIGENERE CIPHER IN JAVA", output);
}
@Test
void encodeDecodeBaeldung() {
VigenereCipher cipher = new VigenereCipher();
String input = "BAELDUNG";
String key = "HELLO";
String encoded = cipher.encode(input, key);
String decoded = cipher.decode(encoded, key);
Assertions.assertEquals(input, decoded);
}
}