Translating C# RSACryptoServiceProvider into JAVA Code

前端 未结 3 890
醉话见心
醉话见心 2020-12-05 20:15

I was given this C# code written by the web service team that exposes some web service that I\'m planning to consume. My password needs to be encrypted with this code so tha

3条回答
  •  半阙折子戏
    2020-12-05 20:25

    According to MSDN docs on RSACryptoServiceProvider.Encrypt, when the second argument is false the cipher uses PKCS#1 v1.5 padding. So right off the bat your cipher spec is incorrect.

    Try RSA/ECB/PKCS1PADDING instead.

    You are converting your key material to much in your second code example and you corrupted it which ends up making your cipher think you have more key material than you actually have and makes your message too long (which is triggering your error) as well as unintelligible to the decryption cipher on the other end. Convert directly to byte arrays and pass those to BigInteger.

    String modulusString = "...";
    String publicExponentString = "...";
    
    byte[] mod = Base64.decodeBase64(modulusString);
    byte[] e = Base64.decodeBase64(publicExponentString);
    
    BigInteger modulus = new BigInteger(1, mod);
    BigInteger publicExponent = new BigInteger(1, e);
    

提交回复
热议问题