0

We have generated RSA public key by our java program to encrypt the data in C++ before transmitting it but when we use this key to encrypt the data using Microsoft encryption API, it doesn't accepts this key.

Do anyone has any ideas on this?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Nisha
  • 11
  • Perhaps the M$ API requires a binary key, and the key you're giving it is a base64-encoded key block. Or vise-versa. What's the error message it's giving? – amphetamachine Jan 27 '10 at 07:19
  • Your best chance to get help would be to show us a complete example - including data - of the code that fails to accept the key. – Rasmus Faber Jan 27 '10 at 19:14

2 Answers2

1

Microsoft code has some intrinsic limitations on what RSA keys it can use: it requires the public key length (the modulus bit length) to be a multiple of 16, and the public exponent must fit in a 32-bit unsigned integer.

However, most RSA keys fit in those constraints and the usual suspects are encoding/decoding issues. Java tends to use big-endian everywhere, because:

  • that's what is mandated by ASN.1-based encoding rules;
  • Java is from Sun who hast long dealt with big-endian hardware (68020, Sparc).

On the other hand, Microsoft's CryptoAPI wants little-endian. You may have used your public modulus in the wrong order.

Thomas Pornin
  • 72,986
  • 14
  • 147
  • 189
0

In my experience the serialization of an RSA key is different between Java and MS.

But, what I did was to use the Bouncy Castle API to do this, as they have a Java and .NET version of their library.

For more options you can look at this question: RSA: How to generate private key in java and use it in C#?

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166