Why RSA encryption can return different results with C# and Java?

后端 未结 4 970
梦毁少年i
梦毁少年i 2021-01-14 03:39

I using:

  • c#: RSACryptoServiceProvider
  • JAVA: KeyFactory.getInstance(\"RSA\")+Cipher

I sending public key (exponent + mo

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 04:23

    RSA encryption is randomized. For a given public key and a given message, each attempt at encryption yields a distinct sequence of bytes. This is normal and expected; random bytes are injected as part of the padding phase, and not injecting random bytes would result in a weak encryption system. During decryption, the padding bytes are located and removed, and the original message is recovered unscathed.

    Hence it is expected that you will get distinct encrypted messages with Java and C#, but also if you run your Java or C# code twice.

提交回复
热议问题