RSA 解密在 Windows 2012 & Windows 10 中失败
RSA Decryption fails in Windows 2012 & Windows 10
RSA 解密在 Windows Server 2012 和 Windows 10 中失败,但在 Windows 7 中就像一个魅力。已放置在代码下方,其中还包含使用的相关密钥.
var rsa = new RSACryptoServiceProvider(1024);
const string privatekey = "<RSAKeyValue><Modulus>m38MvfdL8epfiYvgw6ylMB7zPAKSStQLklCQEOworQwJxw39bXdnm4717BUm5700vZCbG9GBgjwHUxmrFrFsSJxMIdYhokT/DuYOC9Nsf2fiX5Uq+WtBfbSneEr6yVk2VB1fgba6XxoK9O3CKzieR1XxPEofxGRjrXzMe8nIMNc=</Modulus><Exponent>AQAB</Exponent><P>33li38h5n1siKeu3LvsLF+JiytDAVLdRO6BkZ7btGcElTs/iir8ZIJFs2VvB5ELovk0iSBKZTNN/In4asEehaw==</P><Q>siDRbd2xQOyZnIOhfkjAt4QUNhskKRV56OkTvEawxloXZOssh0BjzuW8UVbiM3OuqgXUZm64eJyVghJacWXNRQ==</Q><DP>aafRWG5HGlGTCHDabcrhRsyQ2sTxedwl+uPtNJ/m35tS0T02JVUOQzieVvGr7Qi5U2fSgnLQqeMAo8UtRxTGuw==</DP><DQ>LB724Pi9k/BG6D6ufKI2of4kJ3a3kTmRlcBNtB/qX2cnTkb2I5YuYqwC5j7YDpiD1fdVsf2Q0ccvPSWgo1/s5Q==</DQ><InverseQ>YnPBkCyRyRnVWJPaJg507VdJpEPPeacdJ0wUJfiJgOfJPo01JLOWtgPWVqpW+pDTouv77qn7N1sIRo+IiyzsRA==</InverseQ><D>KEOLvxzr82Zm+qzXtJNd6sn43c8kpm+l12s5AsjD95soZPzAZLoIgAVStKD9hABASd3J7DghuuFkXCZHYQbsLLEeRY2TuuS/e6yyYV0+KNMRMrZAZ2DdWOdsfUmbQMiJNVTLmkwBgFLaFmcZ/DmPBMUYuJmFO9Tg6vEDCg45b5E=</D></RSAKeyValue>";
var dataByte = Convert.FromBase64String("YfFFh5cquMP8qMls+7e1WF23addkU9vLuK4W5U8R0zclbw1SJk+8QC8K17dgN5mXFKEifj669H7fAKfUphWDLqDSsodfFcGbElOC5J9ybf30Or1NPH9g2OTjIAaWluBjAYpg/qEtvJkvzwFpeqWoVOfNdwn51igCbPZKKi04tw==");
rsa.FromXmlString(privatekey);
//var dataNotWorkingByte = Convert.FromBase64String("YfFFh5cquMP8qMls+7e1WF23addkU9vLuK4W5U8R0zclbw1SJk+8QC8K17dgN5mXFKEifj669H7fAKfUphWDLqDSsodfFcGbElOC5J9ybf30Or1NPH9g2OTjIAaWluBjAYpg/qEtvJkvzwFpeqWoVOfNdwn51igCbPZKKi04tw==");
var dataWorkingByte = Convert.FromBase64String("I2S7t/BHFZo7JZFVUCHSSckHcWEASiZNjHmR5O1495y5RIej7d8PfnXjQYUrN1U352RAo373XSDuVhhFwS9XijI6lMJH/rlMjFIXOegcOrBJI406ZpF0dmbBc+Wq4rmBngT6fzrqvLLhijfRUq1m+K2dEfOkOuzRCsXKRKCOZBA=");
var descryptedString = Encoding.ASCII.GetString(decryptedByte);
抛出以下异常
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll
Additional information: Bad Data.
编辑
只有在尝试使用双填充 (==) 解密文本时才会发生异常。提供了有效和无效文本的样本
var dataNotWorkingByte = Convert.FromBase64String("YfFFh5cquMP8qMls+7e1WF23addkU9vLuK4W5U8R0zclbw1SJk+8QC8K17dgN5mXFKEifj669H7fAKfUphWDLqDSsodfFcGbElOC5J9ybf30Or1NPH9g2OTjIAaWluBjAYpg/qEtvJkvzwFpeqWoVOfNdwn51igCbPZKKi04tw==");
var dataWorkingByte = Convert.FromBase64String("I2S7t/BHFZo7JZFVUCHSSckHcWEASiZNjHmR5O1495y5RIej7d8PfnXjQYUrN1U352RAo373XSDuVhhFwS9XijI6lMJH/rlMjFIXOegcOrBJI406ZpF0dmbBc+Wq4rmBngT6fzrqvLLhijfRUq1m+K2dEfOkOuzRCsXKRKCOZBA=");
而且数据的长度很小,不超过50个字符。
提前感谢您的宝贵时间!!
Exception happens only when attempting to decrypt text with double padding (==). Have provided samples of working and not working texts
Base64 填充改变了 byte[]
的长度,但 RSA 解密需要 byte[]
,其位长与密钥长度相同。
Base64 值
I2S7t/BHFZo7JZFVUCHSSckHcWEASiZNjHmR5O1495y5RIej7d8PfnXjQYUrN1U
352RAo373XSDuVhhFwS9XijI6lMJH/rlMjFIXOegcOrBJI406ZpF0dmbBc+Wq4r
mBngT6fzrqvLLhijfRUq1m+K2dEfOkOuzRCsXKRKCOZBA=
等于十六进制值
2364BBB7F047159A3B2591555021D249C9077161004A264D8C7991E4ED78F79C
B94487A3EDDF0F7E75E341852B375537E76440A37EF75D20EE561845C12F578A
323A94C247FEB94C8C521739E81C3AB049238D3A6691747666C173E5AAE2B981
9E04FA7F3AEABCB2E18A37D152AD66F8AD9D11F3A43AECD10AC5CA44A08E6410
这是 128 字节,即 1024 位,因此对于 RSA 解密来说,它不是一个完全错误的值。
另一方面,Base64 值
YfFFh5cquMP8qMls+7e1WF23addkU9vLuK4W5U8R0zclbw1SJk+8QC8K17dgN5m
XFKEifj669H7fAKfUphWDLqDSsodfFcGbElOC5J9ybf30Or1NPH9g2OTjIAaWlu
BjAYpg/qEtvJkvzwFpeqWoVOfNdwn51igCbPZKKi04tw==
等于十六进制值
61F14587972AB8C3FCA8C96CFBB7B5585DB769D76453DBCBB8AE16E54F11D337
256F0D52264FBC402F0AD7B76037999714A1227E3EBAF47EDF00A7D4A615832E
A0D2B2875F15C19B125382E49F726DFDF43ABD4D3C7F60D8E4E320069696E063
018A60FEA12DBC992FCF01697AA5A854E7CD7709F9D628026CF64A2A2D38B7
只有127字节。因此,在 1024 位密钥下解密不是有效的 blob。
真正的错误在于产生那个价值的东西。 RSA 加密总是产生密钥大小的答案:
RFC 3447, section 7 显示 OAEP 和 PKCS1-v1_5 都以 I2OSP (c, k)
结尾,其中“k 表示模数 n 的八位字节长度”。
I2OSP(x, xLen) 有警告说明:“请注意,如果 x 小于 256^(xLen-1),一位或多位前导数字将为零”。我唯一的猜测(除了损坏的数据)是你在某个地方遇到了一个系统,该系统删除了不相关的前导零。
RSA 解密在 Windows Server 2012 和 Windows 10 中失败,但在 Windows 7 中就像一个魅力。已放置在代码下方,其中还包含使用的相关密钥.
var rsa = new RSACryptoServiceProvider(1024);
const string privatekey = "<RSAKeyValue><Modulus>m38MvfdL8epfiYvgw6ylMB7zPAKSStQLklCQEOworQwJxw39bXdnm4717BUm5700vZCbG9GBgjwHUxmrFrFsSJxMIdYhokT/DuYOC9Nsf2fiX5Uq+WtBfbSneEr6yVk2VB1fgba6XxoK9O3CKzieR1XxPEofxGRjrXzMe8nIMNc=</Modulus><Exponent>AQAB</Exponent><P>33li38h5n1siKeu3LvsLF+JiytDAVLdRO6BkZ7btGcElTs/iir8ZIJFs2VvB5ELovk0iSBKZTNN/In4asEehaw==</P><Q>siDRbd2xQOyZnIOhfkjAt4QUNhskKRV56OkTvEawxloXZOssh0BjzuW8UVbiM3OuqgXUZm64eJyVghJacWXNRQ==</Q><DP>aafRWG5HGlGTCHDabcrhRsyQ2sTxedwl+uPtNJ/m35tS0T02JVUOQzieVvGr7Qi5U2fSgnLQqeMAo8UtRxTGuw==</DP><DQ>LB724Pi9k/BG6D6ufKI2of4kJ3a3kTmRlcBNtB/qX2cnTkb2I5YuYqwC5j7YDpiD1fdVsf2Q0ccvPSWgo1/s5Q==</DQ><InverseQ>YnPBkCyRyRnVWJPaJg507VdJpEPPeacdJ0wUJfiJgOfJPo01JLOWtgPWVqpW+pDTouv77qn7N1sIRo+IiyzsRA==</InverseQ><D>KEOLvxzr82Zm+qzXtJNd6sn43c8kpm+l12s5AsjD95soZPzAZLoIgAVStKD9hABASd3J7DghuuFkXCZHYQbsLLEeRY2TuuS/e6yyYV0+KNMRMrZAZ2DdWOdsfUmbQMiJNVTLmkwBgFLaFmcZ/DmPBMUYuJmFO9Tg6vEDCg45b5E=</D></RSAKeyValue>";
var dataByte = Convert.FromBase64String("YfFFh5cquMP8qMls+7e1WF23addkU9vLuK4W5U8R0zclbw1SJk+8QC8K17dgN5mXFKEifj669H7fAKfUphWDLqDSsodfFcGbElOC5J9ybf30Or1NPH9g2OTjIAaWluBjAYpg/qEtvJkvzwFpeqWoVOfNdwn51igCbPZKKi04tw==");
rsa.FromXmlString(privatekey);
//var dataNotWorkingByte = Convert.FromBase64String("YfFFh5cquMP8qMls+7e1WF23addkU9vLuK4W5U8R0zclbw1SJk+8QC8K17dgN5mXFKEifj669H7fAKfUphWDLqDSsodfFcGbElOC5J9ybf30Or1NPH9g2OTjIAaWluBjAYpg/qEtvJkvzwFpeqWoVOfNdwn51igCbPZKKi04tw==");
var dataWorkingByte = Convert.FromBase64String("I2S7t/BHFZo7JZFVUCHSSckHcWEASiZNjHmR5O1495y5RIej7d8PfnXjQYUrN1U352RAo373XSDuVhhFwS9XijI6lMJH/rlMjFIXOegcOrBJI406ZpF0dmbBc+Wq4rmBngT6fzrqvLLhijfRUq1m+K2dEfOkOuzRCsXKRKCOZBA=");
var descryptedString = Encoding.ASCII.GetString(decryptedByte);
抛出以下异常
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll Additional information: Bad Data.
编辑 只有在尝试使用双填充 (==) 解密文本时才会发生异常。提供了有效和无效文本的样本
var dataNotWorkingByte = Convert.FromBase64String("YfFFh5cquMP8qMls+7e1WF23addkU9vLuK4W5U8R0zclbw1SJk+8QC8K17dgN5mXFKEifj669H7fAKfUphWDLqDSsodfFcGbElOC5J9ybf30Or1NPH9g2OTjIAaWluBjAYpg/qEtvJkvzwFpeqWoVOfNdwn51igCbPZKKi04tw==");
var dataWorkingByte = Convert.FromBase64String("I2S7t/BHFZo7JZFVUCHSSckHcWEASiZNjHmR5O1495y5RIej7d8PfnXjQYUrN1U352RAo373XSDuVhhFwS9XijI6lMJH/rlMjFIXOegcOrBJI406ZpF0dmbBc+Wq4rmBngT6fzrqvLLhijfRUq1m+K2dEfOkOuzRCsXKRKCOZBA=");
而且数据的长度很小,不超过50个字符。
提前感谢您的宝贵时间!!
Exception happens only when attempting to decrypt text with double padding (==). Have provided samples of working and not working texts
Base64 填充改变了 byte[]
的长度,但 RSA 解密需要 byte[]
,其位长与密钥长度相同。
Base64 值
I2S7t/BHFZo7JZFVUCHSSckHcWEASiZNjHmR5O1495y5RIej7d8PfnXjQYUrN1U
352RAo373XSDuVhhFwS9XijI6lMJH/rlMjFIXOegcOrBJI406ZpF0dmbBc+Wq4r
mBngT6fzrqvLLhijfRUq1m+K2dEfOkOuzRCsXKRKCOZBA=
等于十六进制值
2364BBB7F047159A3B2591555021D249C9077161004A264D8C7991E4ED78F79C
B94487A3EDDF0F7E75E341852B375537E76440A37EF75D20EE561845C12F578A
323A94C247FEB94C8C521739E81C3AB049238D3A6691747666C173E5AAE2B981
9E04FA7F3AEABCB2E18A37D152AD66F8AD9D11F3A43AECD10AC5CA44A08E6410
这是 128 字节,即 1024 位,因此对于 RSA 解密来说,它不是一个完全错误的值。
另一方面,Base64 值
YfFFh5cquMP8qMls+7e1WF23addkU9vLuK4W5U8R0zclbw1SJk+8QC8K17dgN5m
XFKEifj669H7fAKfUphWDLqDSsodfFcGbElOC5J9ybf30Or1NPH9g2OTjIAaWlu
BjAYpg/qEtvJkvzwFpeqWoVOfNdwn51igCbPZKKi04tw==
等于十六进制值
61F14587972AB8C3FCA8C96CFBB7B5585DB769D76453DBCBB8AE16E54F11D337
256F0D52264FBC402F0AD7B76037999714A1227E3EBAF47EDF00A7D4A615832E
A0D2B2875F15C19B125382E49F726DFDF43ABD4D3C7F60D8E4E320069696E063
018A60FEA12DBC992FCF01697AA5A854E7CD7709F9D628026CF64A2A2D38B7
只有127字节。因此,在 1024 位密钥下解密不是有效的 blob。
真正的错误在于产生那个价值的东西。 RSA 加密总是产生密钥大小的答案:
RFC 3447, section 7 显示 OAEP 和 PKCS1-v1_5 都以 I2OSP (c, k)
结尾,其中“k 表示模数 n 的八位字节长度”。
I2OSP(x, xLen) 有警告说明:“请注意,如果 x 小于 256^(xLen-1),一位或多位前导数字将为零”。我唯一的猜测(除了损坏的数据)是你在某个地方遇到了一个系统,该系统删除了不相关的前导零。