Translating C# RSACryptoServiceProvider into JAVA Code

前端 未结 3 882
醉话见心
醉话见心 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:18

    Using RSA/ECB/OAEPWithSHA-1AndMGF1Padding seems to be the solution for this issue.

    Don't forget these replacements.

    Encoding to Base64 Use System.Convert to convert the input to Base64.

    Replace + by - and / by _. Example: Foo+bar/=== becomes Foo-bar_===.

    Replace any number of = at the end of the string, with an integer denoting how many they were. Example: Foo-bar_=== becomes Foo-bar_3.

    Decoding from Base64 Replace the digit at the end of the string by the same number of = signs. Example: Foo-bar_3 becomes Foo-bar_===.

    Replace - by + and _ by /. Example: Foo-bar_=== becomes Foo+bar/===.

    Use System.Convert to decode the preprocessed input from Base64.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-05 20:36

    Found the solution.

    String modulusString = "hm2oRCtP6usJKYpq7o1K20uUuL11j5xRrbV4FCQhn/JeXLT21laKK9901P69YUS3bLo64x8G1PkCfRtjbbZCIaa1Ci/BCQX8nF2kZVfrPyzcmeAkq4wsDthuZ+jPInknzUI3TQPAzdj6gim97E731i6WP0MHFqW6ODeQ6Dsp8pc=";
    String publicExponentString = "AQAB";
    
    byte[] modulusBytes = Base64.decodeBase64(modulusString);
    byte[] exponentBytes = Base64.decodeBase64(publicExponentString);
    BigInteger modulus = new BigInteger(1, modulusBytes);
    BigInteger publicExponent = new BigInteger(1, exponentBytes);
    
    RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, publicExponent);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PublicKey pubKey = fact.generatePublic(rsaPubKey);
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    
    byte[] plainBytes = clearTextPassword.getBytes("UTF-16LE");
    byte[] cipherData = cipher.doFinal(plainBytes);
    String encryptedStringBase64 = Base64.encodeBase64String(cipherData);
    
    0 讨论(0)
提交回复
热议问题