@@ -7,18 +7,23 @@ namespace Padding_Oracle_Attack
7
7
{
8
8
class PaddingOracleAttack
9
9
{
10
+ private static RemoteServerMock server = new RemoteServerMock ( ) ;
10
11
public static void Main ( )
11
12
{
12
13
string hiddenMessage = "I'd just like to interject for a moment. What you’re referring to as Linux, is in fact, GNU/Linux, or as I’ve recently taken to calling it, GNU plus Linux." ;
13
14
14
15
using ( Aes aes = Aes . Create ( ) )
15
16
{
16
- byte [ ] encrypted = EncryptStringToBytes_Aes ( hiddenMessage ) ;
17
+ byte [ ] encrypted = server . Encrypt ( hiddenMessage ) ;
17
18
var blocks = sliceBytesIntoBlocks ( encrypted ) ;
18
19
19
20
Console . WriteLine ( "Plaintext:\n {0}" , hiddenMessage ) ;
20
21
Console . WriteLine ( "\n Ciphertext:\n {0}" , String . Join ( "\n " , blocks . ConvertAll ( block => Convert . ToBase64String ( block ) ) ) ) ;
21
22
Console . WriteLine ( "\n Attack results:\n TODO" ) ;
23
+
24
+ encrypted [ encrypted . Length - 1 ] = 22 ;
25
+
26
+ Console . WriteLine ( "\n Padding is {0}" , server . IsPaddingCorrect ( encrypted ) ? "correct" : "incorrect" ) ;
22
27
}
23
28
}
24
29
@@ -35,75 +40,5 @@ static List<byte[]> sliceBytesIntoBlocks(byte[] bytes, int blockSizeBytes = 16)
35
40
36
41
return blocks ;
37
42
}
38
- static byte [ ] EncryptStringToBytes_Aes ( string plainText )
39
- {
40
- byte [ ] encrypted ;
41
-
42
- using ( Aes aesAlg = Aes . Create ( ) )
43
- {
44
- aesAlg . Mode = CipherMode . CBC ;
45
- aesAlg . Padding = PaddingMode . PKCS7 ;
46
-
47
- ICryptoTransform encryptor = aesAlg . CreateEncryptor ( aesAlg . Key , aesAlg . IV ) ;
48
-
49
- using ( MemoryStream msEncrypt = new MemoryStream ( ) )
50
- {
51
- using ( CryptoStream csEncrypt = new CryptoStream ( msEncrypt , encryptor , CryptoStreamMode . Write ) )
52
- {
53
- using ( StreamWriter swEncrypt = new StreamWriter ( csEncrypt ) )
54
- {
55
- swEncrypt . Write ( plainText ) ;
56
- }
57
- encrypted = msEncrypt . ToArray ( ) ;
58
- }
59
- }
60
- }
61
-
62
- return encrypted ;
63
- }
64
-
65
- static string DecryptStringFromBytes_Aes ( byte [ ] cipherText , byte [ ] Key , byte [ ] IV )
66
- {
67
- // Check arguments.
68
- if ( cipherText == null || cipherText . Length <= 0 )
69
- throw new ArgumentNullException ( "cipherText" ) ;
70
- if ( Key == null || Key . Length <= 0 )
71
- throw new ArgumentNullException ( "Key" ) ;
72
- if ( IV == null || IV . Length <= 0 )
73
- throw new ArgumentNullException ( "IV" ) ;
74
-
75
- // Declare the string used to hold
76
- // the decrypted text.
77
- string plaintext = null ;
78
-
79
- // Create an Aes object
80
- // with the specified key and IV.
81
- using ( Aes aesAlg = Aes . Create ( ) )
82
- {
83
- aesAlg . Key = Key ;
84
- aesAlg . IV = IV ;
85
-
86
- // Create a decryptor to perform the stream transform.
87
- ICryptoTransform decryptor = aesAlg . CreateDecryptor ( aesAlg . Key , aesAlg . IV ) ;
88
-
89
- // Create the streams used for decryption.
90
- using ( MemoryStream msDecrypt = new MemoryStream ( cipherText ) )
91
- {
92
- using ( CryptoStream csDecrypt = new CryptoStream ( msDecrypt , decryptor , CryptoStreamMode . Read ) )
93
- {
94
- using ( StreamReader srDecrypt = new StreamReader ( csDecrypt ) )
95
- {
96
-
97
- // Read the decrypted bytes from the decrypting stream
98
- // and place them in a string.
99
- plaintext = srDecrypt . ReadToEnd ( ) ;
100
- }
101
- }
102
- }
103
-
104
- }
105
-
106
- return plaintext ;
107
- }
108
43
}
109
44
}
0 commit comments