mirror of
https://github.com/github/codeql.git
synced 2026-04-29 02:35:15 +02:00
C#: Add test for decrypt.
This commit is contained in:
@@ -46,6 +46,9 @@ namespace HardcodedSymmetricEncryptionKey
|
||||
// GOOD (this function hashes password)
|
||||
var de = DecryptWithPassword(ct, c, iv);
|
||||
|
||||
// BAD: harc-coded password passed to Decrypt
|
||||
var de1 = Decrypt(ct, c, iv);
|
||||
|
||||
// BAD [NOT DETECTED]
|
||||
CreateCryptographicKey(null, byteArrayFromString);
|
||||
|
||||
@@ -53,6 +56,26 @@ namespace HardcodedSymmetricEncryptionKey
|
||||
CreateCryptographicKey(null, File.ReadAllBytes("secret.key"));
|
||||
}
|
||||
|
||||
public static string Decrypt(byte[] cipherText, byte[] password, byte[] IV)
|
||||
{
|
||||
byte[] rawPlaintext;
|
||||
var salt = new byte[] { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 };
|
||||
|
||||
using (Aes aes = new AesManaged())
|
||||
{
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(password, IV), CryptoStreamMode.Write))
|
||||
{
|
||||
cs.Write(cipherText, 0, cipherText.Length);
|
||||
}
|
||||
rawPlaintext = ms.ToArray();
|
||||
}
|
||||
|
||||
return Encoding.Unicode.GetString(rawPlaintext);
|
||||
}
|
||||
}
|
||||
|
||||
public static string DecryptWithPassword(byte[] cipherText, byte[] password, byte[] IV)
|
||||
{
|
||||
byte[] rawPlaintext;
|
||||
|
||||
Reference in New Issue
Block a user