Skip to content

Commit 1525a0e

Browse files
committed
Encrypt sample message, slice cyphertext to blocks and display them in base64
1 parent 8dd6f7c commit 1525a0e

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

Program.cs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Security.Cryptography;
45

@@ -8,65 +9,57 @@ class PaddingOracleAttack
89
{
910
public static void Main()
1011
{
11-
string original = "Here is some data to encrypt!";
12+
string hiddenMessage = "I'd just like to interject for a moment. What you’re referring to as Linux, is in fact, GNU/Linux, or as I’ve recently taken to calling it, GNU plus Linux.";
1213

13-
// Create a new instance of the Aes
14-
// class. This generates a new key and initialization
15-
// vector (IV).
16-
using (Aes myAes = Aes.Create())
14+
using (Aes aes = Aes.Create())
1715
{
16+
byte[] encrypted = EncryptStringToBytes_Aes(hiddenMessage);
17+
var blocks = sliceBytesIntoBlocks(encrypted);
1818

19-
// Encrypt the string to an array of bytes.
20-
byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
19+
Console.WriteLine("Plaintext:\n{0}", hiddenMessage);
20+
Console.WriteLine("\nCiphertext:\n{0}", String.Join("\n", blocks.ConvertAll(block => Convert.ToBase64String(block))));
21+
Console.WriteLine("\nAttack results:\nTODO");
22+
}
23+
}
2124

22-
// Decrypt the bytes to a string.
23-
string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
25+
static List<byte[]> sliceBytesIntoBlocks(byte[] bytes, int blockSizeBytes = 16)
26+
{
27+
var blocks = new List<byte[]>();
2428

25-
//Display the original data and the decrypted data.
26-
Console.WriteLine("Original: {0}", original);
27-
Console.WriteLine("Round Trip: {0}", roundtrip);
29+
for (var i = 0; i < bytes.Length; i += blockSizeBytes)
30+
{
31+
byte[] block = new byte[blockSizeBytes];
32+
Array.Copy(bytes, i, block, 0, blockSizeBytes);
33+
blocks.Add(block);
2834
}
35+
36+
return blocks;
2937
}
30-
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
38+
static byte[] EncryptStringToBytes_Aes(string plainText)
3139
{
32-
// Check arguments.
33-
if (plainText == null || plainText.Length <= 0)
34-
throw new ArgumentNullException("plainText");
35-
if (Key == null || Key.Length <= 0)
36-
throw new ArgumentNullException("Key");
37-
if (IV == null || IV.Length <= 0)
38-
throw new ArgumentNullException("IV");
3940
byte[] encrypted;
4041

41-
// Create an Aes object
42-
// with the specified key and IV.
4342
using (Aes aesAlg = Aes.Create())
4443
{
45-
aesAlg.Key = Key;
46-
aesAlg.IV = IV;
44+
aesAlg.Mode = CipherMode.CBC;
45+
aesAlg.Padding = PaddingMode.PKCS7;
4746

48-
// Create an encryptor to perform the stream transform.
4947
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
5048

51-
// Create the streams used for encryption.
5249
using (MemoryStream msEncrypt = new MemoryStream())
5350
{
5451
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
5552
{
5653
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
5754
{
58-
//Write all data to the stream.
5955
swEncrypt.Write(plainText);
6056
}
6157
encrypted = msEncrypt.ToArray();
6258
}
6359
}
6460
}
6561

66-
67-
// Return the encrypted bytes from the memory stream.
6862
return encrypted;
69-
7063
}
7164

7265
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)

0 commit comments

Comments
 (0)