5

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?

2 Answers2

4

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
bartonjs
  • 30,352
  • 2
  • 71
  • 111
  • 1
    which method did you use to convert that base64 string to final decimal? I understand what did you say but I can not reach the same result @bartonjs – Juan Francisco Caballero Sep 27 '16 at 19:45
  • 1
    @JuanFranciscoCaballero I used http://tomeko.net/online_tools/base64.php?lang=en for Base64->Hex and http://www.mobilefish.com/services/big_number/big_number.php for Hex->BigInteger. Be careful not to convert each byte to an integer, since that's not an accurate conversion. – bartonjs Sep 27 '16 at 20:41
0

Modulus is a byte[].

See the RSAParameters structure for more details.

toadflakz
  • 7,764
  • 1
  • 27
  • 40