I have the public key of the RSA algorithm, it looks like this:

<RSAKeyValue><Modulus>1p3TaiUejpOPpeiaElNa1TWcoLyGcExLNuQC/6+jzqLtX5y8S5QSI5gKhLAzZhxX</Modulus><Exponent>AQAB</Exponent></RSAKeyValue> 1p3TaiUejpOPpeiaElNa1TWcoLyGcExLNuQC/6+jzqLtX5y8S5QSI5gKhLAzZhxX AQAB 

The first term is a key, the second is an open exponent, it is obvious that they are encoded in base64 .

Question. How to extract the numbers?

  • 2
    There are simply bytes corresponding to the number: decoding the exponent gives 0x01 0x00 0x01, which corresponds to 65537. Only it is not clear how the bytes are stored: first low or high first ... - Vladimir Martyanov
  • Those. if byte is 247 then this is the number 247? And how to write them in a row ?? - Dagger
  • 2
    If the byte is 247 - there is 247. But if your number is written in several bytes, everything becomes more complicated. The same 65537 is represented by 3 bytes: 0x01, 0x00, 0x01 and it turns out like this: 1 * 256 ^ 0 + 0 * 256 ^ 1 + 1 * 256 ^ 2 = 65537. Accordingly, you need to know which bytes are written first: high or low. - Vladimir Martyanov
  • Thank you very much. - Dagger
  • 2
    @ Vladimir Martianov Please post your comments as an answer. - Nicolas Chabanovsky

2 answers 2

The specified example is an XML file. You can parse it using System.Xml.Linq , where you get two strings, which are turned into a set of bytes using System.Convert.FromBase64String

    Parameters can be decoded directly (like base64), or via import into RSA CSP (since your xml is the result of the export). The byte order is big endian . You can convert to a number via BigInteger co with a change of order.

     using System; using System.Linq; using System.Numerics; using System.Security.Cryptography; class Program { static BigInteger BigEndianToBigInteger(byte[] data) { return new BigInteger( data .Reverse() // смена порядка .Concat(new byte[] { 0 }) // принудительный 0 в начале для получения положительного числа .ToArray() ); } static void Main(string[] args) { var xmlString = @"<RSAKeyValue><Modulus>1p3TaiUejpOPpeiaElNa1TWcoLyGcExLNuQC/6+jzqLtX5y8S5QSI5gKhLAzZhxX</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; var provider = new RSACryptoServiceProvider(); provider.FromXmlString(xmlString); RSAParameters parameters = provider.ExportParameters(false); Console.WriteLine(BigEndianToBigInteger(parameters.Modulus)); Console.WriteLine(BigEndianToBigInteger(parameters.Exponent)); } } 

    The handwritten RSA implementation is in enSO , I took the code for the conversion from there.