question

jonathan avatar image
jonathan asked jonathan commented

Trouble converting base 10 string array of bytes

I am working with the payment API. I am trying to create the RSA Public Key. I have retrieved the modulus and exponent from the call to /v2/merchant/{mId}/pay/key.

According to the documentation, the modulus and exponent are base10 strings. So I need to convert that to a byte array. I am having trouble figuring out how to do this. Is there a code example anywhere that reflects this?

I tried converting the string to a biginteger and then retrieving bytes, but that does not seem to work. Any ideas on this?

10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jacob Abrams avatar image
Jacob Abrams answered jonathan commented

The example code in this FAQ should help: https://docs.clover.com/faq#how-do-i-... You can simply pass the string to a new BigInteger like so: new BigInteger(modulus)

2 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jonathan avatar image jonathan commented ·

I am using C#. These two generate different values. I am not sure why, but the bouncy castle version allowed me to generate the public key. My guess is C# BigInteger.Parse is expecting a base64 string

public byte[] GetBytesFromBase10String(string base10)
        {
            var b = BigInteger.Parse(base10);
            return b.ToByteArray();
        }

        public byte[] GetBytesFromBase10StringWithBouncyCastle(string base10)
        {
            var bigInt = new Org.BouncyCastle.Math.BigInteger(base10, 10);
            return bigInt.ToByteArray
0 Likes 0 ·
jonathan avatar image jonathan commented ·

Verified it worked. I was able to submit a payment using the Bouncy Castle method.

0 Likes 0 ·
jonathan avatar image
jonathan answered

I think I have it, but I am not sure. I was able to use BouncyCastle to convert the string to big integer. Now, just need to see if this works:

var modulus = new Org.BouncyCastle.Math.BigInteger(modulusBase10, 10);
            var exponent = new Org.BouncyCastle.Math.BigInteger(exponentBase10, 10);

            RSACryptoServiceProvider rsaObj = new RSACryptoServiceProvider();
            //Create a new instance of the RSAParameters structure.
            RSAParameters rsaPars = new RSAParameters();
            rsaPars.Modulus = modulus.ToByteArray();
            rsaPars.Exponent = exponent.ToByteArray();

            Console.WriteLine("MODULUS in STRING FORM:>" + Convert.ToBase64String(rsaPars.Modulus));
            //Import key parameters into RSA.

            rsaObj.ImportParameters(rsaPars);
            return rsaObj.ExportCspBlob(false);
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community