Skip to content

Commit e072594

Browse files
committed
Create RemoteServerMock (oracle), refactor
1 parent 1525a0e commit e072594

File tree

2 files changed

+70
-71
lines changed

2 files changed

+70
-71
lines changed

Program.cs

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ namespace Padding_Oracle_Attack
77
{
88
class PaddingOracleAttack
99
{
10+
private static RemoteServerMock server = new RemoteServerMock();
1011
public static void Main()
1112
{
1213
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.";
1314

1415
using (Aes aes = Aes.Create())
1516
{
16-
byte[] encrypted = EncryptStringToBytes_Aes(hiddenMessage);
17+
byte[] encrypted = server.Encrypt(hiddenMessage);
1718
var blocks = sliceBytesIntoBlocks(encrypted);
1819

1920
Console.WriteLine("Plaintext:\n{0}", hiddenMessage);
2021
Console.WriteLine("\nCiphertext:\n{0}", String.Join("\n", blocks.ConvertAll(block => Convert.ToBase64String(block))));
2122
Console.WriteLine("\nAttack results:\nTODO");
23+
24+
encrypted[encrypted.Length - 1] = 22;
25+
26+
Console.WriteLine("\nPadding is {0}", server.IsPaddingCorrect(encrypted) ? "correct" : "incorrect");
2227
}
2328
}
2429

@@ -35,75 +40,5 @@ static List<byte[]> sliceBytesIntoBlocks(byte[] bytes, int blockSizeBytes = 16)
3540

3641
return blocks;
3742
}
38-
static byte[] EncryptStringToBytes_Aes(string plainText)
39-
{
40-
byte[] encrypted;
41-
42-
using (Aes aesAlg = Aes.Create())
43-
{
44-
aesAlg.Mode = CipherMode.CBC;
45-
aesAlg.Padding = PaddingMode.PKCS7;
46-
47-
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
48-
49-
using (MemoryStream msEncrypt = new MemoryStream())
50-
{
51-
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
52-
{
53-
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
54-
{
55-
swEncrypt.Write(plainText);
56-
}
57-
encrypted = msEncrypt.ToArray();
58-
}
59-
}
60-
}
61-
62-
return encrypted;
63-
}
64-
65-
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
66-
{
67-
// Check arguments.
68-
if (cipherText == null || cipherText.Length <= 0)
69-
throw new ArgumentNullException("cipherText");
70-
if (Key == null || Key.Length <= 0)
71-
throw new ArgumentNullException("Key");
72-
if (IV == null || IV.Length <= 0)
73-
throw new ArgumentNullException("IV");
74-
75-
// Declare the string used to hold
76-
// the decrypted text.
77-
string plaintext = null;
78-
79-
// Create an Aes object
80-
// with the specified key and IV.
81-
using (Aes aesAlg = Aes.Create())
82-
{
83-
aesAlg.Key = Key;
84-
aesAlg.IV = IV;
85-
86-
// Create a decryptor to perform the stream transform.
87-
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
88-
89-
// Create the streams used for decryption.
90-
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
91-
{
92-
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
93-
{
94-
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
95-
{
96-
97-
// Read the decrypted bytes from the decrypting stream
98-
// and place them in a string.
99-
plaintext = srDecrypt.ReadToEnd();
100-
}
101-
}
102-
}
103-
104-
}
105-
106-
return plaintext;
107-
}
10843
}
10944
}

RemoteServerMock.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.IO;
2+
using System.Security.Cryptography;
3+
4+
namespace Padding_Oracle_Attack
5+
{
6+
class RemoteServerMock
7+
{
8+
private Aes aesAlg = Aes.Create();
9+
10+
public RemoteServerMock()
11+
{
12+
aesAlg.BlockSize = 128;
13+
aesAlg.Mode = CipherMode.CBC;
14+
aesAlg.Padding = PaddingMode.PKCS7;
15+
}
16+
17+
public byte[] Encrypt(string plaintext)
18+
{
19+
byte[] encrypted;
20+
21+
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
22+
23+
using (MemoryStream msEncrypt = new MemoryStream())
24+
{
25+
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
26+
{
27+
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
28+
{
29+
swEncrypt.Write(plaintext);
30+
}
31+
encrypted = msEncrypt.ToArray();
32+
}
33+
}
34+
35+
return encrypted;
36+
}
37+
38+
public bool IsPaddingCorrect(byte[] ciphertext)
39+
{
40+
try
41+
{
42+
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
43+
44+
using (MemoryStream msDecrypt = new MemoryStream(ciphertext))
45+
{
46+
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
47+
{
48+
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
49+
{
50+
srDecrypt.ReadToEnd();
51+
}
52+
53+
}
54+
}
55+
}
56+
catch (CryptographicException)
57+
{
58+
return false;
59+
}
60+
61+
return true;
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)