-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImplementationClass.cs
150 lines (123 loc) · 4.68 KB
/
ImplementationClass.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
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Security.Cryptography;
namespace P1ElGamal
{
public class ImplementationClass : ImpExpElGamalParameters
{
public struct ElGamalKeyStruct
{
public BigInteger P;
public BigInteger G;
public BigInteger Y;
public BigInteger X;
}
//current key in use
private ElGamalKeyStruct current_key;
public ImplementationClass()
{
current_key = new ElGamalKeyStruct();
current_key.P = new BigInteger(0);
current_key.G = new BigInteger(0);
current_key.Y = new BigInteger(0);
current_key.X = new BigInteger(0);
// default key size
KeySizeValue = 384;
//range of keys
LegalKeySizesValue = new[] { new KeySizes(384, 1088, 8) };
}
private void CreateKeyPair(int key_strength)
{
// create the random number
Random random_number = new Random();
current_key.X = new BigInteger();
current_key.G = new BigInteger();
// create the large prime number, P bits/confidence/random
current_key.P = BigInteger.genPseudoPrime(key_strength, 16, random_number);
// create the two random numbers, which are smaller than P
current_key.X.genRandomBits(key_strength - 1, random_number);
current_key.G.genRandomBits(key_strength - 1, random_number);
// compute Y modPow(exp, modulo) Y = GexpX modP
current_key.Y = current_key.G.modPow(current_key.X, current_key.P);
}
//Checking if user has not typed any input
private bool NeedToGenerateKey()
{
return current_key.P == 0 && current_key.G == 0 && current_key.Y == 0;
}
//If not create this key pair
public ElGamalKeyStruct KeyStruct
{
get
{
if (NeedToGenerateKey())
{
CreateKeyPair(KeySizeValue);
}
return current_key;
}
set
{
current_key = value;
}
}
public override void ImportParameters(ElGamalParameters p_parameters)
{
current_key.P = new BigInteger(p_parameters.P);
current_key.G = new BigInteger(p_parameters.G);
current_key.Y = new BigInteger(p_parameters.Y);
if (p_parameters.X != null && p_parameters.X.Length > 0)
{
current_key.X = new BigInteger(p_parameters.X);
}
// set the length of the key based on the import
KeySizeValue = current_key.P.bitCount();
}
public override ElGamalParameters ExportParameters(bool include_private_params)
{
//If there is nothing to export create first a keypair
if (NeedToGenerateKey())
{
CreateKeyPair(KeySizeValue);
}
ElGamalParameters elgamal_params = new ElGamalParameters();
// set the public values
elgamal_params.P = current_key.P.getBytes();
elgamal_params.G = current_key.G.getBytes();
elgamal_params.Y = current_key.Y.getBytes();
//include the private value, X
if (include_private_params)
{
elgamal_params.X = current_key.X.getBytes();
}
else
{
// ensure that we zero the value
elgamal_params.X = new byte[1];
}
// return the parameter set
return elgamal_params;
}
public override byte[] EncryptData(byte[] data)
{
if (NeedToGenerateKey())
{
// we need to create a new key before we can export
CreateKeyPair(KeySizeValue);
}
// encrypt the data
ElGamalEncrypt encrypt_data = new ElGamalEncrypt(current_key);
return encrypt_data.ProcessData(data);
}
public override byte[] DecryptData(byte[] p_data)
{
if (NeedToGenerateKey())
{
// we need to create a new key before we can export
CreateKeyPair(KeySizeValue);
}
// decrypt the data
ElGamalDecrypt decrypt_data = new ElGamalDecrypt(current_key);
return decrypt_data.ProcessData(p_data);
}
}
}