Skip to content

Commit fcd8151

Browse files
committed
Diffie-Hellman added
1 parent 880c8bc commit fcd8151

16 files changed

+114
-1
lines changed

β€ŽSecurityLibrary/DiffieHellman/DiffieHellman.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,25 @@ namespace SecurityLibrary.DiffieHellman
88
{
99
public class DiffieHellman
1010
{
11+
public int pow(int a, int b, int c){
12+
int res = 1;
13+
for(int i=0;i<b;i++){
14+
res = (res*a)%c;
15+
}
16+
return res;
17+
}
1118
public List<int> GetKeys(int q, int alpha, int xa, int xb)
1219
{
13-
throw new NotImplementedException();
20+
int ya = pow(alpha, xa, q);
21+
int yb = pow(alpha, xb, q);
22+
int k1 = pow(yb, xa, q);
23+
int k2 = pow(ya, xb, q);
24+
25+
List<int> result = new List<int>();
26+
result.Add(k1);
27+
result.Add(k2);
28+
29+
return result;
1430
}
1531
}
1632
}
512 Bytes
Binary file not shown.
Binary file not shown.
512 Bytes
Binary file not shown.
Binary file not shown.

β€ŽSecurityPackage.v12.suo

0 Bytes
Binary file not shown.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
ο»Ώusing System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using SecurityLibrary.DiffieHellman;
4+
using System.Collections.Generic;
5+
6+
namespace SecurityPackageTest
7+
{
8+
9+
[TestClass]
10+
public class DiffieHellmanTest
11+
{
12+
[TestMethod]
13+
public void DeffieHelmanTest1()
14+
{
15+
DiffieHellman algorithm = new DiffieHellman();
16+
List<int> key = algorithm.GetKeys(19, 2, 6, 13);
17+
Assert.AreEqual(key[0], 7);
18+
Assert.AreEqual(key[1], 7);
19+
}
20+
21+
[TestMethod]
22+
public void DeffieHelmanTest2()
23+
{
24+
DiffieHellman algorithm = new DiffieHellman();
25+
List<int> key = algorithm.GetKeys(353, 2, 97, 233);
26+
Assert.AreEqual(key[0], 81);
27+
Assert.AreEqual(key[1], 81);
28+
}
29+
30+
[TestMethod]
31+
public void DeffieHelmanTest3()
32+
{
33+
DiffieHellman algorithm = new DiffieHellman();
34+
List<int> key = algorithm.GetKeys(353, 3, 97, 233);
35+
Assert.AreEqual(key[0], 160);
36+
Assert.AreEqual(key[1], 160);
37+
}
38+
39+
[TestMethod]
40+
public void DeffieHelmanNewTest()
41+
{
42+
DiffieHellman algorithm = new DiffieHellman();
43+
List<int> key = algorithm.GetKeys(541, 10, 50, 100);
44+
Assert.AreEqual(key[0], 449);
45+
Assert.AreEqual(key[1], 449);
46+
}
47+
}
48+
}

β€ŽSecurityPackageTest/ElGamalTest.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
ο»Ώusing System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using SecurityLibrary.ElGamal;
4+
using System.Collections.Generic;
5+
6+
namespace SecurityPackageTest
7+
{
8+
9+
[TestClass]
10+
public class ElGamalTest
11+
{
12+
[TestMethod]
13+
public void ElGamalEnc1()
14+
{
15+
ElGamal algorithm = new ElGamal();
16+
List<long> cipher = algorithm.Encrypt(7187, 4842, 4464, 19, 19);//191
17+
Assert.AreEqual(cipher[0], 2781);
18+
Assert.AreEqual(cipher[1], 437);
19+
}
20+
21+
[TestMethod]
22+
public void ElGamalEnc2()
23+
{
24+
ElGamal algorithm = new ElGamal();
25+
List<long> cipher = algorithm.Encrypt(6323, 4736, 2231, 58, 111);//118
26+
Assert.AreEqual(cipher[0], 6066);
27+
Assert.AreEqual(cipher[1], 899);
28+
}
29+
30+
[TestMethod]
31+
public void ElGamalDec1()
32+
{
33+
ElGamal algorithm = new ElGamal();
34+
int plain = algorithm.Decrypt(2781, 437, 191, 7187);
35+
Assert.AreEqual(plain, 19);
36+
}
37+
38+
[TestMethod]
39+
public void ElGamalDec2()
40+
{
41+
ElGamal algorithm = new ElGamal();
42+
int plain = algorithm.Decrypt(6066, 899, 118, 6323);
43+
Assert.AreEqual(plain, 111);
44+
}
45+
}
46+
}
47+
48+

β€ŽSecurityPackageTest/SecurityPackageTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<ItemGroup>
5656
<Compile Include="AESTest.cs" />
5757
<Compile Include="ColumnarTest.cs" />
58+
<Compile Include="DeffieHelmanTest.cs" />
5859
<Compile Include="DES3DESTest.cs" />
5960
<Compile Include="ExtendedEuclidTest.cs" />
6061
<Compile Include="HillCipherTest.cs" />
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)