-
Notifications
You must be signed in to change notification settings - Fork 20.5k
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
+151
−0
Merged
Changes from 59 commits
Commits
Show all changes
77 commits
Select commit
Hold shift + click to select a range
835783d
Create DiffieHellman.java
riti2601 8e1d3cf
Update DiffieHellman.java
riti2601 ee197e7
Update DiffieHellman.java
riti2601 998592d
Update DiffieHellman.java
riti2601 b9b5363
Update DiffieHellman.java
riti2601 b18cc32
Update DiffieHellman.java
riti2601 6f5b5fc
Update DiffieHellman.java
riti2601 bbc6927
Create MonoAlphabetic.java
riti2601 3e9faea
Update MonoAlphabetic.java
riti2601 7223380
Update MonoAlphabetic.java
riti2601 dc5b5f0
Update MonoAlphabetic.java
riti2601 b52479d
Update MonoAlphabetic.java
riti2601 0fd2b57
Update MonoAlphabetic.java
riti2601 749099f
Update MonoAlphabetic.java
riti2601 31909e8
Update MonoAlphabetic.java
riti2601 8cbe08a
Update MonoAlphabetic.java
riti2601 7bdbec1
Update MonoAlphabetic.java
riti2601 6daef3e
removed the main methods and added proper JUnits tests
riti2601 1b3b5b9
Merge branch 'master' into master
riti2601 bd54505
Removed the main methods and added proper JUnit tests to DiffieHellma…
riti2601 f021dbb
updated DiffieHellman.java
riti2601 175def5
Update MonoAlphabetic.java
riti2601 1d2cc01
Create DiffieHellmanTest.java
riti2601 db4d4dc
Update DiffieHellmanTest.java
riti2601 afdf7e4
Create MonoAlphabeticTest.java
riti2601 2bd00f8
Update DiffieHellman.java
riti2601 98589d9
Update MonoAlphabetic.java
riti2601 9fbfa38
Update DiffieHellmanTest.java
riti2601 c3dcd2d
Update MonoAlphabeticTest.java
riti2601 8b580df
Update MonoAlphabeticTest.java
riti2601 a31ae3b
Update DiffieHellmanTest.java
riti2601 283b5f9
Update MonoAlphabeticTest.java
riti2601 5e8b536
Update DiffieHellman.java
riti2601 32889e7
Update MonoAlphabetic.java
riti2601 577a9b7
Update DiffieHellmanTest.java
riti2601 697eed5
Update MonoAlphabeticTest.java
riti2601 c7cafbc
Update MonoAlphabeticTest.java
riti2601 eea1c0c
Update DiffieHellmanTest.java
riti2601 f2ccca5
Update MonoAlphabetic.java
riti2601 40e69d2
Update DiffieHellman.java
riti2601 423455c
Update DiffieHellmanTest.java
riti2601 2539034
Update DiffieHellmanTest.java
riti2601 888314f
Update MonoAlphabeticTest.java
riti2601 63f803f
Update MonoAlphabeticTest.java
riti2601 d28faa2
Update MonoAlphabetic.java
riti2601 6efedf6
Update MonoAlphabetic.java
riti2601 f9e0a16
Update MonoAlphabeticTest.java
riti2601 f5f2746
Update MonoAlphabeticTest.java
riti2601 d4e7bb6
Update MonoAlphabeticTest.java
riti2601 6e55c9c
Update DiffieHellman.java
riti2601 6543b6c
Update DiffieHellmanTest.java
riti2601 c54c74a
Update DiffieHellmanTest.java
riti2601 5274793
Merge branch 'master' into master
riti2601 cdb0c91
Merge branch 'master' into master
riti2601 a8f58f6
Update MonoAlphabeticTest.java
riti2601 abfe77e
Update src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java
riti2601 e36f87c
added exception in MonoAlphabetic.java
riti2601 d1a9080
added methods charToPos and posToChar in MonoAlphabetic.java
riti2601 40274c2
Update DiffieHellmanTest.java
riti2601 a4831b6
Update MonoAlphabetic.java
riti2601 5b5d9ef
Update DiffieHellmanTest.java
riti2601 b94f1f9
Update MonoAlphabeticTest.java
riti2601 82ecc32
Update MonoAlphabeticTest.java
riti2601 8243b5a
Update MonoAlphabetic.java
riti2601 cad2f32
Update MonoAlphabetic.java
riti2601 8ffc13b
Update MonoAlphabetic.java
riti2601 736ec03
Update MonoAlphabetic.java
riti2601 897d359
Update MonoAlphabetic.java
riti2601 2c8fb5d
Update MonoAlphabetic.java
riti2601 3ecff03
Update MonoAlphabeticTest.java
riti2601 a157a27
Merge branch 'master' into master
riti2601 be08838
Merge branch 'master' into master
riti2601 fb37afe
Merge branch 'master' into master
riti2601 a34e09e
Merge branch 'master' into master
riti2601 1216c9b
Merge branch 'master' into master
riti2601 e9af514
Merge branch 'master' into master
riti2601 338859c
Merge branch 'master' into master
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/com/thealgorithms/ciphers/DiffieHellman.java
This file contains hidden or 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 | ||
riti2601 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return otherPublicValue.modPow(secret, prime); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java
This file contains hidden or 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,51 @@ | ||
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(); | ||
data = data.toUpperCase(); | ||
riti2601 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
for (char c : data.toCharArray()) { | ||
if (c >= 'A' && c <= 'Z') { | ||
int idx = c - 'A'; // Index in alphabet | ||
riti2601 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
sb.append(key.charAt(idx)); // Append the character from the key | ||
} else { | ||
sb.append(c); // Append non-alphabet characters directly | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
// Decryption method | ||
public static String decrypt(String data, String key) { | ||
StringBuilder sb = new StringBuilder(); | ||
data = data.toUpperCase(); | ||
|
||
for (char c : data.toCharArray()) { | ||
if (c >= 'A' && c <= 'Z') { | ||
int idx = charToPos(c); // Get the index in the key | ||
char encryptedChar = key.charAt(idx); | ||
sb.append(encryptedChar); | ||
} | ||
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 | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/com/thealgorithms/ciphers/DiffieHellmanTest.java
This file contains hidden or 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,40 @@ | ||
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)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/com/thealgorithms/ciphers/MonoAlphabeticTest.java
This file contains hidden or 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,28 @@ | ||
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 | ||
assertEquals(encryptedText, MonoAlphabetic.encrypt(plainText, key)); | ||
|
||
// Test decryption | ||
assertEquals(plainText, MonoAlphabetic.decrypt(encryptedText, 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", "UJJYU"), Arguments.of("JAVA", "QWERTYUIOPLKJHGFDSAZXCVBNM", "KZHS")); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.