Skip to content

Created DiffieHellman.java and MonoAlphabetic.java #5508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 77 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
835783d
Create DiffieHellman.java
riti2601 Oct 1, 2024
8e1d3cf
Update DiffieHellman.java
riti2601 Oct 1, 2024
ee197e7
Update DiffieHellman.java
riti2601 Oct 1, 2024
998592d
Update DiffieHellman.java
riti2601 Oct 1, 2024
b9b5363
Update DiffieHellman.java
riti2601 Oct 1, 2024
b18cc32
Update DiffieHellman.java
riti2601 Oct 1, 2024
6f5b5fc
Update DiffieHellman.java
riti2601 Oct 1, 2024
bbc6927
Create MonoAlphabetic.java
riti2601 Oct 1, 2024
3e9faea
Update MonoAlphabetic.java
riti2601 Oct 1, 2024
7223380
Update MonoAlphabetic.java
riti2601 Oct 1, 2024
dc5b5f0
Update MonoAlphabetic.java
riti2601 Oct 1, 2024
b52479d
Update MonoAlphabetic.java
riti2601 Oct 1, 2024
0fd2b57
Update MonoAlphabetic.java
riti2601 Oct 1, 2024
749099f
Update MonoAlphabetic.java
riti2601 Oct 1, 2024
31909e8
Update MonoAlphabetic.java
riti2601 Oct 1, 2024
8cbe08a
Update MonoAlphabetic.java
riti2601 Oct 1, 2024
7bdbec1
Update MonoAlphabetic.java
riti2601 Oct 1, 2024
6daef3e
removed the main methods and added proper JUnits tests
riti2601 Oct 2, 2024
1b3b5b9
Merge branch 'master' into master
riti2601 Oct 2, 2024
bd54505
Removed the main methods and added proper JUnit tests to DiffieHellma…
riti2601 Oct 2, 2024
f021dbb
updated DiffieHellman.java
riti2601 Oct 2, 2024
175def5
Update MonoAlphabetic.java
riti2601 Oct 2, 2024
1d2cc01
Create DiffieHellmanTest.java
riti2601 Oct 2, 2024
db4d4dc
Update DiffieHellmanTest.java
riti2601 Oct 2, 2024
afdf7e4
Create MonoAlphabeticTest.java
riti2601 Oct 2, 2024
2bd00f8
Update DiffieHellman.java
riti2601 Oct 2, 2024
98589d9
Update MonoAlphabetic.java
riti2601 Oct 2, 2024
9fbfa38
Update DiffieHellmanTest.java
riti2601 Oct 2, 2024
c3dcd2d
Update MonoAlphabeticTest.java
riti2601 Oct 2, 2024
8b580df
Update MonoAlphabeticTest.java
riti2601 Oct 2, 2024
a31ae3b
Update DiffieHellmanTest.java
riti2601 Oct 2, 2024
283b5f9
Update MonoAlphabeticTest.java
riti2601 Oct 2, 2024
5e8b536
Update DiffieHellman.java
riti2601 Oct 2, 2024
32889e7
Update MonoAlphabetic.java
riti2601 Oct 2, 2024
577a9b7
Update DiffieHellmanTest.java
riti2601 Oct 2, 2024
697eed5
Update MonoAlphabeticTest.java
riti2601 Oct 2, 2024
c7cafbc
Update MonoAlphabeticTest.java
riti2601 Oct 2, 2024
eea1c0c
Update DiffieHellmanTest.java
riti2601 Oct 2, 2024
f2ccca5
Update MonoAlphabetic.java
riti2601 Oct 2, 2024
40e69d2
Update DiffieHellman.java
riti2601 Oct 2, 2024
423455c
Update DiffieHellmanTest.java
riti2601 Oct 2, 2024
2539034
Update DiffieHellmanTest.java
riti2601 Oct 2, 2024
888314f
Update MonoAlphabeticTest.java
riti2601 Oct 2, 2024
63f803f
Update MonoAlphabeticTest.java
riti2601 Oct 2, 2024
d28faa2
Update MonoAlphabetic.java
riti2601 Oct 2, 2024
6efedf6
Update MonoAlphabetic.java
riti2601 Oct 3, 2024
f9e0a16
Update MonoAlphabeticTest.java
riti2601 Oct 3, 2024
f5f2746
Update MonoAlphabeticTest.java
riti2601 Oct 3, 2024
d4e7bb6
Update MonoAlphabeticTest.java
riti2601 Oct 3, 2024
6e55c9c
Update DiffieHellman.java
riti2601 Oct 3, 2024
6543b6c
Update DiffieHellmanTest.java
riti2601 Oct 3, 2024
c54c74a
Update DiffieHellmanTest.java
riti2601 Oct 3, 2024
5274793
Merge branch 'master' into master
riti2601 Oct 6, 2024
cdb0c91
Merge branch 'master' into master
riti2601 Oct 7, 2024
a8f58f6
Update MonoAlphabeticTest.java
riti2601 Oct 7, 2024
abfe77e
Update src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java
riti2601 Oct 7, 2024
e36f87c
added exception in MonoAlphabetic.java
riti2601 Oct 7, 2024
d1a9080
added methods charToPos and posToChar in MonoAlphabetic.java
riti2601 Oct 7, 2024
40274c2
Update DiffieHellmanTest.java
riti2601 Oct 7, 2024
a4831b6
Update MonoAlphabetic.java
riti2601 Oct 7, 2024
5b5d9ef
Update DiffieHellmanTest.java
riti2601 Oct 7, 2024
b94f1f9
Update MonoAlphabeticTest.java
riti2601 Oct 7, 2024
82ecc32
Update MonoAlphabeticTest.java
riti2601 Oct 7, 2024
8243b5a
Update MonoAlphabetic.java
riti2601 Oct 7, 2024
cad2f32
Update MonoAlphabetic.java
riti2601 Oct 7, 2024
8ffc13b
Update MonoAlphabetic.java
riti2601 Oct 7, 2024
736ec03
Update MonoAlphabetic.java
riti2601 Oct 7, 2024
897d359
Update MonoAlphabetic.java
riti2601 Oct 7, 2024
2c8fb5d
Update MonoAlphabetic.java
riti2601 Oct 7, 2024
3ecff03
Update MonoAlphabeticTest.java
riti2601 Oct 7, 2024
a157a27
Merge branch 'master' into master
riti2601 Oct 9, 2024
be08838
Merge branch 'master' into master
riti2601 Oct 14, 2024
fb37afe
Merge branch 'master' into master
riti2601 Oct 15, 2024
a34e09e
Merge branch 'master' into master
riti2601 Oct 16, 2024
1216c9b
Merge branch 'master' into master
riti2601 Oct 16, 2024
e9af514
Merge branch 'master' into master
riti2601 Oct 22, 2024
338859c
Merge branch 'master' into master
siriak Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/com/thealgorithms/ciphers/DiffieHellman.java
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 src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java
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
}
}
38 changes: 38 additions & 0 deletions src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.thealgorithms.ciphers;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigInteger;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class DiffieHellmanTest {

// Test for public value calculation using instance methods
@ParameterizedTest
@MethodSource("provideTestData")
public void testCalculatePublicValue(BigInteger base, BigInteger secret, BigInteger prime, BigInteger publicExpected, BigInteger sharedExpected) {
DiffieHellman dh = new DiffieHellman(base, secret, prime); // Create an instance of DiffieHellman
assertEquals(publicExpected, dh.calculatePublicValue()); // Call instance method
}

// Test for shared secret calculation using instance methods
@ParameterizedTest
@MethodSource("provideTestData")
public void testCalculateSharedSecret(BigInteger base, BigInteger secret, BigInteger prime, BigInteger publicExpected, BigInteger sharedExpected) {
DiffieHellman dh = new DiffieHellman(base, secret, prime); // Create an instance of DiffieHellman
assertEquals(sharedExpected, dh.calculateSharedSecret(publicExpected)); // Call instance method
}

// Provide test data for both public key and shared secret calculation
private static Stream<Arguments> provideTestData() {
return Stream.of(createTestArgs(5, 6, 23, 8, 13), createTestArgs(2, 5, 13, 6, 2));
}

// Helper method for arguments
private static Arguments createTestArgs(long base, long secret, long prime, long publicExpected, long sharedExpected) {
return Arguments.of(BigInteger.valueOf(base), BigInteger.valueOf(secret), BigInteger.valueOf(prime), BigInteger.valueOf(publicExpected), BigInteger.valueOf(sharedExpected));
}
}
29 changes: 29 additions & 0 deletions src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.thealgorithms.ciphers;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class MonoAlphabeticTest {

// Test for both encryption and decryption with different keys
@ParameterizedTest
@MethodSource("provideTestData")
public void testEncryptDecrypt(String plainText, String key, String encryptedText) {
// Test encryption
String actualEncrypted = MonoAlphabetic.encrypt(plainText, key);
assertEquals(encryptedText, actualEncrypted, "Encryption failed for input: " + plainText + " with key: " + key);

// Test decryption
String actualDecrypted = MonoAlphabetic.decrypt(encryptedText, key);
assertEquals(plainText, actualDecrypted, "Decryption failed for input: " + encryptedText + " with key: " + key);
}

// Provide test data for both encryption and decryption
private static Stream<Arguments> provideTestData() {
return Stream.of(Arguments.of("HELLO", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "LCGGS"), Arguments.of("JAVA", "MNBVCXZLKJHGFDSAPOIUYTREWQ", "JMTM"), Arguments.of("HELLO", "QWERTYUIOPLKJHGFDSAZXCVBNM", "ITKKG"), Arguments.of("JAVA", "QWERTYUIOPLKJHGFDSAZXCVBNM", "PQCQ"));
}
}