-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge15.cs
41 lines (38 loc) · 1.13 KB
/
Challenge15.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
using System;
using System.Collections.Generic;
using System.Text;
namespace CryptoPalsChallenge
{
public static class Challenge15
{
private static int? GetPaddingCount(byte[] plainText)
{
byte padding = plainText[plainText.Length - 1];
if (padding == 0)
{
return null;
}
for (int i = 1; i < padding; i++)
{
if (plainText[plainText.Length - 1 - i] != padding)
{
return null;
}
}
return padding;
}
public static bool IsValidPkcs7Padding(byte[] plainText)
{
return GetPaddingCount(plainText) != null;
}
public static byte[] StripPkcs7Padding(byte[] plainText)
{
int? paddingCount = GetPaddingCount(plainText);
if (paddingCount == null)
{
throw new Exception("Bad padding");
}
return Utility.Pluck(plainText, 0, plainText.Length - paddingCount.Value);
}
}
}