Skip to content

Commit 52a9fb0

Browse files
committed
C#: Add test for decrypt.
1 parent 4101676 commit 52a9fb0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

csharp/ql/test/query-tests/Security Features/CWE-321/HardcodedSymmetricEncryptionKey/HardcodedSymmetricEncryptionKey.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,36 @@ static void Main(string[] args)
4646
// GOOD (this function hashes password)
4747
var de = DecryptWithPassword(ct, c, iv);
4848

49+
// BAD: harc-coded password passed to Decrypt
50+
var de1 = Decrypt(ct, c, iv);
51+
4952
// BAD [NOT DETECTED]
5053
CreateCryptographicKey(null, byteArrayFromString);
5154

5255
// GOOD
5356
CreateCryptographicKey(null, File.ReadAllBytes("secret.key"));
5457
}
5558

59+
public static string Decrypt(byte[] cipherText, byte[] password, byte[] IV)
60+
{
61+
byte[] rawPlaintext;
62+
var salt = new byte[] { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 };
63+
64+
using (Aes aes = new AesManaged())
65+
{
66+
using (MemoryStream ms = new MemoryStream())
67+
{
68+
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(password, IV), CryptoStreamMode.Write))
69+
{
70+
cs.Write(cipherText, 0, cipherText.Length);
71+
}
72+
rawPlaintext = ms.ToArray();
73+
}
74+
75+
return Encoding.Unicode.GetString(rawPlaintext);
76+
}
77+
}
78+
5679
public static string DecryptWithPassword(byte[] cipherText, byte[] password, byte[] IV)
5780
{
5881
byte[] rawPlaintext;

0 commit comments

Comments
 (0)