Home » Java » android – .net RSACryptoServiceProvider.Encrypt analogy in java-Exceptionshub

android – .net RSACryptoServiceProvider.Encrypt analogy in java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I tried to move some code from .net to JAVA. And I stacked on a simple method:

public static byte[] EncryptData(byte[] plainBytes)
{
 RSACryptoServiceProvider publicKeyCrypto = (RSACryptoServiceProvider)platformCertificate.PublicKey.Key;
 var encrypted = publicKeyCrypto.Encrypt(plainBytes, false);
 return encrypted;
}

In JAVA I used:

RSAPublicKey rsaKey = (RSAPublicKey)platformCertificate.getPublicKey();

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
                cipher.init(Cipher.ENCRYPT_MODE, rsaKey);
                byte[] cipherData = cipher.doFinal(plainBytes);

But the java code result doesn’t decrypt correctly on the server side.

Could somebody please provide some information about what I am doing wrong?

How to&Answers: