nous avons un projet écrit en Delphi que nous voulons convertir en C#. Le problème est que nous avons des mots de passe et des paramètres cryptés et écrits dans le registre. Lorsque nous avons besoin d'un mot de passe spécifié, nous l'obtenons du registre et nous le décryptons pour que nous puissions l'utiliser. Pour la conversion en C#, nous devons le faire de la même manière afin que l'application puisse également être utilisée par les utilisateurs qui ont l'ancienne version et qui veulent la mettre à jour.
Voici le code que nous utilisons pour crypter/décrypter les chaînes dans Delphi:Comment décrypter une chaîne en C# qui a été cryptée en 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.
Ainsi, par exemple quand on veut chiffrer la chaîne asdf1234
nous obtenons le résultat WcOb/iKo4g8=
.
Nous voulons maintenant décrypter cette chaîne en C#. Voici ce que nous avons essayé de faire:
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);
}
Bien le résultat diffère du résultat attendu. Après nous appelons DecryptString()
nous nous attendons à obtenir asdf1234
mais nous obtenons autre chose.
Est-ce que quelqu'un a une idée de comment décrypter cela correctement?
Merci à l'avance
Simon
EDIT:
Ok, je vous remercie tous pour vos suggestions. Nous n'avons pas pu trouver comment tout faire en C#, nous avons donc décidé de prendre notre version de repli, en utilisant une DLL Delphi avec P/Invoke, comme cela a été suggéré.
Ceci est notre solution de repli. Nous avons déjà une DLL Delphi que nous pouvons P/Invoke. Mais il serait préférable d'avoir tout dans le même code C#. –
N'est-ce pas un problème de migration? Une fois que vous avez migré tous vos clients, la DLL Delphi peut être supprimée. –
Être ne peut pas assurer quand et si tous les clients sont migrés. –