Juan Francisco Caballero - 10 months ago 119

C# Question

I have the public key generated in c# with RSACryptoServiceProvider:

`<RSAKeyValue>`

<Modulus>

4kKhD/FWAMtQTRifArfXjxZN+6bOXTkHrVpyz/1wODhSOBqDewoSOFAp5boBd3wFjXszHA+gpUxZNWHRTj898Q==

</Modulus>

<Exponent>

AQAB

</Exponent>

<RSAKeyValue>

Those parameters are generated in a RSA variable initialized on 512 bits

`new RSACryptoServiceProvider(512)`

Now, I need to use these (modulus and exponent) to encrypt some data, but in groovy (groovyscript in a SoapUI Test).

In groovy, I was testing RSA encrypt, and for its public key, it gets modulus and exponent with only decimal number. The Modulus above looks like a base64 string , but when I tried to decode in groovy, it gets some special characters, the code I use for that is

`byte[] decoded = encoded.decodeBase64()`

string s == new String(decoded)

what I finally need is know how to use the modulus and exponent obtained in c# to encrypt some data in groovy.

Some help in how to do that?

Answer Source

In the XML representation here the numbers are Base64-encoded Big Endian byte array representations of numbers. The most sensible string format for them (other than Base64) is Hexadecimal, since that aligns at the byte boundaries; and you might have a Hex to BigInt decode routine.

Exponent

```
Base64: AQAB
Hexadecimal: 01 00 01
Decimal: 65537
```

Modulus

```
Base64:
4kKhD/FWAMtQTRifArfXjxZN+6bOXTkHrVpyz/1wODhSOBqDewoSOFAp5boBd3wFjXszHA+gpUxZNWHRTj898Q==
Hexadecimal:
E2 42 A1 0F F1 56 00 CB 50 4D 18 9F 02 B7 D7 8F
16 4D FB A6 CE 5D 39 07 AD 5A 72 CF FD 70 38 38
52 38 1A 83 7B 0A 12 38 50 29 E5 BA 01 77 7C 05
8D 7B 33 1C 0F A0 A5 4C 59 35 61 D1 4E 3F 3D F1
Decimal:
11 850 211 890 167 428 942 656 005 762 527 792
664 504 148 414 649 299 622 730 495 954 496 884
582 668 295 994 906 881 962 852 147 063 424 895
822 707 299 811 616 971 053 013 246 862 591 780
599 074 078 193
```