Decrypt a string with C# that was originally encrypted in Foxpro
I'm writting a .Net MVC program that has to be able to read strings that were encrypted by a Foxpro application. If I were doing the encryption and decryption from the start, no problem. I don't know what foxpro's Encrpyt and Decrypt methods are doing under the hood to know how to apply the same encrypt and decrypt methods in C#.
Here is the call to Encrypt in foxpro.
encyptedString = ENCRYPT(saltString + unencryptedString, secretkey_aes256,2,0)
Here is the call to Decrypt in foxpro (second line removes the salt)
unencryptedString = DECRYPT(encryptedString, secretkey_aes256,2,0)
unencryptedString = SUBSTR(unencryptedString, LEN(saltString) + 1)
I obviously know the values for the saltString and secretkey_aes256 and yes the aes256 is a clue but I don't know what the parameters 2 and 0 are doing.
Thought someone would get a kick out of solving this riddle and maybe to them it isn't even a riddle. I've been working on it for a couple of hours and I'm just not savvy enough with encrypt and decrypt to figure it out so far.
Any help is appreciated.
EDIT:
if 2 mean A开发者_如何学CES256 and 0 means ECB, any clues as to what I'm doing wrong with the code below??
public string DecryptString(string encrypted)
{
RijndaelManaged myAES = new RijndaelManaged();
myAES.KeySize = 256;
myAES.BlockSize = 256;
myAES.Mode = CipherMode.ECB;
myAES.Padding = PaddingMode.None;
byte[] _key = ASCIIEncoding.UTF8.GetBytes(_secret);
myAES.Key = _key;
byte[] encrypted_bytes = ASCIIEncoding.UTF8.GetBytes(@encrypted.Trim());
ICryptoTransform decrypto = myAES.CreateDecryptor();
string decrypted = "";
decrypted = ASCIIEncoding.UTF8.GetString(decrypto.TransformFinalBlock(encrypted_bytes, 0, encrypted.Trim().Length));
return decrypted;
}
VFP doesn't have built-in encryption and decryption, so whoever wrote the code was using a 3rd party product. Chris Diver is probably right that it was Craig Boyd's VFPEncryption.FLL. Here's the latest documentation for that library: http://www.sweetpotatosoftware.com/spsblog/2009/08/09/MajorVFPEncryptionUpdate.aspx
Since there is not enough information you will need experiment a bit to find the right BlockSize
(256 Bit?) and IV
, I would think it is all set to 0x00 .
For the decryption itself you can use RijndaelManaged
with KeySize
set to 256 bits and Mode
set to ECB
- a good starting point is http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx
AES256 has a blocksize of 128 bits and a keysize of 256 bits. Therefore you need to set the blocksize of your RijndaelManaged instance to 128, not 256.
精彩评论