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
|
//-----------------------------------------------------------------------
// <copyright file="DiffieHellmanTests.cs" company="Jason Alexander, Andrew Arnott">
// Copyright (c) Jason Alexander, Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test.OpenId {
using System;
using System.IO;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Messages;
using NUnit.Framework;
using Org.Mentalis.Security.Cryptography;
[TestFixture]
public class DiffieHellmanTests : OpenIdTestBase {
[TestCase]
public void Test() {
string s1 = Test1();
string s2 = Test1();
Assert.AreNotEqual(s1, s2, "Secret keys should NOT be the same.");
}
[TestCase, Timeout(15000), Category("Slow"), Category("Performance")]
public void TestPublic() {
TextReader reader = new StringReader(OpenIdTestBase.LoadEmbeddedFile("dhpriv.txt"));
try {
string line;
int lineNumber = 0;
while ((line = reader.ReadLine()) != null) {
TestUtilities.TestLogger.InfoFormat("\tLine {0}", ++lineNumber);
string[] parts = line.Trim().Split(' ');
byte[] x = Convert.FromBase64String(parts[0]);
DiffieHellmanManaged dh = new DiffieHellmanManaged(AssociateDiffieHellmanRequest.DefaultMod, AssociateDiffieHellmanRequest.DefaultGen, x);
byte[] pub = dh.CreateKeyExchange();
byte[] y = Convert.FromBase64String(parts[1]);
if (y[0] == 0 && y[1] <= 127) {
y.CopyTo(y, 1);
}
Assert.AreEqual(
Convert.ToBase64String(y),
Convert.ToBase64String(DiffieHellmanUtilities.EnsurePositive(pub)),
line);
}
} finally {
reader.Close();
}
}
private static string Test1() {
DiffieHellman dh1 = new DiffieHellmanManaged();
DiffieHellman dh2 = new DiffieHellmanManaged();
string secret1 = Convert.ToBase64String(dh1.DecryptKeyExchange(dh2.CreateKeyExchange()));
string secret2 = Convert.ToBase64String(dh2.DecryptKeyExchange(dh1.CreateKeyExchange()));
Assert.AreEqual(secret1, secret2, "Secret keys do not match for some reason.");
return secret1;
}
}
}
|