Skip to content

Instantly share code, notes, and snippets.

@randombit
Last active March 8, 2018 12:42
Show Gist options
  • Save randombit/9c024c00c5b476a0fea0c8ec7c141538 to your computer and use it in GitHub Desktop.
Save randombit/9c024c00c5b476a0fea0c8ec7c141538 to your computer and use it in GitHub Desktop.
#include <botan/fpe_fe1.h>
#include <assert.h>
#include <iostream>
using namespace Botan;
BigInt power(size_t g, size_t x)
{
BigInt r = 1;
for(size_t i = 0; i != x; ++i)
r *= g;
return r;
}
BigInt rank(const std::string& s)
{
BigInt r = 0;
for(size_t i = 0; i != s.size(); ++i)
{
char c = s[i];
if(::islower(c) == false)
throw std::runtime_error("Unexpected character in string");
r *= 26;
r += (c - 'a');
}
return r;
}
std::string derank(BigInt n, size_t len)
{
std::string s;
for(size_t i = 0; i != len; ++i)
{
char c = 'a' + (n % 26);
s.push_back(c);
n /= 26;
}
std::reverse(s.begin(), s.end());
return s;
}
int main()
{
const SymmetricKey key("ABCDEF");
std::vector<std::string> words = {
"aaa", "abc", "abcc",
"here", "are", "some", "words", "i", "will", "encrypt",
"some", "are", "longer", "some", "are", "shorter",
"notice", "that", "some", "is", "encrypted", "differently",
"each", "time", "due", "to", "the", "changing", "tweak",
};
std::vector<uint8_t> tweak(8);
uint64_t ctr = 0;
for(std::string word : words)
{
const size_t len = word.size();
BigInt r = rank(word);
assert(word == derank(r, len));
// This could be cached instead of repeatedly computed
BigInt mod = power(26, len);
BigInt c = FPE::fe1_encrypt(mod, r, key, tweak);
std::string ctext = derank(c, len);
std::cout << word << " -> "<< ctext << "\n";
BigInt p = FPE::fe1_decrypt(mod, rank(ctext), key, tweak);
std::string ptext = derank(p, ctext.size());
assert(ptext == word);
++ctr;
store_be(ctr, tweak.data());
}
}
@MCCoolMoDAue
Copy link

MCCoolMoDAue commented Mar 8, 2018

when I run this test program it fails in line 49
const SymmetricKey key("ABCDEF");
when I run it in debug, however it doesn't.

I built it in VC2017 on windows.
is there a known issue?

botan was build in 32 bit.

@MCCoolMoDAue
Copy link

MCCoolMoDAue commented Mar 8, 2018

never mind, somehow I got this working again for me.
may have been the configuration options.
This worked for me:
python configure.py --cpu=x86 --os=windows --cc=msvc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment