1- using System . Diagnostics ;
1+ using System . Net . Mime ;
2+ using System . Linq ;
3+ using System . Diagnostics ;
24using System ;
35using System . Text ;
6+ using Mono . Options ;
47
58namespace Padding_Oracle_Attack
69{
710 class PaddingOracleAttack
811 {
912 private static RemoteServerMock server = new RemoteServerMock ( ) ;
1013
11- public static void Main ( )
14+ public static void Main ( String [ ] args )
1215 {
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:" ) ;
1423 string plaintext = Console . ReadLine ( ) ;
1524
1625 byte [ ] encrypted = server . Encrypt ( plaintext ) ;
1726 var blocks = ByteUtils . SliceIntoBlocks ( encrypted ) ;
1827
1928 Console . WriteLine ( "\n Ciphertext blocks (base64):\n {0}" , String . Join ( "\n " , blocks . ConvertAll ( block => Convert . ToBase64String ( block ) ) ) ) ;
29+
2030 Console . WriteLine ( "\n Padding oracle attack results:" ) ;
2131 Console . WriteLine ( "(first block cannot be decrypted)" ) ;
2232
@@ -36,12 +46,39 @@ public static void Main()
3646 var decodedBlocksCount = blocks . Count - 1 ;
3747 Console . WriteLine ( "\n Decoded {0} blocks." , decodedBlocksCount ) ;
3848
39- if ( decodedBlocksCount > 0 ) {
49+ if ( decodedBlocksCount > 0 )
50+ {
4051 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 ) ;
4253 }
4354 }
4455
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+
4582 private static string DecryptBlock ( byte [ ] block , byte [ ] previousBlock )
4683 {
4784 byte [ ] decrypted = new byte [ block . Length ] ;
0 commit comments