Data encrypted in C# is 1 byte too long to be decrypted in Java

后端 未结 2 1313
自闭症患者
自闭症患者 2021-01-01 08:04

I have a server written in Java which sends converts its RSA key to the XML format used by .NET before sending it to the client:

public String getPublicKeyXM         


        
相关标签:
2条回答
  • 2021-01-01 08:25

    This will not fix the problem (I tested it to no avail), but I wanted to call to your attention that RSACryptoServiceProvider implements the IDisposable interface and therefore should be properly disposed of when complete. Your C# encrypt method can be written a bit better (and more concise!) as such:

        public static byte[] encrypt(string xmlKey, byte[] bytes)
        {
            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(xmlKey);
                return rsa.Encrypt(bytes, false);
            }
        }
    
    0 讨论(0)
  • 2021-01-01 08:45

    I found the problem. The BigInteger's toByteArray() function included a leading zero for some reason. I just removed the leading zeros from the array and it now works like a charm!

    0 讨论(0)
提交回复
热议问题