BAEL-7496: Vigenère Cipher in Java (#16072)
This commit is contained in:
parent
8ca61a8c38
commit
e1a2eed0d9
algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere
69
algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipher.java
Normal file
69
algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipher.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
63
algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipherUnitTest.java
Normal file
63
algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipherUnitTest.java
Normal 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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user