-
Notifications
You must be signed in to change notification settings - Fork 19.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into bandwidth_new_algo
- Loading branch information
Showing
165 changed files
with
6,531 additions
and
1,247 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/com/thealgorithms/ciphers/BaconianCipher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.thealgorithms.ciphers; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* The Baconian Cipher is a substitution cipher where each letter is represented | ||
* by a group of five binary digits (A's and B's). It can also be used to hide | ||
* messages within other texts, making it a simple form of steganography. | ||
* https://en.wikipedia.org/wiki/Bacon%27s_cipher | ||
* | ||
* @author Bennybebo | ||
*/ | ||
public class BaconianCipher { | ||
|
||
private static final Map<Character, String> BACONIAN_MAP = new HashMap<>(); | ||
private static final Map<String, Character> REVERSE_BACONIAN_MAP = new HashMap<>(); | ||
|
||
static { | ||
// Initialize the Baconian cipher mappings | ||
String[] baconianAlphabet = {"AAAAA", "AAAAB", "AAABA", "AAABB", "AABAA", "AABAB", "AABBA", "AABBB", "ABAAA", "ABAAB", "ABABA", "ABABB", "ABBAA", "ABBAB", "ABBBA", "ABBBB", "BAAAA", "BAAAB", "BAABA", "BAABB", "BABAA", "BABAB", "BABBA", "BABBB", "BBAAA", "BBAAB"}; | ||
char letter = 'A'; | ||
for (String code : baconianAlphabet) { | ||
BACONIAN_MAP.put(letter, code); | ||
REVERSE_BACONIAN_MAP.put(code, letter); | ||
letter++; | ||
} | ||
|
||
// Handle I/J as the same letter | ||
BACONIAN_MAP.put('I', BACONIAN_MAP.get('J')); | ||
REVERSE_BACONIAN_MAP.put(BACONIAN_MAP.get('I'), 'I'); | ||
} | ||
|
||
/** | ||
* Encrypts the given plaintext using the Baconian cipher. | ||
* | ||
* @param plaintext The plaintext message to encrypt. | ||
* @return The ciphertext as a binary (A/B) sequence. | ||
*/ | ||
public String encrypt(String plaintext) { | ||
StringBuilder ciphertext = new StringBuilder(); | ||
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Remove non-letter characters | ||
|
||
for (char letter : plaintext.toCharArray()) { | ||
ciphertext.append(BACONIAN_MAP.get(letter)); | ||
} | ||
|
||
return ciphertext.toString(); | ||
} | ||
|
||
/** | ||
* Decrypts the given ciphertext encoded in binary (A/B) format using the Baconian cipher. | ||
* | ||
* @param ciphertext The ciphertext to decrypt. | ||
* @return The decrypted plaintext message. | ||
*/ | ||
public String decrypt(String ciphertext) { | ||
StringBuilder plaintext = new StringBuilder(); | ||
|
||
for (int i = 0; i < ciphertext.length(); i += 5) { | ||
String code = ciphertext.substring(i, i + 5); | ||
if (REVERSE_BACONIAN_MAP.containsKey(code)) { | ||
plaintext.append(REVERSE_BACONIAN_MAP.get(code)); | ||
} else { | ||
throw new IllegalArgumentException("Invalid Baconian code: " + code); | ||
} | ||
} | ||
|
||
return plaintext.toString(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/thealgorithms/ciphers/DiffieHellman.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.thealgorithms.ciphers; | ||
|
||
import java.math.BigInteger; | ||
|
||
public final class DiffieHellman { | ||
|
||
private final BigInteger base; | ||
private final BigInteger secret; | ||
private final BigInteger prime; | ||
|
||
// Constructor to initialize base, secret, and prime | ||
public DiffieHellman(BigInteger base, BigInteger secret, BigInteger prime) { | ||
// Check for non-null and positive values | ||
if (base == null || secret == null || prime == null || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) { | ||
throw new IllegalArgumentException("Base, secret, and prime must be non-null and positive values."); | ||
} | ||
this.base = base; | ||
this.secret = secret; | ||
this.prime = prime; | ||
} | ||
|
||
// Method to calculate public value (g^x mod p) | ||
public BigInteger calculatePublicValue() { | ||
// Returns g^x mod p | ||
return base.modPow(secret, prime); | ||
} | ||
|
||
// Method to calculate the shared secret key (otherPublic^secret mod p) | ||
public BigInteger calculateSharedSecret(BigInteger otherPublicValue) { | ||
if (otherPublicValue == null || otherPublicValue.signum() <= 0) { | ||
throw new IllegalArgumentException("Other public value must be non-null and positive."); | ||
} | ||
// Returns b^x mod p or a^y mod p | ||
return otherPublicValue.modPow(secret, prime); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.thealgorithms.ciphers; | ||
|
||
public final class MonoAlphabetic { | ||
|
||
private MonoAlphabetic() { | ||
throw new UnsupportedOperationException("Utility class"); | ||
} | ||
|
||
// Encryption method | ||
public static String encrypt(String data, String key) { | ||
if (!data.matches("[A-Z]+")) { | ||
throw new IllegalArgumentException("Input data contains invalid characters. Only uppercase A-Z are allowed."); | ||
} | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
// Encrypt each character | ||
for (char c : data.toCharArray()) { | ||
int idx = charToPos(c); // Get the index of the character | ||
sb.append(key.charAt(idx)); // Map to the corresponding character in the key | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
// Decryption method | ||
public static String decrypt(String data, String key) { | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
// Decrypt each character | ||
for (char c : data.toCharArray()) { | ||
int idx = key.indexOf(c); // Find the index of the character in the key | ||
if (idx == -1) { | ||
throw new IllegalArgumentException("Input data contains invalid characters."); | ||
} | ||
sb.append(posToChar(idx)); // Convert the index back to the original character | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
// Helper method: Convert a character to its position in the alphabet | ||
private static int charToPos(char c) { | ||
return c - 'A'; // Subtract 'A' to get position (0 for A, 1 for B, etc.) | ||
} | ||
|
||
// Helper method: Convert a position in the alphabet to a character | ||
private static char posToChar(int pos) { | ||
return (char) (pos + 'A'); // Add 'A' to convert position back to character | ||
} | ||
} |
Oops, something went wrong.