Hey All,
I'm trying to encrypt a string in Setup Factory using the Blowfish algorithm and decrypt it in .NET using CryptoSys API for .NET.
Here's a high level overview of what I'm trying to accomplish:
1st) Use the a Crypto action in Setup Factory to encrypt a string that stores some application data in the registry. I am currently attempting to implement Blowfish within Setup Factory. I'm not too concerned with what algorithm I use, whatever is the easiest to implement is fine.
2nd) I need to read this string from within our software application and then decrypt it using C# .NET.
When I run my encryption code in Setup Factory I get: "WcxpiBwj9bgxvjBq7nsQ==".
When I run my encryption code in .NET I get: "j9PpOSolpFBbKGerLANSFA==".
I'm currently using http://www.cryptosys.net/ for the Blowfish encryption. I considered purchasing Chilkat, but I don't want to add an additional 5.5MB to our download.
I would expect them to be the same values, but unfortunately not. In Blowfish, there really isn't too much to configure.
Thanks in advance,
Josh
///////////////////////////////////////////
// SETUP FACTORY CODE
///////////////////////////////////////////
sEncryptedString = Crypto.BlowfishEncryptString("secrettext", "secretkey", 0);
Dialog.Message("Encrypted String", sEncryptedString);
///////////////////////////////////////////
// CALL TO OUR BLOWFISH ENCRYPTION FUNCTION IN .NET
///////////////////////////////////////////
string strEncrypt = Crytography.Encrypt("secrettext", "secretkey");
MessageBox.Show(strEncrypt);
///////////////////////////////////////////
// .NET BLOWFISH ENCRYPT FUNCTION
///////////////////////////////////////////
public static string Encrypt(string toEncrypt, string key)
{
Console.WriteLine("CryptoSys API Version={0}", General.Version());
// ENCRYPTION.
// INPUT: data string, key in hex format
// OUTPUT: ciphertext in hex format
Console.WriteLine("ENCRYPTION:");
string keyHex = Cnv.ToHex(key);
string data = toEncrypt;
// 1. Convert string to hex format
string dataHex = Cnv.ToHex(data);
// 2. Pad it ready for encryption
string plainHex = Blowfish.Pad(dataHex);
Console.WriteLine("SecretKey ='{0}'", key);
Console.WriteLine("Key ={0}", keyHex);
Console.WriteLine("SecretText ='{0}'", data);
Console.WriteLine("Secret Text In Hex ={0}", dataHex);
Console.WriteLine("Padded Secret Text Hex ={0}", plainHex);
// 3. Encrypt it
string cipherHex = Blowfish.Encrypt(plainHex, keyHex, Mode.ECB, null);
Console.WriteLine("Encrypted Text ={0}", cipherHex);
string strBase64 = Convert.ToBase64String(Cnv.FromHex(cipherHex));
Console.WriteLine("Base64 Encrypted Text ={0}", strBase64);
// DECRYPTION.
// INPUT: ciphertext in hex format, key in hex format
// OUTPUT: data string OR "decryption failed" error
Console.WriteLine("DECRYPTION:");
Console.WriteLine("Key Hex ={0}", keyHex);
Console.WriteLine("Encrypted Text ={0}", Cnv.ToHex(Convert.FromBase64String(strBase64)));
// 1. Decrypt ciphertext
plainHex = Blowfish.Decrypt(cipherHex, keyHex, Mode.ECB, null);
Console.WriteLine("Output in hex ={0}", plainHex);
// 2. Unpad it
dataHex = Blowfish.Unpad(plainHex);
Console.WriteLine("After unpadding ={0}", dataHex);
// 3. Check for error (see CryptoSysAPI.chm help)
if (dataHex.Length == plainHex.Length)
{
Console.WriteLine("decyryption error");
return "decyryption error";
}
// 4. Convert from hex back to string, if OK
data = Cnv.StringFromHex(dataHex);
Console.WriteLine("Data ='{0}'", data);
return data;
}
///////////////////////////////////////////
// CONSOLE OUTPUT FROM .NET ENCRYPTION
///////////////////////////////////////////
CryptoSys API Version=430
ENCRYPTION:
SecretKey ='secretkey'
Key =7365637265746B6579
SecretText ='secrettext'
Secret Text In Hex =73656372657474657874
Padded Secret Text Hex =73656372657474657874060606060606
Encrypted Text =8FD3E9392A25A4505B2867AB2C035214
Base64 Encrypted Text =j9PpOSolpFBbKGerLANSFA==
DECRYPTION:
Key Hex =7365637265746B6579
Encrypted Text =8FD3E9392A25A4505B2867AB2C035214
Output in hex =73656372657474657874060606060606
After unpadding =73656372657474657874
Data ='secrettext'

Reply With Quote

