1
- using System . Diagnostics ;
1
+ using System . Net . Mime ;
2
+ using System . Linq ;
3
+ using System . Diagnostics ;
2
4
using System ;
3
5
using System . Text ;
6
+ using Mono . Options ;
4
7
5
8
namespace Padding_Oracle_Attack
6
9
{
7
10
class PaddingOracleAttack
8
11
{
9
12
private static RemoteServerMock server = new RemoteServerMock ( ) ;
10
13
11
- public static void Main ( )
14
+ public static void Main ( String [ ] args )
12
15
{
13
- Console . WriteLine ( "Enter plaintext:" ) ;
16
+ Console . WriteLine ( "~~ Padding Oracle Attack Demo ~~" ) ;
17
+
18
+ HandleConfigurationArguments ( args ) ;
19
+
20
+ Console . WriteLine ( "Oracle response delay set to {0} ms." , server . OracleDelayMilliseconds ) ;
21
+
22
+ Console . WriteLine ( "\n Enter plaintext:" ) ;
14
23
string plaintext = Console . ReadLine ( ) ;
15
24
16
25
byte [ ] encrypted = server . Encrypt ( plaintext ) ;
17
26
var blocks = ByteUtils . SliceIntoBlocks ( encrypted ) ;
18
27
19
28
Console . WriteLine ( "\n Ciphertext blocks (base64):\n {0}" , String . Join ( "\n " , blocks . ConvertAll ( block => Convert . ToBase64String ( block ) ) ) ) ;
29
+
20
30
Console . WriteLine ( "\n Padding oracle attack results:" ) ;
21
31
Console . WriteLine ( "(first block cannot be decrypted)" ) ;
22
32
@@ -36,12 +46,39 @@ public static void Main()
36
46
var decodedBlocksCount = blocks . Count - 1 ;
37
47
Console . WriteLine ( "\n Decoded {0} blocks." , decodedBlocksCount ) ;
38
48
39
- if ( decodedBlocksCount > 0 ) {
49
+ if ( decodedBlocksCount > 0 )
50
+ {
40
51
var timeElapsed = stopwatch . Elapsed ;
41
- Console . WriteLine ( "Time elapsed: {0}, avg {1:0.0} ms per block" , timeElapsed . ToString ( ) , timeElapsed . Divide ( decodedBlocksCount ) . TotalMilliseconds ) ;
52
+ Console . WriteLine ( "Time elapsed: {0}, avg {1:0.000} s per block" , timeElapsed . ToString ( ) , timeElapsed . Divide ( decodedBlocksCount ) . TotalMilliseconds / 1000 ) ;
42
53
}
43
54
}
44
55
56
+ private static void HandleConfigurationArguments ( String [ ] args )
57
+ {
58
+ OptionSet arguments = new OptionSet ( ) ;
59
+ arguments . Add ( "d|delay=" , "oracle delay in milliseconds for each padding request" , ( uint d ) => server . OracleDelayMilliseconds = d ) ;
60
+ arguments . Add ( "h|help" , "displays this message" , _ => {
61
+ arguments . WriteOptionDescriptions ( Console . Out ) ;
62
+ Environment . Exit ( 0 ) ;
63
+ } ) ;
64
+
65
+ try
66
+ {
67
+ var rest = arguments . Parse ( args ) ;
68
+ if ( rest . Count == 0 ) {
69
+ return ;
70
+ }
71
+ Console . WriteLine ( "Unrecognized arguments: {0}" , String . Join ( "," , rest ) ) ;
72
+ }
73
+ catch ( OptionException e )
74
+ {
75
+ Console . WriteLine ( e . Message ) ;
76
+ }
77
+
78
+ arguments . WriteOptionDescriptions ( Console . Out ) ;
79
+ Environment . Exit ( 1 ) ;
80
+ }
81
+
45
82
private static string DecryptBlock ( byte [ ] block , byte [ ] previousBlock )
46
83
{
47
84
byte [ ] decrypted = new byte [ block . Length ] ;
0 commit comments