Skip to content

Instantly share code, notes, and snippets.

@fukuroder
Last active November 23, 2015 12:54
Show Gist options
  • Save fukuroder/4d4505fb835323679742 to your computer and use it in GitHub Desktop.
Save fukuroder/4d4505fb835323679742 to your computer and use it in GitHub Desktop.
#include "../JuceLibraryCode/JuceHeader.h"
std::vector<uint32> Encrypt(const std::string& text, const std::string& key)
{
// create Blowfish
BlowFish bf(key.data(), key.length());
// encrypt
std::vector<uint32> result;
auto p = text.begin();
while (p != text.end())
{
char data[8];
for (char& d : data)
{
if ( p != text.end() )
{
d = *p;
++p;
}
else
{
d = '\0';
}
}
uint32* pdata = reinterpret_cast<uint32*>(data);
bf.encrypt(pdata[0], pdata[1]);
result.push_back(pdata[0]);
result.push_back(pdata[1]);
}
return result;
}
std::string Decrypt(const std::vector<uint32>& enc, const std::string& key)
{
// create Blowfish
BlowFish bf(key.data(), key.length());
// decrypt
std::string result;
auto p = enc.begin();
while (p != enc.end())
{
uint32 data[2];
data[0] = *p;
++p;
if (p != enc.end())
{
data[1] = *p;
++p;
}
else
{
data[1] = 0;
}
bf.decrypt(data[0], data[1]);
char* pdata = reinterpret_cast<char*>(data);
for (int i = 0; i < 8; ++i)
{
if (pdata[i] != '\0')
{
result.push_back(pdata[i]);
}
}
}
return result;
}
int main ()
{
const char key[] = "3291E47C-AD64-47E5-8402-8C56CF84108C";
std::cout << "key: " << key << std::endl;
const char text[] = "01234567890123456789";
std::cout << "text: " << text << std::endl;
std::cout << "encrypt: ";
std::vector<uint32> enc = Encrypt(text, key);
for (uint32 e : enc)
{
std::cout << std::hex << e << " ";
}
std::cout << std::endl;
std::cout << "decrypt: " << Decrypt(enc, key) << std::endl;
return 0;
}
// key: 3291E47C-AD64-47E5-8402-8C56CF84108C
// text: 01234567890123456789
// encrypt: 834f37ca 3973bbde b4aa8669 ecf7f0c1 9dd0f56b d42cefce
// decrypt: 01234567890123456789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment