forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsufficientKeySize.cs
More file actions
279 lines (227 loc) · 9.11 KB
/
InsufficientKeySize.cs
File metadata and controls
279 lines (227 loc) · 9.11 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.IO;
using System.Security.Cryptography;
namespace InsufficientKeySize
{
class MainCrypto
{
static public byte[] EncryptWithRSA(byte[] plaintext, RSAParameters key)
{
try
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512); // BAD
rsa.ImportParameters(key);
return rsa.Encrypt(plaintext, true);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
static public byte[] EncryptWithRSA2(byte[] plaintext, RSAParameters key)
{
try
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); // BAD
rsa = new RSACryptoServiceProvider(1024); // GOOD
rsa.ImportParameters(key);
return rsa.Encrypt(plaintext, true);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
static public byte[] EncryptWithDSA(byte[] plaintext, DSAParameters key)
{
try
{
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(2048); // GOOD
dsa.ImportParameters(key);
return dsa.SignData(plaintext);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
static public byte[] EncryptWithDSA2(byte[] plaintext, DSAParameters key)
{
try
{
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(); // BAD
dsa = new DSACryptoServiceProvider(1024); // GOOD
dsa.ImportParameters(key);
return dsa.SignData(plaintext);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
public static void Main()
{
try
{
DSAParameters privateKeyInfo;
DSAParameters publicKeyInfo;
// Create a new instance of DSACryptoServiceProvider to generate
// a new key pair.
using (DSACryptoServiceProvider DSA = new DSACryptoServiceProvider())
{
privateKeyInfo = DSA.ExportParameters(true);
publicKeyInfo = DSA.ExportParameters(false);
}
// The hash value to sign.
byte[] HashValue =
{
59, 4, 248, 102, 77, 97, 142, 201,
210, 12, 224, 93, 25, 41, 100, 197,
213, 134, 130, 135
};
// The value to hold the signed value.
byte[] SignedHashValue = DSASignHash(HashValue, privateKeyInfo, "SHA1");
// Verify the hash and display the results.
bool verified = DSAVerifyHash(HashValue, SignedHashValue, publicKeyInfo, "SHA1");
if (verified)
{
Console.WriteLine("The hash value was verified.");
}
else
{
Console.WriteLine("The hash value was not verified.");
}
}
catch (ArgumentNullException e)
{
Console.WriteLine(e.Message);
}
}
public static byte[] DSASignHash(byte[] HashToSign, DSAParameters DSAKeyInfo,
string HashAlg)
{
byte[] sig = null;
try
{
// Create a new instance of DSACryptoServiceProvider.
using (DSACryptoServiceProvider DSA = new DSACryptoServiceProvider(1024)) // GOOD
{
// Import the key information.
DSA.ImportParameters(DSAKeyInfo);
// Create an DSASignatureFormatter object and pass it the
// DSACryptoServiceProvider to transfer the private key.
DSASignatureFormatter DSAFormatter = new DSASignatureFormatter(DSA);
// Set the hash algorithm to the passed value.
DSAFormatter.SetHashAlgorithm(HashAlg);
// Create a signature for HashValue and return it.
sig = DSAFormatter.CreateSignature(HashToSign);
}
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
return sig;
}
public static bool DSAVerifyHash(byte[] HashValue, byte[] SignedHashValue,
DSAParameters DSAKeyInfo, string HashAlg)
{
bool verified = false;
try
{
// Create a new instance of DSACryptoServiceProvider.
using (DSACryptoServiceProvider DSA = new DSACryptoServiceProvider()) // BAD
{
// Import the key information.
DSA.ImportParameters(DSAKeyInfo);
// Create an DSASignatureDeformatter object and pass it the
// DSACryptoServiceProvider to transfer the private key.
DSASignatureDeformatter DSADeformatter = new DSASignatureDeformatter(DSA);
// Set the hash algorithm to the passed value.
DSADeformatter.SetHashAlgorithm(HashAlg);
// Verify signature and return the result.
verified = DSADeformatter.VerifySignature(HashValue, SignedHashValue);
}
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
return verified;
}
public static void Main2()
{
// Create a new DES key.
DESCryptoServiceProvider key = new DESCryptoServiceProvider(); // BAD
// Encrypt a string to a byte array.
byte[] buffer = Encrypt("This is some plaintext!", key);
// Decrypt the byte array back to a string.
string plaintext = Decrypt(buffer, key);
// Display the plaintext value to the console.
Console.WriteLine(plaintext);
}
// Encrypt the string.
public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream();
// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);
// Create a StreamWriter to write a string
// to the stream.
StreamWriter sw = new StreamWriter(encStream);
// Write the plaintext to the stream.
sw.WriteLine(PlainText);
// Close the StreamWriter and CryptoStream.
sw.Close();
encStream.Close();
// Get an array of bytes that represents
// the memory stream.
byte[] buffer = ms.ToArray();
// Close the memory stream.
ms.Close();
// Return the encrypted byte array.
return buffer;
}
// Decrypt the byte array.
public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
{
// Create a memory stream to the passed buffer.
MemoryStream ms = new MemoryStream(CypherText);
// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);
// Create a StreamReader for reading the stream.
StreamReader sr = new StreamReader(encStream);
// Read the stream as a string.
string val = sr.ReadLine();
// Close the streams.
sr.Close();
encStream.Close();
ms.Close();
return val;
}
const int KEY_SIZE = 64;
public static void RC2()
{
// Create a new instance of the RC2CryptoServiceProvider class
// and automatically generate a Key and IV.
RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
rc2CSP.EffectiveKeySize = KEY_SIZE; // BAD
Console.WriteLine("Effective key size is {0} bits.", rc2CSP.EffectiveKeySize);
// Get the key and IV.
byte[] key = rc2CSP.Key;
byte[] IV = rc2CSP.IV;
// Get an encryptor.
ICryptoTransform encryptor = rc2CSP.CreateEncryptor(key, IV);
// Encrypt the data as an array of encrypted bytes in memory.
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
}
}
}