[英]SQL CLR stored procedure for encryption
背景:我有一个SSIS包,该包从系统A加载配置文件数据,并在系统B中创建相应的配置文件和成员身份用户。系统B使用自定义的ASP.NET成员资格提供程序,该提供程序必须能够解密SSIS程序包生成的密码。 我已经创建了用于盐生成和加密的CLR存储过程,以供SSIS包使用。
问题:为了使ASP.NET成员资格提供程序可以解密加密的密码,我需要设置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定义的初始向量创建默认的对称算法。
任何帮助表示赞赏。 另外,请告诉我是否有更好/不同的方法可能会更容易。
谢谢。
无论如何,要考虑这一点:
throw new InvalidOperationException(String.Format("Unable to {0}crypt data. See inner exception for more detail.", encryptFlag ? "en" : "de"), ex);
这是相当可读的。 是不是
还有一件事- using
块可以嵌套,因此不需要其他缩进。
我最终将密钥放入代码中。 当然不是最好的解决方案,但是它有效。 无论如何,我不再使用这种方法。 我正在实施一个完全不同的方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.