1
- using System ;
1
+ using System . Diagnostics ;
2
+ using System ;
2
3
using System . Text ;
3
4
4
5
namespace Padding_Oracle_Attack
5
6
{
6
- using static ByteUtils ;
7
-
8
7
class PaddingOracleAttack
9
8
{
10
9
private static RemoteServerMock server = new RemoteServerMock ( ) ;
@@ -15,17 +14,32 @@ public static void Main()
15
14
string plaintext = Console . ReadLine ( ) ;
16
15
17
16
byte [ ] encrypted = server . Encrypt ( plaintext ) ;
18
- var blocks = sliceBytesIntoBlocks ( encrypted ) ;
17
+ var blocks = ByteUtils . SliceIntoBlocks ( encrypted ) ;
19
18
20
19
Console . WriteLine ( "\n Ciphertext blocks (base64):\n {0}" , String . Join ( "\n " , blocks . ConvertAll ( block => Convert . ToBase64String ( block ) ) ) ) ;
21
20
Console . WriteLine ( "\n Padding oracle attack results:" ) ;
22
21
Console . WriteLine ( "(first block cannot be decrypted)" ) ;
23
22
23
+ var stopwatch = new Stopwatch ( ) ;
24
+
24
25
for ( int blockIndex = 1 ; blockIndex < blocks . Count ; ++ blockIndex )
25
26
{
27
+ stopwatch . Start ( ) ;
28
+
26
29
string decryptedPlaintext = DecryptBlock ( blocks [ blockIndex ] , blocks [ blockIndex - 1 ] ) ;
30
+
31
+ stopwatch . Stop ( ) ;
32
+
27
33
Console . WriteLine ( decryptedPlaintext [ 0 ] != 16 ? decryptedPlaintext : "(padding-only block)" ) ;
28
34
}
35
+
36
+ var decodedBlocksCount = blocks . Count - 1 ;
37
+ Console . WriteLine ( "\n Decoded {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
+ }
29
43
}
30
44
31
45
private static string DecryptBlock ( byte [ ] block , byte [ ] previousBlock )
@@ -45,7 +59,7 @@ private static string DecryptBlock(byte[] block, byte[] previousBlock)
45
59
for ( byte v = byte . MinValue ; v <= byte . MaxValue ; ++ v )
46
60
{
47
61
manipulatedPrevious [ block . Length - paddingLength ] = v ;
48
- if ( server . IsPaddingCorrect ( concat ( manipulatedPrevious , block ) ) )
62
+ if ( server . IsPaddingCorrect ( ByteUtils . Concatenate ( manipulatedPrevious , block ) ) )
49
63
{
50
64
found = true ;
51
65
decrypted [ block . Length - paddingLength ] = ( byte ) ( previousBlock [ block . Length - paddingLength ] ^ paddingLength ^ v ) ;
0 commit comments