crypto api和.net的rsa互通性分析

来源:互联网 发布:淘宝网苹果版下载安装 编辑:程序博客网 时间:2024/06/01 19:02

crypto api和.net的rsa加密解密不能互通,实在想不通。

最初以为是padding不一致的问题,例如PKCS#1 1.5还是OAEP 。最后微软网站上看到了答案,原来.net自己偷偷对数据进行了翻转!

怪不得rsa不通呢,原来其他接口的数据,到这里数据还需要翻转啊!

.net上的说明:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx

 

The RSACryptoServiceProvider supports key lengths from 384 bits to 16384 bits in increments of 8 bits if you have the Microsoft Enhanced Cryptographic Provider installed. It supports key lengths from 384 bits to 512 bits in increments of 8 bits if you have the Microsoft Base Cryptographic Provider installed.

Interoperation with the Microsoft Cryptographic API (CAPI)

Unlike the RSA implementation in unmanaged CAPI, the RSACryptoServiceProvider class reverses the order of an encrypted array of bytes after encryption and before decryption. By default, data encrypted by the RSACryptoServiceProvider class cannot be decrypted by the CAPI CryptDecrypt function and data encrypted by the CAPI CryptEncrypt method cannot be decrypted by the RSACryptoServiceProvider class.

If you do not compensate for the reverse ordering when interoperating between APIs, the RSACryptoServiceProvider class throws a CryptographicException.

To interoperate with CAPI, you must manually reverse the order of encrypted bytes before the encrypted data interoperates with another API. You can easily reverse the order of a managed byte array by calling the Array..::.Reverse method.

原创粉丝点击