-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge15.cpp
More file actions
49 lines (34 loc) · 1.34 KB
/
challenge15.cpp
File metadata and controls
49 lines (34 loc) · 1.34 KB
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
/*
PKCS#7 padding validation
Write a function that takes a plaintext, determines if it has valid PKCS#7 padding, and strips the padding off.
The string:
"ICE ICE BABY\x04\x04\x04\x04"
... has valid padding, and produces the result "ICE ICE BABY".
The string:
"ICE ICE BABY\x05\x05\x05\x05"
... does not have valid padding, nor does:
"ICE ICE BABY\x01\x02\x03\x04"
If you are writing in a language with exceptions, like Python or Ruby, make your function throw an exception on bad padding.
Crypto nerds know where we're going with this. Bear with us.
*/
#include "challenges.h"
#include "encoding_utils.h"
std::string challenge15() {
const std::string validPadding = "ICE ICE BABY\x04\x04\x04\x04";
const std::string invalidPadding1 = "ICE ICE BABY\x05\x05\x05\x05";
const std::string invalidPadding2 = "ICE ICE BABY\x01\x02\x03\x04";
std::ostringstream result;
auto testUnpad = [&](const std::string& input, const std::string& label) {
try {
std::string upadded = unpadPKCS7WithErrors(input);
result << label << ": Unpadded successfully!\n";
}
catch (const std::exception& e) {
result << label << " : Unpad failed! Error - " << e.what() << "\n";
}
};
testUnpad(validPadding, "Valid PKCS7 String");
testUnpad(invalidPadding1, "Invalid PKCS7 String 1");
testUnpad(invalidPadding2, "Invalid PKCS7 String 2");
return result.str();
}