C# Delphi中C加密/解密代码的等价物
我有一个C代码来加密/解密字符串:C# Delphi中C加密/解密代码的等价物,c#,delphi,encryption,cryptography,C#,Delphi,Encryption,Cryptography,我有一个C代码来加密/解密字符串: private static byte[] EncryptString(byte[] clearText, byte[] Key, byte[] IV) { MemoryStream ms = new MemoryStream(); Rijndael alg = Rijndael.Create(); alg.Key = Key; alg.IV = IV; CryptoStream cs = new CryptoStrea
private static byte[] EncryptString(byte[] clearText, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(clearText, 0, clearText.Length);
cs.Close();
byte[] encryptedData = ms.ToArray();
return encryptedData;
}
public static string EncryptString(string clearText, string Password)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x74, 0x68, 0x69, 0x73, 0x69, 0x61, 0x74, 0x65, 0x73, 0x74 });
byte[] encryptedData = EncryptString(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}
private static byte[] DecryptString(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
byte[] decryptedData = ms.ToArray();
return decryptedData;
}
public static string DecryptString(string cipherText, string Password)
{
if (!string.IsNullOrEmpty(cipherText))
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x74, 0x68, 0x69, 0x73, 0x69, 0x61, 0x74, 0x65, 0x73, 0x74 });
byte[] decryptedData = DecryptString(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return System.Text.Encoding.Unicode.GetString(decryptedData);
}
else
{
return "";
}
}
加密字符串应本地存储在注册表或文件中。Delphi应用程序也必须能够访问此字符串。请注意,由于某些原因,无法将加密/解密代码外包给DLL
我的问题是我无法在Delphi中生成PasswordDerivedBytes。
有人能给我一个提示吗?我建议您使用。它提供了几乎所有您需要的cryptowise和一些示例,这些示例主要完成您想要做的事情。不幸的是,StackOverflow不是用来将代码从一种语言转换为另一种语言的。你需要自己做研究,自己做这件事,当你有一个特定的问题时,问问这个特定的问题。找一个并使用它。我很抱歉以不恰当的方式问我的问题。是的,你是对的,你可以有这样的印象,我想要一个从C到Delphi的代码对话,但这不是我的意图。请允许我澄清我的问题:我的特殊问题是我无法在Delphi中获得PasswordDerivedBytes的对应项。因此,我无法在Delphi中得到正确的结果。你能给我一个如何在Delphi中构建PasswordDerivedBytes的提示吗?请把细节放在问题中,不要评论。编辑问题。PasswordDerivedBytes必须是您发布的代码专有的类型,但您的代码不包括此类型。你确定这就是全部代码吗?