背景:我有一个 SSIS 包,它从系统 A 加载配置文件数据并在系统 B 中创建相应的配置文件和成员资格用户。系统 B 使用自定义 ASP.NET 成员资格提供程序,它必须能够解密 SSIS 包生成的密码。我为 SSIS 包使用的盐生成和加密创建了 CLR 存储过程。
问题:为了让 ASP.NET Membership Provider 能够解密加密的密码,我需要设置 CLR 存储过程使用的 MachineKey。我不知道如何做到这一点,或者是否有可能。
我使用 Reflector 从 System.Membership dll 中提取所需的加密代码。经过一番重构后,它看起来像这样:
private static byte[] PerformEncryption(byte[] input, bool encryptFlag)
{
if (input == null)
return null;
byte[] inputBuf = input;
byte[] outputBuf;
try
{
using (MemoryStream targetStream = new MemoryStream())
using (SymmetricAlgorithm algo = SymmetricAlgorithm.Create())
using (ICryptoTransform cryptoTransform = CreateCryptoTransform(algo, encryptFlag))
using (CryptoStream cryptoStream = new CryptoStream(targetStream, cryptoTransform, CryptoStreamMode.Write))
{
int start = 0;
int length = input.Length;
// Write the input buffer to the cryptoStream passing the byte stream through the cryptoTransform
cryptoStream.Write(inputBuf, start, length);
cryptoStream.FlushFinalBlock();
outputBuf = targetStream.ToArray();
}
}
catch (Exception ex)
{
throw new InvalidOperationException("Unable to " + (encryptFlag ? "en" : "de") + "crypt data. See inner exception for more detail.", ex);
}
return outputBuf;
}
问题是SymmetricAlgorithm.Create()
使用 MachineKey 定义的初始向量创建默认对称算法。
任何帮助表示赞赏。另外,如果有更好/不同的方法可能更容易,请告诉我。
谢谢。