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 17 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
42 changes: 42 additions & 0 deletions src/main/java/com/thealgorithms/ciphers/DiffieHellman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.math.BigInteger;
import java.util.Scanner;

public final class DiffieHellman {
private DiffieHellman() {
throw new UnsupportedOperationException("Utility class");
}
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
System.out.println("Hello User! \nEnter your name:");
String name = read.nextLine();
read.nextLine();
System.out.println("Welcome " + name + "!");

BigInteger n;
BigInteger g;
BigInteger x;
BigInteger y;
BigInteger k1;
BigInteger k2;
BigInteger a;
BigInteger b;

System.out.println("Enter two prime numbers: ");
n = new BigInteger(read.next());
g = new BigInteger(read.next());

System.out.println("Person A : Enter your secret number");
x = new BigInteger(read.next());
a = g.modPow(x, n);

System.out.println("Person B : Enter your secret number");
y = new BigInteger(read.next());
b = g.modPow(y, n);

k1 = b.modPow(x, n);
k2 = a.modPow(y, n);

System.out.println("A's secret key: " + k1);
System.out.println("B's secret key: " + k2);
}
}
72 changes: 72 additions & 0 deletions src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import java.util.Scanner;

public final class MonoAlphabetic {
private MonoAlphabetic() {
throw new UnsupportedOperationException("Utility class");
}
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
System.out.println("Hello User! \nEnter your name:");
String name = read.nextLine();
read.nextLine();
System.out.println("Welcome " + name + "!\nDo you want to encrypt data or decrypt the data?\nFor encryption enter: 1\nFor decryption enter: 2");
int x = read.nextInt();

String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ";

switch (x) {
case 1:
System.out.println("\nPlease enter the data that is to be encrypted, we will be using MonoAlphabetic Cipher to encrypt the data.");
String data = read.nextLine().toUpperCase();
String encryptedData = encrypt(data, key);
System.out.println("Encrypted data: " + encryptedData);
break;
case 2:
System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data.");
data = read.nextLine().toUpperCase();
String decryptedData = decrypt(data, key);
System.out.println("Decrypted data: " + decryptedData);
break;
default:
System.out.println("The input was invalid. Kindly restart.");
break;
}
}

public static String encrypt(String data, String key) {
int idx;
char c;
StringBuffer sb = new StringBuffer(data);

for (int i = 0; i < sb.length(); i++) {
idx = sb.charAt(i) - 65;
c = key.charAt(idx);
sb.setCharAt(i, c);
}
return new String(sb);
}

public static String decrypt(String data, String key) {
int idx;
char c;
StringBuffer sb = new StringBuffer(data);

for (int i = 0; i < sb.length(); i++) {
c = sb.charAt(i);
idx = getIndex(c, key);
c = (char) (idx + 65);
sb.setCharAt(i, c);
}
return new String(sb);
}

public static int getIndex(char c, String key) {
int idx = -1;
for (int i = 0; i < key.length(); i++) {
if (key.charAt(i) == c) {
idx = i;
}
}
return idx;
}
}