我們有一個用Delphi編寫的項目,我們希望將其轉換爲C#。問題是我們有一些密碼和設置被加密並寫入註冊表。當我們需要一個指定的密碼時,我們從註冊表中獲取並解密,以便我們可以使用它。爲了轉換到C#,我們必須以同樣的方式進行,以便具有舊版本並想升級它的用戶也可以使用該應用程序。
這是我們使用加密代碼/解密在Delphi中的字符串:如何解密在Delphi中加密的C#中的字符串
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.
因此,例如,當我們想要的字符串asdf1234
我們得到的結果WcOb/iKo4g8=
加密。
我們現在想要在C#中解密該字符串。以下是我們試圖做的:
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);
}
那麼結果與預期結果不同。在我們撥打DecryptString()
後,我們預計會得到asdf1234
,但我們還有其他的東西。
有沒有人有如何正確解密的想法?
在此先感謝
西蒙
編輯:
好的,謝謝大家的建議。我們無法找到如何在C#中完成所有工作,因此我們決定採用我們的回退版本,使用帶有P/Invoke的Delphi DLL,正如它的建議。
這是我們的後備。我們已經有一個Delphi DLL,我們可以P/Invoke。但最好是將它全部放在相同的C#代碼中。 – 2010-05-31 08:27:15
這不是遷移問題嗎?一旦你遷移了所有的客戶,那麼delphi DLL就可以被刪除。 – 2010-05-31 08:35:46
無法確保何時以及是否所有客戶都被遷移。 – 2010-05-31 08:43:58