Skip to content

Commit 19f723e

Browse files
committed
Add elapsed time information, refactor some method names
1 parent 3da8411 commit 19f723e

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

ByteUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Padding_Oracle_Attack
55
{
66
static class ByteUtils
77
{
8-
public static List<byte[]> sliceBytesIntoBlocks(byte[] bytes, int blockSizeBytes = 16)
8+
public static List<byte[]> SliceIntoBlocks(byte[] bytes, int blockSizeBytes = 16)
99
{
1010
var blocks = new List<byte[]>();
1111

@@ -19,7 +19,7 @@ public static List<byte[]> sliceBytesIntoBlocks(byte[] bytes, int blockSizeBytes
1919
return blocks;
2020
}
2121

22-
public static byte[] concat(byte[] first, byte[] second)
22+
public static byte[] Concatenate(byte[] first, byte[] second)
2323
{
2424
var result = new byte[first.Length + second.Length];
2525

Program.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using System;
1+
using System.Diagnostics;
2+
using System;
23
using System.Text;
34

45
namespace Padding_Oracle_Attack
56
{
6-
using static ByteUtils;
7-
87
class PaddingOracleAttack
98
{
109
private static RemoteServerMock server = new RemoteServerMock();
@@ -15,17 +14,32 @@ public static void Main()
1514
string plaintext = Console.ReadLine();
1615

1716
byte[] encrypted = server.Encrypt(plaintext);
18-
var blocks = sliceBytesIntoBlocks(encrypted);
17+
var blocks = ByteUtils.SliceIntoBlocks(encrypted);
1918

2019
Console.WriteLine("\nCiphertext blocks (base64):\n{0}", String.Join("\n", blocks.ConvertAll(block => Convert.ToBase64String(block))));
2120
Console.WriteLine("\nPadding oracle attack results:");
2221
Console.WriteLine("(first block cannot be decrypted)");
2322

23+
var stopwatch = new Stopwatch();
24+
2425
for (int blockIndex = 1; blockIndex < blocks.Count; ++blockIndex)
2526
{
27+
stopwatch.Start();
28+
2629
string decryptedPlaintext = DecryptBlock(blocks[blockIndex], blocks[blockIndex - 1]);
30+
31+
stopwatch.Stop();
32+
2733
Console.WriteLine(decryptedPlaintext[0] != 16 ? decryptedPlaintext : "(padding-only block)");
2834
}
35+
36+
var decodedBlocksCount = blocks.Count - 1;
37+
Console.WriteLine("\nDecoded {0} blocks.", decodedBlocksCount);
38+
39+
if (decodedBlocksCount > 0) {
40+
var timeElapsed = stopwatch.Elapsed;
41+
Console.WriteLine("Time elapsed: {0}, avg {1:0.0} ms per block", timeElapsed.ToString(), timeElapsed.Divide(decodedBlocksCount).TotalMilliseconds);
42+
}
2943
}
3044

3145
private static string DecryptBlock(byte[] block, byte[] previousBlock)
@@ -45,7 +59,7 @@ private static string DecryptBlock(byte[] block, byte[] previousBlock)
4559
for (byte v = byte.MinValue; v <= byte.MaxValue; ++v)
4660
{
4761
manipulatedPrevious[block.Length - paddingLength] = v;
48-
if (server.IsPaddingCorrect(concat(manipulatedPrevious, block)))
62+
if (server.IsPaddingCorrect(ByteUtils.Concatenate(manipulatedPrevious, block)))
4963
{
5064
found = true;
5165
decrypted[block.Length - paddingLength] = (byte)(previousBlock[block.Length - paddingLength] ^ paddingLength ^ v);

0 commit comments

Comments
 (0)