1

背景:我有一个 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 定义的初始向量创建默认对称算法。

任何帮助表示赞赏。另外,如果有更好/不同的方法可能更容易,请告诉我。

谢谢。

4

2 回答 2

0

无论如何,考虑到这一点:

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

这是相当可读的。不是吗?

还有一件事——using块可以嵌套,所以不需要额外的缩进。

于 2011-02-26T01:42:06.397 回答
0

我最终将密钥放入代码中。当然不是最好的解决方案,但它确实有效。无论如何,我不再使用这种方法了。我正在实施一个完全不同的场景。

于 2011-03-02T17:30:11.443 回答