Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4d7782d

Browse files
RSA Algorithm added
1 parent fcd8151 commit 4d7782d

16 files changed

+41
-14
lines changed

‎SecurityLibrary/AES/ExtendedEuclid.cs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,34 @@ public class ExtendedEuclid
1616
/// <returns>Mul inverse, -1 if no inv</returns>
1717
public int GetMultiplicativeInverse(int number, int baseN)
1818
{
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;
19+
int b=number;
20+
int m = baseN;
21+
int a1=1,a2=0,a3=m;
22+
int b1=0,b2=1,b3=b;
23+
24+
while(b3!=0 && b3 !=1){
25+
int q = a3/b3;
26+
int t1 = a1-(q*b1)
27+
,t2 = a2-(q*b2)
28+
,t3 = a3-(q*b3);
29+
30+
a1 = b1;
31+
a2 = b2;
32+
a3 = b3;
33+
b1 = t1;
34+
b2 = t2;
35+
b3 = t3;
36+
}
37+
38+
if(b3==0){
39+
return -1;
40+
}
41+
else if(b3 == 1){
42+
b2=b2<-1?b2+baseN:b2;
43+
return b2;
2844
}
29-
v %= baseN;
30-
v = (v < 0)?(v + baseN) % baseN:-1;
31-
return v;
45+
46+
return -1;
3247
}
3348
}
3449
}

‎SecurityLibrary/RSA/RSA.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using SecurityLibrary.DiffieHellman;
7+
using SecurityLibrary.AES;
68

79
namespace SecurityLibrary.RSA
810
{
911
public class RSA
1012
{
13+
public DiffieHellman.DiffieHellman obj = new DiffieHellman.DiffieHellman();
14+
public AES.ExtendedEuclid ex = new AES.ExtendedEuclid();
1115
public int Encrypt(int p, int q, int M, int e)
1216
{
13-
throw new NotImplementedException();
17+
int n = p * q;
18+
int me = obj.pow(M, e, n)%n;
19+
return me;
1420
}
1521

1622
public int Decrypt(int p, int q, int C, int e)
1723
{
18-
throw new NotImplementedException();
24+
int n2 = p * q;
25+
int n = (p-1) * (q-1);
26+
e = ex.GetMultiplicativeInverse(e,n);
27+
int cd = obj.pow(C, e, n2);
28+
return cd;
1929
}
2030
}
2131
}

‎SecurityLibrary/SecurityLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<ItemGroup>
5050
<Compile Include="AES\AES.cs" />
5151
<Compile Include="AES\ExtendedEuclid.cs" />
52+
<Compile Include="ElGamal\ELGAMAL.cs" />
5253
<Compile Include="MainAlgorithms\Columnar.cs" />
5354
<Compile Include="DES\DES.cs" />
5455
<Compile Include="DES\TripleDES.cs" />
512 Bytes
Binary file not shown.
14 KB
Binary file not shown.
512 Bytes
Binary file not shown.
14 KB
Binary file not shown.

‎SecurityPackage.v12.suo

6 KB
Binary file not shown.

‎SecurityPackageTest/SecurityPackageTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<Compile Include="ColumnarTest.cs" />
5858
<Compile Include="DeffieHelmanTest.cs" />
5959
<Compile Include="DES3DESTest.cs" />
60+
<Compile Include="ElGamalTest.cs" />
6061
<Compile Include="ExtendedEuclidTest.cs" />
6162
<Compile Include="HillCipherTest.cs" />
6263
<Compile Include="PlayfairTest.cs" />
512 Bytes
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /