From e1a2eed0d91dc015110e0fe8709f886399df9f9b Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Sat, 9 Mar 2024 10:18:55 +0000 Subject: [PATCH] =?UTF-8?q?BAEL-7496:=20Vigen=C3=A8re=20Cipher=20in=20Java?= =?UTF-8?q?=20(#16072)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../algorithms/vigenere/VigenereCipher.java | 69 +++++++++++++++++++ .../vigenere/VigenereCipherUnitTest.java | 63 +++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipher.java create mode 100644 algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipherUnitTest.java diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipher.java b/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipher.java new file mode 100644 index 0000000000..37a7c13d17 --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipher.java @@ -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; + } + +} diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipherUnitTest.java b/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipherUnitTest.java new file mode 100644 index 0000000000..0e61e9c42e --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/vigenere/VigenereCipherUnitTest.java @@ -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); + } +}