// ==============================================================================
// 01-BLOWFISH Encryption method.
// ==============================================================================
/**
The key values for the BlowFish encryption method
*/
static const int BlowFishKeySize = 10;
static const uint8 BlowFishCryptoKey[BlowFishKeySize] =
{
15, 205, 127, 6, 92,
109, 16, 57, 137, 241
};
// ===================================================================================
// 03-ENCRYPT/DECRYPT UTILITIES
// ------------------------------
// Encrypt and decrypt the contents of registration-file, using BlowFish method
// ===================================================================================
struct EncryptUtils
{
/** -----------------------------------------------------------------------------
Encrypt the contents of registration/activation info
-------------------------------------------------------------------------------*/
static const String encryptKeys(const String JSON_Keys)
{
if (! JSON_Keys.length())
return String::empty;
BlowFish Bf(BlowFishCryptoKey, BlowFishKeySize);
String Keys_To_Encrypt = JSON_Keys;
// make buffer to have an even length, so we always have left and right ints
if (Keys_To_Encrypt.length() % 2 == 0)
Keys_To_Encrypt = Keys_To_Encrypt + " ";
else
Keys_To_Encrypt += " ";
// Encrypt Keys-string
String Encrypted_Keys = EncryptStringWithBlowFish(Keys_To_Encrypt);
return Encrypted_Keys;
}
/** -----------------------------------------------------------------------------
Encrypt a Key-String and returns an Encrypted Key-String with BlowFish method
Note for “%08x” specifier :
8 : show every single char in a manner of 8 bits
0 : prefix each char with 0’s instead of just blank spaces
x : print each char in a hexadecimal code using lower-case letters.
-----------------------------------------------------------------------------*/
static String EncryptStringWithBlowFish(String Keys_To_Encrypt)
{
BlowFish Bf(BlowFishCryptoKey, BlowFishKeySize);
String Encrypted_Keys = String::empty;
int Len = Keys_To_Encrypt.length();
for (int i = 0; i < Len; i += 2)
{
uint32 left = (uint32)Keys_To_Encrypt[i];
uint32 right = (uint32)Keys_To_Encrypt[i + 1];
Bf.encrypt(left, right);
Encrypted_Keys += String::formatted(("%08x"), left) + String::formatted(("%08x"), right);
}
return Encrypted_Keys;
}
/** -----------------------------------------------------------------------------
Decrypt the keys-String
-------------------------------------------------------------------------------*/
static String decryptKeys(String& JSON_Keys)
{
String Decrypted_Keys = String::empty;
// Decrypt keys-string
Decrypted_Keys = decryptStringFromBlowFish(JSON_Keys);
// adjust plain text by removing appended ' ' or ' '
int Len = Decrypted_Keys.length();
if (Decrypted_Keys[Len - 1] == ' ')
Decrypted_Keys = Decrypted_Keys.substring(0, Len - 1);
else
Decrypted_Keys = Decrypted_Keys.substring(0, Len - 2);
return Decrypted_Keys;
}
/**-----------------------------------------------------------------------------
Decrypt the contents of registrtion-file which preveously encrypted
by the BlowFish method
-----------------------------------------------------------------------------*/
static String decryptStringFromBlowFish(String Keys_To_Decrypt)
{
BlowFish Bf(BlowFishCryptoKey, BlowFishKeySize);
String Decrypted_Keys = String::empty;
int Len = Keys_To_Decrypt.length();
for (int i = 0; i < Len; i += 16)
{
String hex = Keys_To_Decrypt.substring(i, i + 8);
uint32 left = hex.getHexValue32();
hex = Keys_To_Decrypt.substring(i + 8, i + 16);
uint32 right = hex.getHexValue32();
Bf.decrypt(left, right);
//concatenate, but ignore 0s (which may arise because we padded everything to 8 bytes
if (left != 0)
Decrypted_Keys += String::createStringFromData(&left, 4);
if (right != 0)
Decrypted_Keys += String::createStringFromData(&right, 4);
}
return Decrypted_Keys;
}
}; // end of struct EncryptUtils
I hope this help you to understand how the BlowFish works. . .