Skip to content

Instantly share code, notes, and snippets.

@eklitzke
Created January 4, 2021 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eklitzke/9e86051839e4ad2181db8cf5198c7b1f to your computer and use it in GitHub Desktop.
Save eklitzke/9e86051839e4ad2181db8cf5198c7b1f to your computer and use it in GitHub Desktop.
#include "cryptopals/aes.h"
#include "cryptopals/answer.h"
#include "cryptopals/block.h"
#include "cryptopals/codec.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/str_split.h"
namespace cryptopals {
namespace {
class Helper {
public:
Helper() : key_(Block::RandomBlock()) {}
std::string Oracle(const std::string &in) {
std::string escaped = absl::StrReplaceAll(in, {{";", "%3B"}, {"=", "%3D"}});
std::string plain = absl::StrCat("comment1=cooking%20MCs;userdata=", escaped,
";comment2=%20like%20a%20pound%20of%20bacon");
PadPKCS7(&plain);
return EncryptCBC(plain, key_);
}
bool IsAdmin(const std::string &cipher) {
std::string plain = DecryptCBC(cipher, key_);
UnpadPKCS7(&plain);
for (std::string_view chunk : absl::StrSplit(plain, ";")) {
if (chunk.empty()) {
continue;
}
std::vector<std::string_view> chunks = absl::StrSplit(chunk, "=");
if (chunks.size() == 2 && chunks[0] == "admin") {
return chunks[1] == "true";
}
}
return false;
}
private:
const Block key_;
};
void FlipBit(char &c) {
if (c & 1) {
c &= 0xfe;
} else {
c |= 1;
}
}
} // namespace
void Solution16(Answer &&ans) {
Helper h;
std::string input = ":admin<true";
std::string cipher = h.Oracle(input);
FlipBit(cipher[16]);
FlipBit(cipher[22]);
ASSERT_ALWAYS(h.IsAdmin(cipher));
}
} // namespace cryptopals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment