2011-02-23 54 views
1

背景:我有一個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中定義的初始化向量默認的對稱算法。

任何幫助表示讚賞。另外,請讓我知道是否有更好的/不同的方法可能更容易。

謝謝。

回答

0

我最終把密鑰放入代碼中。當然不是最好的解決方案,但它的工作。無論如何,我不再使用這種方法。我正在實施一個完全不同的場景。

0

無論如何,考慮到這戶頭:

throw new InvalidOperationException(String.Format("Unable to {0}crypt data. See inner exception for more detail.", encryptFlag ? "en" : "de"), ex); 

這是相當可讀。不是嗎?

另一件事 - using塊可以嵌套,所以不需要額外的縮進。

+0

是的,string.format的東西不是非常可讀的,但我喜歡避免在任何可能的地方添加字符串。我打算把它分成兩行,但是爲什麼要打擾一個異常消息呢?它不是複雜的商業邏輯,我試圖讓它可讀。無論是或者只是讓消息總是說「加密」。無論如何,我喜歡用大括號省略的技巧,我會試試看。 – mikesigs 2011-03-01 16:07:23