Skip to content

Commit e643cc4

Browse files
committed
Updated test suite with AesGcm and AesCcm tests
1 parent 45ae4d1 commit e643cc4

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 AesGcmExample
9+
{
10+
public static void RunExample()
11+
{
12+
const string originalMessage = "This is a secret message!";
13+
14+
byte[] key = GenerateRandomKey();
15+
byte[] nonce = GenerateRandomNonce();
16+
17+
var (encryptedMessage, tag) = EncryptStringWithGcm(originalMessage, key, nonce);
18+
string decryptedMessage = DecryptStringWithGcm(encryptedMessage, key, nonce, tag);
19+
20+
bool isSuccessful = originalMessage == decryptedMessage;
21+
Console.WriteLine("Decryption successful: {0}", isSuccessful);
22+
}
23+
24+
private static (byte[], byte[]) EncryptStringWithGcm(string plaintext, byte[] key, byte[] nonce)
25+
{
26+
using (var aes = new AesGcm(key, AesGcm.TagByteSizes.MaxSize))
27+
{
28+
var plaintextBytes = Encoding.UTF8.GetBytes(plaintext);
29+
var ciphertext = new byte[plaintextBytes.Length];
30+
var tag = new byte[AesGcm.TagByteSizes.MaxSize];
31+
aes.Encrypt(nonce, plaintextBytes, ciphertext, tag);
32+
33+
return (ciphertext, tag);
34+
}
35+
}
36+
37+
private static string DecryptStringWithGcm(byte[] ciphertext, byte[] key, byte[] nonce, byte[] tag)
38+
{
39+
using (var aes = new AesGcm(key, AesGcm.TagByteSizes.MaxSize))
40+
{
41+
var plaintextBytes = new byte[ciphertext.Length];
42+
aes.Decrypt(nonce, ciphertext, tag, plaintextBytes);
43+
44+
return Encoding.UTF8.GetString(plaintextBytes);
45+
}
46+
}
47+
48+
private static byte[] GenerateRandomKey()
49+
{
50+
byte[] key = new byte[32];
51+
using (var rng = RandomNumberGenerator.Create())
52+
{
53+
rng.GetBytes(key);
54+
}
55+
return key;
56+
}
57+
58+
private static byte[] GenerateRandomNonce()
59+
{
60+
byte[] nonce = new byte[AesGcm.NonceByteSizes.MaxSize];
61+
using (var rng = RandomNumberGenerator.Create())
62+
{
63+
rng.GetBytes(nonce);
64+
}
65+
return nonce;
66+
}
67+
}
68+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| AesCfbExample.cs:68:53:68:113 | DecryptOperation | AesCfbExample.cs:66:66:66:75 | Message | AesCfbExample.cs:47:33:47:51 | KeyOperationOutput |
2+
| AesGcmExample.cs:42:17:42:67 | DecryptOperation | AesGcmExample.cs:42:36:42:45 | Message | AesGcmExample.cs:31:52:31:61 | KeyOperationOutput |
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, Crypto::MessageArtifactNode m
5+
where n.getAnInputArtifact() = m
6+
select n, m, m.getSourceNode()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
| AesCfbExample.cs:42:53:42:114 | EncryptOperation | AesCfbExample.cs:31:17:31:23 | Key | AesCfbExample.cs:87:30:87:32 | RandomNumberGeneration |
22
| AesCfbExample.cs:68:53:68:113 | DecryptOperation | AesCfbExample.cs:63:66:63:68 | Key | AesCfbExample.cs:87:30:87:32 | RandomNumberGeneration |
3+
| AesGcmExample.cs:31:17:31:67 | EncryptOperation | AesGcmExample.cs:26:41:26:43 | Key | AesGcmExample.cs:53:30:53:32 | RandomNumberGeneration |
4+
| AesGcmExample.cs:42:17:42:67 | DecryptOperation | AesGcmExample.cs:39:41:39:43 | Key | AesGcmExample.cs:53:30:53:32 | RandomNumberGeneration |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
| AesCfbExample.cs:42:53:42:114 | EncryptOperation | AesCfbExample.cs:32:17:32:22 | Nonce | AesCfbExample.cs:97:30:97:31 | RandomNumberGeneration |
22
| AesCfbExample.cs:68:53:68:113 | DecryptOperation | AesCfbExample.cs:63:71:63:72 | Nonce | AesCfbExample.cs:97:30:97:31 | RandomNumberGeneration |
3+
| AesGcmExample.cs:31:17:31:67 | EncryptOperation | AesGcmExample.cs:31:29:31:33 | Nonce | AesGcmExample.cs:63:30:63:34 | RandomNumberGeneration |
4+
| AesGcmExample.cs:42:17:42:67 | DecryptOperation | AesGcmExample.cs:42:29:42:33 | Nonce | AesGcmExample.cs:63:30:63:34 | RandomNumberGeneration |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
| 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 |
22
| 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 |
3+
| AesGcmExample.cs:31:17:31:67 | EncryptOperation | AesGcmExample.cs:31:36:31:49 | Message | AesGcmExample.cs:31:52:31:61 | KeyOperationOutput | AesGcmExample.cs:26:41:26:43 | Key | AesGcmExample.cs:31:29:31:33 | Nonce | AesGcmExample.cs:31:17:31:67 | KeyOperationAlgorithm | Encrypt |
4+
| AesGcmExample.cs:42:17:42:67 | DecryptOperation | AesGcmExample.cs:42:36:42:45 | Message | AesGcmExample.cs:42:53:42:66 | KeyOperationOutput | AesGcmExample.cs:39:41:39:43 | Key | AesGcmExample.cs:42:29:42:33 | Nonce | AesGcmExample.cs:42:17:42:67 | KeyOperationAlgorithm | Decrypt |

0 commit comments

Comments
 (0)