Skip to content

Commit 46c037c

Browse files
committed
Added unit tests for C# block cipher modes
1 parent 9238b12 commit 46c037c

File tree

8 files changed

+128
-0
lines changed

8 files changed

+128
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.IO;
3+
using System.Security.Cryptography;
4+
using System.Text;
5+
6+
namespace QuantumExamples.Cryptography
7+
{
8+
public class AesCfbExample
9+
{
10+
public static void RunExample()
11+
{
12+
const string originalMessage = "This is a secret message!";
13+
14+
byte[] key = GenerateRandomKey();
15+
byte[] iv = GenerateRandomIV();
16+
17+
byte[] encryptedData = EncryptStringWithCfb(originalMessage, key, iv);
18+
string decryptedMessage = DecryptStringWithCfb(encryptedData, key, iv);
19+
20+
bool isSuccessful = originalMessage == decryptedMessage;
21+
Console.WriteLine("Decryption successful: {0}", isSuccessful);
22+
}
23+
24+
private static byte[] EncryptStringWithCfb(string plainText, byte[] key, byte[] iv)
25+
{
26+
byte[] encrypted;
27+
28+
using (Aes aes = Aes.Create())
29+
{
30+
// Set the key and IV on the AES instance.
31+
aes.Key = key;
32+
aes.IV = iv;
33+
aes.Mode = CipherMode.CFB;
34+
aes.Padding = PaddingMode.None;
35+
36+
ICryptoTransform encryptor = aes.CreateEncryptor();
37+
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
38+
39+
// Create an empty memory stream and write the plaintext to the crypto stream.
40+
using (MemoryStream msEncrypt = new MemoryStream())
41+
{
42+
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
43+
{
44+
csEncrypt.Write(plainBytes, 0, plainBytes.Length);
45+
csEncrypt.FlushFinalBlock();
46+
}
47+
encrypted = msEncrypt.ToArray();
48+
}
49+
}
50+
return encrypted;
51+
}
52+
53+
private static string DecryptStringWithCfb(byte[] cipherText, byte[] key, byte[] iv)
54+
{
55+
string decrypted;
56+
57+
using (Aes aes = Aes.Create())
58+
{
59+
aes.Mode = CipherMode.CFB;
60+
aes.Padding = PaddingMode.None;
61+
62+
// Pass the key and IV to the decryptor directly.
63+
ICryptoTransform decryptor = aes.CreateDecryptor(key, iv);
64+
65+
// Pass the ciphertext to the memory stream directly.
66+
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
67+
{
68+
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
69+
{
70+
using (MemoryStream msPlain = new MemoryStream())
71+
{
72+
csDecrypt.CopyTo(msPlain);
73+
byte[] plainBytes = msPlain.ToArray();
74+
decrypted = Encoding.UTF8.GetString(plainBytes);
75+
}
76+
}
77+
}
78+
}
79+
return decrypted;
80+
}
81+
82+
private static byte[] GenerateRandomKey()
83+
{
84+
byte[] key = new byte[32]; // 256-bit key
85+
using (var rng = RandomNumberGenerator.Create())
86+
{
87+
rng.GetBytes(key);
88+
}
89+
return key;
90+
}
91+
92+
private static byte[] GenerateRandomIV()
93+
{
94+
byte[] iv = new byte[16]; // 128-bit IV
95+
using (var rng = RandomNumberGenerator.Create())
96+
{
97+
rng.GetBytes(iv);
98+
}
99+
return iv;
100+
}
101+
}
102+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| AesCfbExample.cs:42:53:42:114 | EncryptOperation | AesCfbExample.cs:31:17:31:23 | Key | AesCfbExample.cs:87:30:87:32 | RandomNumberGeneration |
2+
| AesCfbExample.cs:68:53:68:113 | DecryptOperation | AesCfbExample.cs:63:66:63:68 | Key | AesCfbExample.cs:87:30:87:32 | RandomNumberGeneration |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import csharp
2+
import experimental.quantum.Language
3+
4+
from Crypto::CipherOperationNode op, Crypto::KeyArtifactNode k
5+
where op.getAKey() = k
6+
select op, k, k.getSourceNode()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| AesCfbExample.cs:42:53:42:114 | EncryptOperation | AesCfbExample.cs:32:17:32:22 | Nonce | AesCfbExample.cs:97:30:97:31 | RandomNumberGeneration |
2+
| AesCfbExample.cs:68:53:68:113 | DecryptOperation | AesCfbExample.cs:63:71:63:72 | Nonce | AesCfbExample.cs:97:30:97:31 | RandomNumberGeneration |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import csharp
2+
import experimental.quantum.Language
3+
4+
from Crypto::CipherOperationNode op, Crypto::NonceArtifactNode n
5+
where op.getANonce() = n
6+
select op, n, n.getSourceNode()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| AesCfbExample.cs:42:53:42:114 | EncryptOperation | AesCfbExample.cs:44:41:44:50 | Message | AesCfbExample.cs:47:33:47:51 | KeyOperationOutput | AesCfbExample.cs:31:17:31:23 | Key | AesCfbExample.cs:32:17:32:22 | Nonce | AesCfbExample.cs:28:30:28:41 | KeyOperationAlgorithm | Encrypt |
2+
| AesCfbExample.cs:68:53:68:113 | DecryptOperation | AesCfbExample.cs:66:66:66:75 | Message | AesCfbExample.cs:73:49:73:65 | KeyOperationOutput | AesCfbExample.cs:63:66:63:68 | Key | AesCfbExample.cs:63:71:63:72 | Nonce | AesCfbExample.cs:57:30:57:41 | KeyOperationAlgorithm | Decrypt |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import csharp
2+
import experimental.quantum.Language
3+
4+
from Crypto::CipherOperationNode n
5+
select n, n.getAnInputArtifact(), n.getAnOutputArtifact(), n.getAKey(), n.getANonce(),
6+
n.getAnAlgorithmOrGenericSource(), n.getKeyOperationSubtype()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
semmle-extractor-options: /nostdlib /noconfig
2+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj

0 commit comments

Comments
 (0)