This commit is contained in:
parent
e26fd9da71
commit
7ece806cf5
@ -48,7 +48,6 @@
|
|||||||
* [ProductCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ProductCipher.java)
|
* [ProductCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ProductCipher.java)
|
||||||
* [RSA](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/RSA.java)
|
* [RSA](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/RSA.java)
|
||||||
* [SimpleSubCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java)
|
* [SimpleSubCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java)
|
||||||
* [SimpleSubstitutionCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/SimpleSubstitutionCipher.java)
|
|
||||||
* [Vigenere](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Vigenere.java)
|
* [Vigenere](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Vigenere.java)
|
||||||
* conversions
|
* conversions
|
||||||
* [AnyBaseToAnyBase](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java)
|
* [AnyBaseToAnyBase](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java)
|
||||||
@ -596,7 +595,6 @@
|
|||||||
* [PolybiusTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PolybiusTest.java)
|
* [PolybiusTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PolybiusTest.java)
|
||||||
* [RSATest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/RSATest.java)
|
* [RSATest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/RSATest.java)
|
||||||
* [SimpleSubCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java)
|
* [SimpleSubCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java)
|
||||||
* [SimpleSubstitutionCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/SimpleSubstitutionCipherTest.java)
|
|
||||||
* [VigenereTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/VigenereTest.java)
|
* [VigenereTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/VigenereTest.java)
|
||||||
* conversions
|
* conversions
|
||||||
* [BinaryToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java)
|
* [BinaryToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/BinaryToDecimalTest.java)
|
||||||
|
@ -1,83 +0,0 @@
|
|||||||
package com.thealgorithms.ciphers;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The simple substitution cipher is a cipher that has been in use for many
|
|
||||||
* hundreds of years (an excellent history is given in Simon Singhs 'the Code
|
|
||||||
* Book'). It basically consists of substituting every plaintext character for a
|
|
||||||
* different ciphertext character. It differs from the Caesar cipher in that the
|
|
||||||
* cipher alphabet is not simply the alphabet shifted, it is completely jumbled.
|
|
||||||
*
|
|
||||||
* @author Hassan Elseoudy
|
|
||||||
*/
|
|
||||||
public class SimpleSubstitutionCipher {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Encrypt text by replacing each element with its opposite character.
|
|
||||||
*
|
|
||||||
* @return Encrypted message
|
|
||||||
*/
|
|
||||||
public static String encode(String message, String cipherSmall) {
|
|
||||||
StringBuilder encoded = new StringBuilder();
|
|
||||||
|
|
||||||
// This map is used to encode
|
|
||||||
Map<Character, Character> cipherMap = new HashMap<>();
|
|
||||||
|
|
||||||
char beginSmallLetter = 'a';
|
|
||||||
char beginCapitalLetter = 'A';
|
|
||||||
|
|
||||||
cipherSmall = cipherSmall.toLowerCase();
|
|
||||||
String cipherCapital = cipherSmall.toUpperCase();
|
|
||||||
|
|
||||||
// To handle Small and Capital letters
|
|
||||||
for (int i = 0; i < cipherSmall.length(); i++) {
|
|
||||||
cipherMap.put(beginSmallLetter++, cipherSmall.charAt(i));
|
|
||||||
cipherMap.put(beginCapitalLetter++, cipherCapital.charAt(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < message.length(); i++) {
|
|
||||||
if (Character.isAlphabetic(message.charAt(i))) {
|
|
||||||
encoded.append(cipherMap.get(message.charAt(i)));
|
|
||||||
} else {
|
|
||||||
encoded.append(message.charAt(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return encoded.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decrypt message by replacing each element with its opposite character in
|
|
||||||
* cipher.
|
|
||||||
*
|
|
||||||
* @return message
|
|
||||||
*/
|
|
||||||
public static String decode(String encryptedMessage, String cipherSmall) {
|
|
||||||
StringBuilder decoded = new StringBuilder();
|
|
||||||
|
|
||||||
Map<Character, Character> cipherMap = new HashMap<>();
|
|
||||||
|
|
||||||
char beginSmallLetter = 'a';
|
|
||||||
char beginCapitalLetter = 'A';
|
|
||||||
|
|
||||||
cipherSmall = cipherSmall.toLowerCase();
|
|
||||||
String cipherCapital = cipherSmall.toUpperCase();
|
|
||||||
|
|
||||||
for (int i = 0; i < cipherSmall.length(); i++) {
|
|
||||||
cipherMap.put(cipherSmall.charAt(i), beginSmallLetter++);
|
|
||||||
cipherMap.put(cipherCapital.charAt(i), beginCapitalLetter++);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < encryptedMessage.length(); i++) {
|
|
||||||
if (Character.isAlphabetic(encryptedMessage.charAt(i))) {
|
|
||||||
decoded.append(cipherMap.get(encryptedMessage.charAt(i)));
|
|
||||||
} else {
|
|
||||||
decoded.append(encryptedMessage.charAt(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return decoded.toString();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
package com.thealgorithms.ciphers;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
public class SimpleSubstitutionCipherTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testEncode() {
|
|
||||||
// Given
|
|
||||||
String message = "HELLOWORLD";
|
|
||||||
String key = "phqgiumeaylnofdxjkrcvstzwb";
|
|
||||||
|
|
||||||
// When
|
|
||||||
String actual = SimpleSubstitutionCipher.encode(message, key);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
assertEquals("EINNDTDKNG", actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testDecode() {
|
|
||||||
// Given
|
|
||||||
String message = "EINNDTDKNG";
|
|
||||||
String key = "phqgiumeaylnofdxjkrcvstzwb";
|
|
||||||
|
|
||||||
// When
|
|
||||||
String actual = SimpleSubstitutionCipher.decode(message, key);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
assertEquals("HELLOWORLD", actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testIsTextTheSameAfterEncodeAndDecode() {
|
|
||||||
// Given
|
|
||||||
String text = "HELLOWORLD";
|
|
||||||
String key = "phqgiumeaylnofdxjkrcvstzwb";
|
|
||||||
|
|
||||||
// When
|
|
||||||
String encodedText = SimpleSubstitutionCipher.encode(text, key);
|
|
||||||
String decodedText = SimpleSubstitutionCipher.decode(encodedText, key);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
assertEquals(text, decodedText);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user