Skip to content

Commit 5c590b0

Browse files
committed
Add ByteUtils, more refactoring
1 parent e072594 commit 5c590b0

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

ByteUtils.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Padding_Oracle_Attack
5+
{
6+
static class ByteUtils
7+
{
8+
public static List<byte[]> sliceBytesIntoBlocks(byte[] bytes, int blockSizeBytes = 16)
9+
{
10+
var blocks = new List<byte[]>();
11+
12+
for (var i = 0; i < bytes.Length; i += blockSizeBytes)
13+
{
14+
byte[] block = new byte[blockSizeBytes];
15+
Array.Copy(bytes, i, block, 0, blockSizeBytes);
16+
blocks.Add(block);
17+
}
18+
19+
return blocks;
20+
}
21+
22+
public static byte[] xor(byte[] bytes, byte[] other)
23+
{
24+
if (bytes.Length != other.Length)
25+
{
26+
throw new ArgumentException("Both arrays must have same length");
27+
}
28+
29+
var result = new byte[bytes.Length];
30+
31+
for (int i = 0; i < bytes.Length; ++i)
32+
{
33+
result[i] = (byte)(bytes[i] ^ other[i]);
34+
}
35+
36+
return result;
37+
}
38+
39+
public static byte[] concat(byte[] first, byte[] second)
40+
{
41+
var result = new byte[first.Length + second.Length];
42+
43+
first.CopyTo(result, 0);
44+
second.CopyTo(result, first.Length);
45+
46+
return result;
47+
}
48+
}
49+
}

Program.cs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,27 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Security.Cryptography;
52

63
namespace Padding_Oracle_Attack
74
{
5+
using static ByteUtils;
6+
87
class PaddingOracleAttack
98
{
109
private static RemoteServerMock server = new RemoteServerMock();
10+
1111
public static void Main()
1212
{
1313
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.";
1414

15-
using (Aes aes = Aes.Create())
16-
{
17-
byte[] encrypted = server.Encrypt(hiddenMessage);
18-
var blocks = sliceBytesIntoBlocks(encrypted);
19-
20-
Console.WriteLine("Plaintext:\n{0}", hiddenMessage);
21-
Console.WriteLine("\nCiphertext:\n{0}", String.Join("\n", blocks.ConvertAll(block => Convert.ToBase64String(block))));
22-
Console.WriteLine("\nAttack results:\nTODO");
23-
24-
encrypted[encrypted.Length - 1] = 22;
15+
byte[] encrypted = server.Encrypt(hiddenMessage);
16+
var blocks = sliceBytesIntoBlocks(encrypted);
2517

26-
Console.WriteLine("\nPadding is {0}", server.IsPaddingCorrect(encrypted) ? "correct" : "incorrect");
27-
}
28-
}
29-
30-
static List<byte[]> sliceBytesIntoBlocks(byte[] bytes, int blockSizeBytes = 16)
31-
{
32-
var blocks = new List<byte[]>();
18+
Console.WriteLine("Plaintext:\n{0}", hiddenMessage);
19+
Console.WriteLine("\nCiphertext:\n{0}", String.Join("\n", blocks.ConvertAll(block => Convert.ToBase64String(block))));
20+
Console.WriteLine("\nAttack results:\nTODO");
3321

34-
for (var i = 0; i < bytes.Length; i += blockSizeBytes)
35-
{
36-
byte[] block = new byte[blockSizeBytes];
37-
Array.Copy(bytes, i, block, 0, blockSizeBytes);
38-
blocks.Add(block);
39-
}
22+
encrypted[encrypted.Length - 1] = 22;
4023

41-
return blocks;
24+
Console.WriteLine("\nPadding is {0}", server.IsPaddingCorrect(encrypted) ? "correct" : "incorrect");
4225
}
4326
}
4427
}

0 commit comments

Comments
 (0)