背景:我有一個SSIS包,用於從系統A加載配置文件數據並在系統B中創建相應的配置文件和成員資格用戶。系統B使用必須能夠使用的自定義ASP.NET成員資格提供程序解密由SSIS包生成的密碼。我已經創建了CLR存儲過程以供SSIS包使用的鹽生成和加密。用於加密的SQL CLR存儲過程
問題:爲了使加密密碼可以被ASP.NET成員提供者解密,我需要設置CLR存儲過程使用的MachineKey。我不知道該怎麼做,或者甚至有可能。
我用反射器從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中定義的初始化向量默認的對稱算法。
任何幫助表示讚賞。另外,請讓我知道是否有更好的/不同的方法可能更容易。
謝謝。
是的,string.format的東西不是非常可讀的,但我喜歡避免在任何可能的地方添加字符串。我打算把它分成兩行,但是爲什麼要打擾一個異常消息呢?它不是複雜的商業邏輯,我試圖讓它可讀。無論是或者只是讓消息總是說「加密」。無論如何,我喜歡用大括號省略的技巧,我會試試看。 – mikesigs 2011-03-01 16:07:23