Java RSA Encrypt using .NET XML Key File - need help

Posted by badMonkey on Stack Overflow See other posts from Stack Overflow or by badMonkey
Published on 2010-04-22T14:14:44Z Indexed on 2010/04/23 13:13 UTC
Read the original article Hit count: 204

Filed under:
|
|
|

In .net I have generated the following public key file:

<RSAKeyValue>
<Modulus>xTSiS4+I/x9awUXcF66Ffw7tracsQfGCn6g6k/hGkLquHYMFTCYk4mOB5NwLwqczwvl8HkQfDShGcvrm47XHKUzA8iadWdA5n4toBECzRxiCWCHm1KEg59LUD3fxTG5ogGiNxDj9wSguCIzFdUxBYq5ot2J4iLgGu0qShml5vwk=</Modulus>
<Exponent>AQAB</Exponent>

.NET is happy to encrypt using it's normal methods.

I am trying to use this key to encode a string in Java and am running into an Arithmetic problem (exception) when I attempt to encrypt the string.

The following is the code I am using to encrypt:

byte[] modulusBytes = Base64.decode(this.getString(R.string.public_key_modulus));
byte[] exponentBytes = Base64.decode(this.getString(R.string.public_key_exponent));
BigInteger modulus = new BigInteger( modulusBytes );                
BigInteger exponent = new BigInteger( exponentBytes);

RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(rsaPubKey);

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);

byte[] cipherData = cipher.doFinal( new String("big kitty dancing").getBytes() );    

It is the final line in the code block that fails.

I have looked at numerous examples and this is the best I could come up with. If it is not obvious, the R.string.public_key_modulus is a copy/paste of the text in the Modulus element, same applies for exponent.

© Stack Overflow or respective owner

Related posts about java

Related posts about rsa