-
Notifications
You must be signed in to change notification settings - Fork 2
/
RemoteServerMock.cs
77 lines (65 loc) · 2.17 KB
/
RemoteServerMock.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.IO;
using System.Threading;
using System.Security.Cryptography;
namespace Padding_Oracle_Attack
{
class RemoteServerMock
{
private Aes aesAlg = Aes.Create();
public uint OracleDelayMilliseconds { get; set; } = 0;
public PaddingMode Padding {
get {
return aesAlg.Padding;
}
}
public RemoteServerMock(PaddingMode paddingMode = PaddingMode.PKCS7)
{
aesAlg.BlockSize = 128;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = paddingMode;
}
public byte[] Encrypt(string plaintext)
{
byte[] encrypted;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plaintext);
}
encrypted = msEncrypt.ToArray();
}
}
return encrypted;
}
public bool IsPaddingCorrect(byte[] ciphertext)
{
if (OracleDelayMilliseconds > 0)
{
Thread.Sleep((int)OracleDelayMilliseconds);
}
try
{
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(ciphertext))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
srDecrypt.ReadToEnd();
}
}
}
}
catch (CryptographicException)
{
return false;
}
return true;
}
}
}