RSA: How to get ciphertext in decimal from an encrypted base64? I was given the ciphertext as base64 just like that:

ADknbLUTEh1NBT80tH57H7B44DZOWao67c8I8+AQwuSwnUUxiJUidAedSaOGceVjC4tVF3sfnj0b
1akQiMMXO7YPj+0MFFN09WqE0Jz36ZtY695Esp3DoErjnK8=


How can I convert it to decimal?
Base 64 is an encoding of bytes, not numbers. Of course, bytes can be used to represent a number. The output of RSA encryption consists of bytes (or octets, the term used in the RSA PKCS#1 stancard). So the base 64 decoding has come after RSA encryption in your protocol. This all means that the conversion is a two step process.

How to decode base 64 to bytes is not a question we're going to answer here, there are plenty of libraries that will do this for you.



Before the conversion happens it is important to validate that the value is precisely as long as the modulus (the key size), when encoded to a minimal number of unsigned byte. The real second operation, both for RSA that uses OAEP- and RSA v1.5 padding, is the conversion from bytes to the integer. This operation has a name: OS2IP, or the Octet String to (2, gedit?) Integer primitive. Very basically, this interprets the bytes as an unsigned, big endian integer. 


    a.  Convert the ciphertext C to an integer ciphertext
        representative c (see Section 4.2):

         c = OS2IP (C).



This points to the following text in Section 4.2 of PKCS#1 v2.2:


  OS2IP converts an octet string to a nonnegative integer.

   OS2IP (X)

   Input:  X octet string to be converted

   Output:  x corresponding nonnegative integer

   Steps:

      1.  Let X_1 X_2 ... X_xLen be the octets of X from first to last,
          and let x_(xLen-i) be the integer value of the octet X_i for 1
          &lt;= i &lt;= xLen.

      2.  Let x = x_(xLen-1) 256^(xLen-1) + x_(xLen-2) 256^(xLen-2) +
          ...  + x_1 256 + x_0.

      3.  Output x.



Do not try and implement this as a mathematical function using multiplication and calculating things to the power of two; it is just a mathematical description of the decoding of an unsigned integer.

After the trick is to find a good big integer library for your language and use the right method to convert or at least interpret. In Java it is simply new BigInteger(1, &lt;the byte array&gt;) where the 1 is the sign bit, which is of course always positive (or zero). Java copies the value represented by the bytes to the internal interpretation but other languages may leave the number where it is and simply reference it as an unsigned big integer.



Beware that other languages such as C# are commonly little endian. You may need to find the right method that explicitly mentions the endianess. Otherwise you may need to reverse the location of the bytes within the representation before parsing the bytes that now represent a little endian integer.