-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
executable file
·73 lines (64 loc) · 3.11 KB
/
Program.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
using System;
using System.Text;
using NTRU.types;
using NTRU.Params;
using NTRU.rand;
namespace NTRU
{
public class Program
{
public static void Main(string[] args)
{
RandContext rand_ctx = rand.rand.init(rand.rand.RNG_DEFAULT);
Console.WriteLine("Successfully Created RandContext!");
KeyPair kp = NTRUWrapper.generate_key_pair(EncParamSets.DEFAULT_PARAMS_256_BITS, rand_ctx);
Console.WriteLine("Successfully Generated Keys!!");
byte[] msg = Encoding.UTF8.GetBytes("Hello from NTRU!");
byte[] encrypted = NTRUWrapper.encrypt(msg, kp.get_public(), EncParamSets.DEFAULT_PARAMS_256_BITS, rand_ctx);
byte[] decrypted = NTRUWrapper.decrypt(encrypted, kp, EncParamSets.DEFAULT_PARAMS_256_BITS);
Console.WriteLine(Encoding.UTF8.GetString(decrypted));
if (Encoding.UTF8.GetString(decrypted) == "Hello from NTRU!")
{
Console.WriteLine("Ecryption / Decryption Test Succeded!");
}
else
{
Console.WriteLine("Ecryption / Decryption Test Failed!");
}
byte[] exportedPriv = kp.get_private().export(EncParamSets.DEFAULT_PARAMS_256_BITS);
Console.WriteLine ("ENC PARAMS Private Key Length: " + EncParamSets.DEFAULT_PARAMS_256_BITS.private_len() + " Byte Array: " + exportedPriv.Length);
for (int i = 0; i < exportedPriv.Length; i++) {
Console.Write(exportedPriv[i]);
}
Console.Write("\n");
byte[] exportedPub = kp.get_public().export(EncParamSets.DEFAULT_PARAMS_256_BITS);
Console.WriteLine ("ENC PARAMS PublicKey Key Length: " + EncParamSets.DEFAULT_PARAMS_256_BITS.public_len() + " Byte Array: " + exportedPub.Length);
for (int i = 0; i < exportedPub.Length; i++) {
Console.Write(exportedPub[i]);
}
Console.Write("\n");
PublicKey pub = PublicKey.import(exportedPub);
PrivateKey priv = PrivateKey.import(exportedPriv);
KeyPair newKP = new KeyPair(priv, pub);
if(newKP == kp) {
Console.WriteLine("Importing / Exporting Key Test Succeded!");
}
else
{
Console.WriteLine("Importing / Exporting Key Test Failed!");
}
byte[] msg2 = Encoding.UTF8.GetBytes("Hello from NTRU! 2");
byte[] encrypted2 = NTRUWrapper.encrypt(msg2, newKP.get_public(), EncParamSets.DEFAULT_PARAMS_256_BITS, rand_ctx);
byte[] decrypted2 = NTRUWrapper.decrypt(encrypted2, newKP, EncParamSets.DEFAULT_PARAMS_256_BITS);
Console.WriteLine(Encoding.UTF8.GetString(decrypted2));
if (Encoding.UTF8.GetString(decrypted2) == "Hello from NTRU! 2")
{
Console.WriteLine("Ecryption / Decryption Test 2 Succeded!");
}
else
{
Console.WriteLine("Ecryption / Decryption Test 2 Failed!");
}
}
}
}