SQL Server 中的 EncryptByPassPhrase 和 C# 中的 Triple DES 解密
上下文:
- 数据存储在 SQL Server 中。某些字段使用 EncryptWithPassPhrase 进行加密
update my_table set my_field = EncryptByPassPhrase('012345678901234567890123','some value')
使用 NHibernate 获取数据。使用Interceptor解码加密数据
public static string Decrypt(byte[] toDecryptArray, string key) { 字节[]键数组; keyArray = UTF8Encoding.UTF8.GetBytes(key); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); tdes.Key = keyArray; tdes.Mode = CipherMode.ECB; tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length); 返回 UTF8Encoding.UTF8.GetString(resultArray); }
它在 cTransform.TransformFinalBlock
上失败,提示“要解密的数据长度无效”。
我的代码有什么问题吗?数据使用相同的密钥进行加密/解密 (012345678901234567980123
)。
Context:
- data is stored in SQL Server. Some fields are encrypted using EncryptWithPassPhrase
update my_table set my_field = EncryptByPassPhrase('012345678901234567890123','some value')
data is fetch using NHibernate. Encrypted data are decoded using Interceptor
public static string Decrypt(byte[] toDecryptArray, string key) { byte[] keyArray; keyArray = UTF8Encoding.UTF8.GetBytes(key); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); tdes.Key = keyArray; tdes.Mode = CipherMode.ECB; tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); }
It fails on cTransform.TransformFinalBlock
saying "Length of data to decrypt is invalid".
What is wrong with my code? Data is encrypted/decrypted with same key (012345678901234567980123
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有找到如何通过 C# 代码解密使用 T-SQL 函数 EncryptByPassPhrase 加密的内容。
但 SQL Server 2005 允许您创建 C# 程序集并将其加载到 SQL Server 中,然后构建存储过程或函数或 C# 方法。因此,我使用应用程序和 SQL Server 之间的共享代码创建了“我的”EncryptByPassPhrase 函数,以便我可以在双方进行加密/解密。
I did not found out how to decrypt by C# code what was encrypted with T-SQL function EncryptByPassPhrase.
But SQL Server 2005 allow you to create C# assembly and load it into SQL Server, then build stored procedures or functions or your C# methods. Thus, I created "my" EncryptByPassPhrase function with shared code between app and SQL Server, so that I can encrypt/decrypt in both sides.
文档中提到密码短语时说:
“nvarchar、char、varchar、binary、varbinary 或 nchar 类型的变量,包含用于生成对称密钥的密码短语。”
http://msdn.microsoft.com/en-us/library/ms190357.aspx
所以在 C# 中无法解密的原因是您需要使用派生密钥,而不是密码本身。不过,我不知道你是如何获得密钥的。
The documentation says of the passphrase:
"A variable of type nvarchar, char, varchar, binary, varbinary, or nchar containing a passphrase from which to generate a symmetric key."
http://msdn.microsoft.com/en-us/library/ms190357.aspx
So the reason why you can't decrypt it in C# is that you need to use the derived key, not the passphrase itself. I don't know how you'd derive the key, though.