mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 10:15:51 +08:00
Add DiffieHellman and MonoAlphabetic (#5508)
This commit is contained in:
@ -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));
|
||||
}
|
||||
}
|
@ -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"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user