在.Net 4.5.2升级后尝试解密数据时出现HttpException

时间:2014-09-08 21:24:39

标签: c# .net encryption machinekey httpexception

我有一个REST服务(C#/ IIS),其中使用以下内容加密少量数据:

var encryptedText = MachineKey.Encode(bytes, MachineKeyProtection.All)

稍后将加密的字符串发布(发送到REST服务)并使用以下方法解码:

MachineKey.Decode(encryptedText, MachineKeyProtection.All)

MachineKey是自动生成的,如web.config中所示:

    <machineKey
        decryption="AES"
        decryptionKey="AutoGenerate"
        validation="AES"
        validationKey="AutoGenerate" />

系统从.Net 4.5.1升级到4.5.2后,我无法解密升级前加密的字符串;它给出了一个HttpException“无法验证数据”。 (我可以解密升级后加密的字符串。)

因此,4.5.1和4.5.2之间的某些内容在算法上发生了变化,使它们不兼容。我无法在网上找到有关此特定问题的任何内容。有没有人有关于这个问题的具体细节和/或解决这个问题的工作?

如果重要,该项目的目标是 .Net 4.0 ,而不是4.5或4.5.1或4.5.2。

(顺便说一句,听起来不建议将MachineKey.Encode / Decode用于短期加密以外的任何其他事情,因为这类问题可能吗?另外,我知道现在不推荐使用Encode / Decode ,但我有一个现有的系统,此时无法改变它。)

更新

当键是AutoGenerate时,这只是一个问题。如果我给他们明确的值,升级后一切都会继续正常工作。

1 个答案:

答案 0 :(得分:0)

One thing that I noticed is Load User Profile settings in IIS Application Pool. If you have it disabled for IIS 6 compatibility MachineKey.Decode fails to decrypt data every time app pool is recycled. For my own tests it seems like only obsolete Encode/Decode methods suffer from it and that too when using MachineKeyProtection.All. They work fine with MachineKeyProtection.Encryption.