How to decrypt a string in C# that was encrypted in Delphi
Posted
by Simon Linder
on Stack Overflow
See other posts from Stack Overflow
or by Simon Linder
Published on 2010-05-31T08:15:08Z
Indexed on
2010/05/31
8:22 UTC
Read the original article
Hit count: 886
Hi all,
we have a project written in Delphi that we want to convert to C#. Problem is that we have some passwords and settings that are encrypted and written into the registry. When we need a specified password we get it from the registry and decrypt it so we can use it. For the conversion into C# we have to do it the same way so that the application can also be used by users that have the old version and want to upgrade it.
Here is the code we use to encrypt/decrypt strings in Delphi:
unit uCrypt;
interface
function EncryptString(strPlaintext, strPassword : String) : String;
function DecryptString(strEncryptedText, strPassword : String) : String;
implementation
uses
DCPcrypt2, DCPblockciphers, DCPdes, DCPmd5;
const
CRYPT_KEY = '1q2w3e4r5t6z7u8';
function EncryptString(strPlaintext) : String;
var
cipher : TDCP_3des;
strEncryptedText : String;
begin
if strPlaintext <> '' then
begin
try
cipher := TDCP_3des.Create(nil);
try
cipher.InitStr(CRYPT_KEY, TDCP_md5);
strEncryptedText := cipher.EncryptString(strPlaintext);
finally
cipher.Free;
end;
except
strEncryptedText := '';
end;
end;
Result := strEncryptedText;
end;
function DecryptString(strEncryptedText) : String;
var
cipher : TDCP_3des;
strDecryptedText : String;
begin
if strEncryptedText <> '' then
begin
try
cipher := TDCP_3des.Create(nil);
try
cipher.InitStr(CRYPT_KEY, TDCP_md5);
strDecryptedText := cipher.DecryptString(strEncryptedText);
finally
cipher.Free;
end;
except
strDecryptedText := '';
end;
end;
Result := strDecryptedText;
end;
end.
So for example when we want to encrypt the string asdf1234
we get the result WcOb/iKo4g8=
.
We now want to decrypt that string in C#. Here is what we tried to do:
public static void Main(string[] args)
{
string Encrypted = "WcOb/iKo4g8=";
string Password = "1q2w3e4r5t6z7u8";
string DecryptedString = DecryptString(Encrypted, Password);
}
public static string DecryptString(string Message, string Passphrase)
{
byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
// Step 1. We hash the passphrase using MD5
// We use the MD5 hash generator as the result is a 128 bit byte array
// which is a valid length for the TripleDES encoder we use below
MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
// Step 2. Create a new TripleDESCryptoServiceProvider object
TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
// Step 3. Setup the decoder
TDESAlgorithm.Key = TDESKey;
TDESAlgorithm.Mode = CipherMode.ECB;
TDESAlgorithm.Padding = PaddingMode.None;
// Step 4. Convert the input string to a byte[]
byte[] DataToDecrypt = Convert.FromBase64String(Message);
// Step 5. Attempt to decrypt the string
try
{
ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
}
finally
{
// Clear the TripleDes and Hashprovider services of any sensitive information
TDESAlgorithm.Clear();
HashProvider.Clear();
}
// Step 6. Return the decrypted string in UTF8 format
return UTF8.GetString(Results);
}
Well the result differs from the expected result. After we call DecryptString()
we expect to get asdf1234
but we get something else.
Does anyone have an idea of how to decrypt that correctly?
Thanks in advance
Simon
© Stack Overflow or respective owner