-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHardcodedEncryptionKey.cs
More file actions
42 lines (37 loc) · 1.28 KB
/
HardcodedEncryptionKey.cs
File metadata and controls
42 lines (37 loc) · 1.28 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace CodeQL_Query_Writing_Demo
{
//TODO: Don't hardcode encryption keys
class HardcodedEncryptionKey
{
public void Case1()
{
string someHardcodedString = "thisisabadhardcodedkeybadbadbad";
byte[] badHardcodedKey = Encoding.ASCII.GetBytes(someHardcodedString);
AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider();
aesCryptoServiceProvider.Key = badHardcodedKey;
}
public byte[] Case2(byte[] iv, byte[] rawPlaintext, byte[] cipherText)
{
string someHardcodedString = "thisisabadhardcodedkeybadbadbad";
byte[] badHardcodedKey = Encoding.ASCII.GetBytes(someHardcodedString);
using (Aes aes = new AesCng())
{
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(badHardcodedKey, iv), CryptoStreamMode.Write))
{
cs.Write(rawPlaintext, 0, rawPlaintext.Length);
}
cipherText = ms.ToArray();
}
return cipherText;
}
}
}
}