Skip to content

Commit b39a437

Browse files
committed
Extended Euclid added
1 parent 7fa3b53 commit b39a437

17 files changed

+273
-1
lines changed

β€ŽSecurityLibrary/AES/ExtendedEuclid.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ public class ExtendedEuclid
1616
/// <returns>Mul inverse, -1 if no inv</returns>
1717
public int GetMultiplicativeInverse(int number, int baseN)
1818
{
19-
throw new NotImplementedException();
19+
int i = baseN, v = 0, d = 1;
20+
while (number > 0)
21+
{
22+
int t = i / number, x = number;
23+
number = i % x;
24+
i = x;
25+
x = d;
26+
d = v - t * x;
27+
v = x;
28+
}
29+
v %= baseN;
30+
v = (v < 0)?(v + baseN) % baseN:-1;
31+
return v;
2032
}
2133
}
2234
}
0 Bytes
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

β€ŽSecurityPackage.v12.suo

7.5 KB
Binary file not shown.

β€ŽSecurityPackageTest/AESTest.cs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
ο»Ώusing System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using SecurityLibrary.AES;
4+
5+
namespace SecurityPackageTest
6+
{
7+
[TestClass]
8+
public class AESTest
9+
{
10+
string mainPlain = "0x3243F6A8885A308D313198A2e0370734";
11+
string mainCipher = "0x3925841D02DC09FBDC118597196A0B32";
12+
string mainKey = "0x2B7E151628AED2A6ABF7158809CF4F3C";
13+
14+
string mainPlain2 = "0x00000000000000000000000000000001";
15+
string mainCipher2 = "0x58e2fccefa7e3061367f1d57a4e7455a";
16+
string mainKey2 = "0x00000000000000000000000000000000";
17+
18+
string mainPlain3 = "0x00112233445566778899aabbccddeeff";
19+
string mainCipher3 = "0x69c4e0d86a7b0430d8cdb78070b4c55a";
20+
string mainKey3 = "0x000102030405060708090a0b0c0d0e0f";
21+
22+
string newPlain = "0x54776F204F6E65204E696E652054776F";
23+
string newCipher = "0x29C3505F571420F6402299B31A02D73A";
24+
string newKey = "0x5468617473206D79204B756E67204675";
25+
26+
[TestMethod]
27+
public void AESTestEnc1()
28+
{
29+
AES algorithm = new AES();
30+
string cipher = algorithm.Encrypt(mainPlain, mainKey);
31+
Assert.IsTrue(cipher.Equals(mainCipher, StringComparison.InvariantCultureIgnoreCase));
32+
}
33+
34+
[TestMethod]
35+
public void AESTestDec1()
36+
{
37+
AES algorithm = new AES();
38+
string plain = algorithm.Decrypt(mainCipher, mainKey);
39+
Assert.IsTrue(plain.Equals(mainPlain, StringComparison.InvariantCultureIgnoreCase));
40+
}
41+
42+
[TestMethod]
43+
public void AESTestEnc2()
44+
{
45+
AES algorithm = new AES();
46+
string cipher = algorithm.Encrypt(mainPlain2, mainKey2);
47+
Assert.IsTrue(cipher.Equals(mainCipher2, StringComparison.InvariantCultureIgnoreCase));
48+
}
49+
50+
[TestMethod]
51+
public void AESTestDec2()
52+
{
53+
AES algorithm = new AES();
54+
string plain = algorithm.Decrypt(mainCipher2, mainKey2);
55+
Assert.IsTrue(plain.Equals(mainPlain2, StringComparison.InvariantCultureIgnoreCase));
56+
}
57+
58+
[TestMethod]
59+
public void AESTestEnc3()
60+
{
61+
AES algorithm = new AES();
62+
string cipher = algorithm.Encrypt(mainPlain3, mainKey3);
63+
Assert.IsTrue(cipher.Equals(mainCipher3, StringComparison.InvariantCultureIgnoreCase));
64+
}
65+
66+
[TestMethod]
67+
public void AESTestDec3()
68+
{
69+
AES algorithm = new AES();
70+
string plain = algorithm.Decrypt(mainCipher3, mainKey3);
71+
Assert.IsTrue(plain.Equals(mainPlain3, StringComparison.InvariantCultureIgnoreCase));
72+
}
73+
74+
[TestMethod]
75+
public void AESTestNewEnc()
76+
{
77+
AES algorithm = new AES();
78+
string cipher = algorithm.Encrypt(newPlain, newKey);
79+
Assert.IsTrue(cipher.Equals(newCipher, StringComparison.InvariantCultureIgnoreCase));
80+
}
81+
82+
[TestMethod]
83+
public void AESTestNewDec()
84+
{
85+
AES algorithm = new AES();
86+
string plain = algorithm.Decrypt(newCipher, newKey);
87+
Assert.IsTrue(plain.Equals(newPlain, StringComparison.InvariantCultureIgnoreCase));
88+
}
89+
}
90+
}

β€ŽSecurityPackageTest/DES3DESTest.cs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
ο»Ώusing System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using SecurityLibrary.DES;
4+
using System.Collections.Generic;
5+
6+
namespace SecurityPackageTest
7+
{
8+
[TestClass]
9+
public class DES3DESTest
10+
{
11+
string mainPlain = "0x0123456789ABCDEF";
12+
string mainCipher = "0x85E813540F0AB405";
13+
string mainKey = "0x133457799BBCDFF1";
14+
15+
string mainPlain2 = "0x596F7572206C6970";
16+
string mainCipher2 = "0xC0999FDDE378D7ED";
17+
string mainKey2 = "0x0E329232EA6D0D73";
18+
19+
string mainPlainTriple = "0x0123456789ABCDEF";
20+
string mainCipherTriple = "0x85E813540F0AB405";
21+
List<string> mainKeyTriple = new List<string>() {"0x133457799BBCDFF1", "0x133457799BBCDFF1" };
22+
23+
24+
string newPlain = "0x6D6573736167652E";
25+
string newCipher = "0x7CF45E129445D451";
26+
string newKey = "0x38627974656B6579";
27+
28+
[TestMethod]
29+
public void DESTestEnc1()
30+
{
31+
DES algorithm = new DES();
32+
string cipher = algorithm.Encrypt(mainPlain, mainKey);
33+
Assert.IsTrue(cipher.Equals(mainCipher, StringComparison.InvariantCultureIgnoreCase));
34+
}
35+
36+
[TestMethod]
37+
public void DESTestDec1()
38+
{
39+
DES algorithm = new DES();
40+
string plain = algorithm.Decrypt(mainCipher, mainKey);
41+
Assert.IsTrue(plain.Equals(mainPlain, StringComparison.InvariantCultureIgnoreCase));
42+
}
43+
44+
[TestMethod]
45+
public void DESTestEnc2()
46+
{
47+
DES algorithm = new DES();
48+
string cipher = algorithm.Encrypt(mainPlain2, mainKey2);
49+
Assert.IsTrue(cipher.Equals(mainCipher2, StringComparison.InvariantCultureIgnoreCase));
50+
}
51+
52+
[TestMethod]
53+
public void DESTestDec2()
54+
{
55+
DES algorithm = new DES();
56+
string plain = algorithm.Decrypt(mainCipher2, mainKey2);
57+
Assert.IsTrue(plain.Equals(mainPlain2, StringComparison.InvariantCultureIgnoreCase));
58+
}
59+
60+
[TestMethod]
61+
public void TripleDESTestEnc1()
62+
{
63+
TripleDES algorithm = new TripleDES();
64+
string cipher = algorithm.Encrypt(mainPlainTriple, mainKeyTriple);
65+
Assert.IsTrue(cipher.Equals(mainCipherTriple, StringComparison.InvariantCultureIgnoreCase));
66+
}
67+
68+
[TestMethod]
69+
public void TripleDESTestDec1()
70+
{
71+
TripleDES algorithm = new TripleDES();
72+
string plain = algorithm.Decrypt(mainCipherTriple, mainKeyTriple);
73+
Assert.IsTrue(plain.Equals(mainPlainTriple, StringComparison.InvariantCultureIgnoreCase));
74+
}
75+
76+
77+
[TestMethod]
78+
public void DESTestNewEnc()
79+
{
80+
DES algorithm = new DES();
81+
string cipher = algorithm.Encrypt(newPlain, newKey);
82+
Assert.IsTrue(cipher.Equals(newCipher, StringComparison.InvariantCultureIgnoreCase));
83+
}
84+
85+
[TestMethod]
86+
public void DESTestNewDec()
87+
{
88+
DES algorithm = new DES();
89+
string plain = algorithm.Decrypt(newCipher, newKey);
90+
Assert.IsTrue(plain.Equals(newPlain, StringComparison.InvariantCultureIgnoreCase));
91+
}
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
ο»Ώusing System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using SecurityLibrary.AES;
4+
5+
namespace SecurityPackageTest
6+
{
7+
[TestClass]
8+
public class ExtendedEuclidTest
9+
{
10+
[TestMethod]
11+
public void ExtendedEuclidTest1()
12+
{
13+
ExtendedEuclid algorithm = new ExtendedEuclid();
14+
int res = algorithm.GetMultiplicativeInverse(23, 26);
15+
Assert.AreEqual(res, 17);
16+
}
17+
18+
[TestMethod]
19+
public void ExtendedEuclidTest2()
20+
{
21+
ExtendedEuclid algorithm = new ExtendedEuclid();
22+
int res = algorithm.GetMultiplicativeInverse(22, 26);
23+
Assert.AreEqual(res, -1);
24+
}
25+
26+
[TestMethod]
27+
public void ExtendedEuclidTest3()
28+
{
29+
ExtendedEuclid algorithm = new ExtendedEuclid();
30+
int res = algorithm.GetMultiplicativeInverse(50, 71);
31+
Assert.AreEqual(res, 27);
32+
}
33+
34+
[TestMethod]
35+
public void ExtendedEuclidTest4()
36+
{
37+
ExtendedEuclid algorithm = new ExtendedEuclid();
38+
int res = algorithm.GetMultiplicativeInverse(43, 64);
39+
Assert.AreEqual(res, 3);
40+
}
41+
42+
[TestMethod]
43+
public void ExtendedEuclidTest5()
44+
{
45+
ExtendedEuclid algorithm = new ExtendedEuclid();
46+
int res = algorithm.GetMultiplicativeInverse(1111, 22222);
47+
Assert.AreEqual(res, 11101);
48+
}
49+
50+
[TestMethod]
51+
public void ExtendedEuclidTest6()
52+
{
53+
ExtendedEuclid algorithm = new ExtendedEuclid();
54+
int res = algorithm.GetMultiplicativeInverse(123456789, 1236);
55+
Assert.AreEqual(res, -1);
56+
}
57+
58+
[TestMethod]
59+
public void ExtendedEuclidTest7()
60+
{
61+
ExtendedEuclid algorithm = new ExtendedEuclid();
62+
int res = algorithm.GetMultiplicativeInverse(123456789, 12365);
63+
Assert.AreEqual(res, 3729);
64+
}
65+
66+
[TestMethod]
67+
public void ExtendedEuclidNewTest()
68+
{
69+
ExtendedEuclid algorithm = new ExtendedEuclid();
70+
int res = algorithm.GetMultiplicativeInverse(13245687, 135469);
71+
Assert.AreEqual(res, 38164);
72+
}
73+
}
74+
}

β€ŽSecurityPackageTest/SecurityPackageTest.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@
5353
</Otherwise>
5454
</Choose>
5555
<ItemGroup>
56+
<Compile Include="AESTest.cs" />
5657
<Compile Include="ColumnarTest.cs" />
58+
<Compile Include="DES3DESTest.cs" />
59+
<Compile Include="ExtendedEuclidTest.cs" />
5760
<Compile Include="HillCipherTest.cs" />
5861
<Compile Include="PlayfairTest.cs" />
5962
<Compile Include="MonoalphabeticTest.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)