Skip to content

Commit 880c8bc

Browse files
committed
RC4
1 parent 4c5dd3e commit 880c8bc

19 files changed

+1248
-7
lines changed

SecurityLibrary/DES/DES.cs

Lines changed: 493 additions & 2 deletions
Large diffs are not rendered by default.

SecurityLibrary/DES/TripleDES.cs

Lines changed: 522 additions & 3 deletions
Large diffs are not rendered by default.

SecurityLibrary/ElGamal/ELGAMAL.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SecurityLibrary.ElGamal
8+
{
9+
public class ElGamal
10+
{
11+
/// <summary>
12+
/// Encryption
13+
/// </summary>
14+
/// <param name="alpha"></param>
15+
/// <param name="q"></param>
16+
/// <param name="y"></param>
17+
/// <param name="k"></param>
18+
/// <returns>list[0] = C1, List[1] = C2</returns>
19+
public List<long> Encrypt(int q, int alpha, int y, int k, int m)
20+
{
21+
throw new NotImplementedException();
22+
23+
}
24+
public int Decrypt(int c1, int c2, int x, int q)
25+
{
26+
throw new NotImplementedException();
27+
28+
}
29+
}
30+
}

SecurityLibrary/RC4/RC4.cs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,75 @@ public class RC4 : CryptographicTechnique
1313
{
1414
public override string Decrypt(string cipherText, string key)
1515
{
16-
throw new NotImplementedException();
16+
return Encrypt(cipherText, key);
1717
}
1818

1919
public override string Encrypt(string plainText, string key)
2020
{
21-
throw new NotImplementedException();
21+
bool flag = false;
22+
if (plainText[1] == 'x' && plainText[0] == '0')
23+
{
24+
flag = true;
25+
string tmpP = "";
26+
for (int i = 2; i < plainText.Length; i+=2)
27+
{
28+
tmpP += char.ConvertFromUtf32(Convert.ToInt32(plainText[i].ToString()+plainText[i+1].ToString(), 16));
29+
}
30+
plainText = tmpP;
31+
}
32+
33+
if (key[0] == '0' && key[1] == 'x')
34+
{
35+
string tmpK = "";
36+
for (int i = 2; i < key.Length; i+=2)
37+
{
38+
tmpK += char.ConvertFromUtf32(Convert.ToInt32(key[i].ToString()+key[i+1].ToString(), 16));
39+
}
40+
key = tmpK;
41+
}
42+
int[] S = new int[256];
43+
int[] T = new int[256];
44+
for (int i = 0; i < 256; i++)
45+
{
46+
S[i] = i;
47+
T[i] = key[i % key.Length];
48+
}
49+
50+
int j = 0;
51+
for (int i = 0; i < 256; i++)
52+
{
53+
j = (j + S[i] + T[i]) % 256;
54+
int tmp = S[i];
55+
S[i] = S[j];
56+
S[j] = tmp;
57+
}
58+
59+
int a=0, l=0, k = 0;
60+
int plLength = plainText.Length;
61+
int t;
62+
63+
string C = "";
64+
for (int i = 0; i < plainText.Length; i++)
65+
{
66+
a = (a + 1) % 256;
67+
l = (l + S[a]) % 256;
68+
int tmp;
69+
tmp = S[a];
70+
S[a] = S[l];
71+
S[l] = tmp;
72+
t = (S[a] + S[l]) % 256;
73+
k = S[t];
74+
Console.WriteLine(plainText[i].ToString() + k.ToString());
75+
C += char.ConvertFromUtf32((plainText[i] ^ k));
76+
}
77+
78+
if (flag)
79+
{
80+
C = string.Join("", C.Select(c => ((int)c).ToString("x2")));
81+
C = "0x" + C;
82+
}
83+
Console.WriteLine(C);
84+
return C;
2285
}
2386
}
2487
}
26 KB
Binary file not shown.
26 KB
Binary file not shown.
26 KB
Binary file not shown.
26 KB
Binary file not shown.

SecurityPackage.v12.suo

12.5 KB
Binary file not shown.

SecurityPackageTest/RC4Test.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using SecurityLibrary.RC4;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace SecurityPackageTest
10+
{
11+
[TestClass]
12+
public class RC4Test
13+
{
14+
[TestMethod]
15+
public void RC4TestEnc1()
16+
{
17+
RC4 algorithm = new RC4();
18+
string cipher = algorithm.Encrypt("abcd", "test");
19+
Assert.IsTrue(cipher.Equals("ÏíDu"));
20+
}
21+
22+
[TestMethod]
23+
public void RC4TestDec1()
24+
{
25+
RC4 algorithm = new RC4();
26+
string cipher = algorithm.Decrypt("ÏíDu", "test");
27+
Assert.IsTrue(cipher.Equals("abcd"));
28+
}
29+
30+
[TestMethod]
31+
public void RC4TestEnc2()
32+
{
33+
RC4 algorithm = new RC4();
34+
string cipher = algorithm.Encrypt("0x61626364", "0x74657374");
35+
Assert.IsTrue(cipher.Equals("0xcfed4475", StringComparison.InvariantCultureIgnoreCase));
36+
}
37+
38+
[TestMethod]
39+
public void RC4TestDec2()
40+
{
41+
RC4 algorithm = new RC4();
42+
string cipher = algorithm.Encrypt("0xcfed4475", "0x74657374");
43+
Assert.IsTrue(cipher.Equals("0x61626364", StringComparison.InvariantCultureIgnoreCase));
44+
}
45+
46+
[TestMethod]
47+
public void RC4TestNewEnc()
48+
{
49+
RC4 algorithm = new RC4();
50+
string cipher = algorithm.Encrypt("aaaa", "test");
51+
Assert.IsTrue(cipher.Equals("ÏîFp"));
52+
}
53+
54+
[TestMethod]
55+
public void RC4TestNewDec()
56+
{
57+
RC4 algorithm = new RC4();
58+
string cipher = algorithm.Decrypt("ÏîFp", "test");
59+
Assert.IsTrue(cipher.Equals("aaaa"));
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)