forked from openssl-net/openssl-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCipher.cs
140 lines (121 loc) · 3.56 KB
/
TestCipher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using NUnit.Framework;
using OpenSSL.Crypto;
using System.Text;
using System.Reflection;
using OpenSSL.Core;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
namespace UnitTests
{
[TestFixture]
public class TestCipher
{
static CryptoKey[] Keys;
static TestCipher()
{
const int numKeys = 10;
Keys = new CryptoKey[numKeys];
for (int i = 0; i < numKeys; i++)
{
using (var rsa = new RSA())
{
rsa.GenerateKeys(1024, BigNumber.One, null, null);
Keys[i] = new CryptoKey(rsa);
}
}
}
public class WithNullFactory : IEnumerable
{
public IEnumerator GetEnumerator()
{
var fields = typeof(Cipher).GetFields(BindingFlags.Public | BindingFlags.Static);
return fields
.Where(x => x.Name != "DES_EDE3_CFB1")
.Select(x => new TestCaseData(x.GetValue(null)).SetName(x.Name))
.GetEnumerator();
}
}
public class Factory : IEnumerable
{
public IEnumerator GetEnumerator()
{
var fields = typeof(Cipher).GetFields(BindingFlags.Public | BindingFlags.Static);
return fields
.Where(x => x.Name != "Null")
.Where(x => x.Name != "DES_EDE3_CFB1")
.Select(x => new TestCaseData(x.GetValue(null)).SetName(x.Name))
.GetEnumerator();
}
}
[Test]
[TestCaseSource(typeof(WithNullFactory))]
public void TestEncryptDecrypt(Cipher cipher)
{
var inputMsg = "This is a message";
var input = Encoding.ASCII.GetBytes(inputMsg);
var iv = Encoding.ASCII.GetBytes("12345678");
var key = Encoding.ASCII.GetBytes("This is the key");
Console.Write("Using cipher {0}: ", cipher.LongName);
using (var cc = new CipherContext(cipher))
{
Console.Write(" KeyLength: {0}, IVLength: {1}, BlockSize: {2}, Stream: {3} ",
cipher.KeyLength, cipher.IVLength, cipher.BlockSize, cc.IsStream);
var pt = cc.Encrypt(input, key, iv);
if (cipher == Cipher.Null)
Assert.AreEqual(input, pt);
else
Assert.AreNotEqual(input, pt);
var ct = cc.Decrypt(pt, key, iv);
var msg = Encoding.ASCII.GetString(ct);
Console.WriteLine("\"{0}\"", msg);
Assert.AreEqual(inputMsg, msg);
}
}
[Test]
[TestCaseSource(typeof(Factory))]
public void TestEncryptDecryptWithSalt(Cipher cipher)
{
if (cipher == Cipher.Null)
Assert.Ignore();
var inputMsg = "This is a message";
var input = Encoding.ASCII.GetBytes(inputMsg);
var salt = Encoding.ASCII.GetBytes("salt");
var secret = Encoding.ASCII.GetBytes("Password!");
Console.Write("Using cipher {0}: ", cipher.LongName);
using (var cc = new CipherContext(cipher))
{
Console.Write(" KeyLength: {0}, IVLength: {1}, BlockSize: {2}, Stream: {3} ",
cipher.KeyLength, cipher.IVLength, cipher.BlockSize, cc.IsStream);
byte[] iv;
var key = cc.BytesToKey(MessageDigest.SHA1, salt, secret, 1, out iv);
var pt = cc.Encrypt(input, key, iv);
Assert.AreNotEqual(input, pt);
var ct = cc.Decrypt(pt, key, iv);
var msg = Encoding.ASCII.GetString(ct);
Console.WriteLine("\"{0}\"", msg);
Assert.AreEqual(inputMsg, msg);
}
}
[Test]
[TestCaseSource(typeof(Factory))]
public void TestSealOpen(Cipher cipher)
{
if (cipher == Cipher.Null)
Assert.Ignore();
var inputMsg = "This is a message";
var input = Encoding.ASCII.GetBytes(inputMsg);
using (var cc = new CipherContext(cipher))
{
var env = cc.Seal(Keys, input);
Assert.AreNotEqual(input, env.Data);
for (int i = 0; i < Keys.Length; i++)
{
var result = cc.Open(env.Data, env.Keys[i], env.IV, Keys[i]);
Assert.AreEqual(input, result);
}
}
}
}
}