Skip to content

Commit 9c52151

Browse files
committed
Implement padding oracle attack, remove not needed code
1 parent 5c590b0 commit 9c52151

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

ByteUtils.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,6 @@ public static List<byte[]> sliceBytesIntoBlocks(byte[] bytes, int blockSizeBytes
1919
return blocks;
2020
}
2121

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-
3922
public static byte[] concat(byte[] first, byte[] second)
4023
{
4124
var result = new byte[first.Length + second.Length];

Program.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text;
23

34
namespace Padding_Oracle_Attack
45
{
@@ -17,11 +18,45 @@ public static void Main()
1718

1819
Console.WriteLine("Plaintext:\n{0}", hiddenMessage);
1920
Console.WriteLine("\nCiphertext:\n{0}", String.Join("\n", blocks.ConvertAll(block => Convert.ToBase64String(block))));
20-
Console.WriteLine("\nAttack results:\nTODO");
21+
Console.WriteLine("\nAttack results:");
2122

22-
encrypted[encrypted.Length - 1] = 22;
23+
for (int blockIndex = 1; blockIndex < blocks.Count; ++blockIndex)
24+
{
25+
Console.WriteLine(DecryptBlock(blocks[blockIndex], blocks[blockIndex - 1]));
26+
}
27+
}
28+
29+
private static string DecryptBlock(byte[] block, byte[] previousBlock)
30+
{
31+
byte[] decrypted = new byte[block.Length];
32+
byte[] manipulatedPrevious = new byte[16];
33+
34+
// in case of PKCS7 padding value is same as padding length
35+
for (int paddingLength = 1; paddingLength <= block.Length; ++paddingLength)
36+
{
37+
for (int pos = block.Length - 1; pos >= block.Length - paddingLength; --pos)
38+
{
39+
int previousPaddingLength = paddingLength - 1;
40+
manipulatedPrevious[pos] ^= (byte)(previousPaddingLength ^ paddingLength);
41+
}
42+
var found = false;
43+
for (byte v = byte.MinValue; v <= byte.MaxValue; ++v)
44+
{
45+
manipulatedPrevious[block.Length - paddingLength] = v;
46+
if (server.IsPaddingCorrect(concat(manipulatedPrevious, block)))
47+
{
48+
found = true;
49+
decrypted[block.Length - paddingLength] = (byte)(previousBlock[block.Length - paddingLength] ^ paddingLength ^ v);
50+
break;
51+
}
52+
}
53+
if (!found)
54+
{
55+
throw new Exception("Decryption not possible. This function supports only AES/CBC/PKCS7");
56+
}
57+
}
2358

24-
Console.WriteLine("\nPadding is {0}", server.IsPaddingCorrect(encrypted) ? "correct" : "incorrect");
59+
return Encoding.UTF8.GetString(decrypted, 0, decrypted.Length);
2560
}
2661
}
2762
}

0 commit comments

Comments
 (0)