question

edpdev avatar image
edpdev asked prasath published

C# Credit card encryption example

Can anyone point me in the right direction for an example of encrypting card data in C#? Using the key from CDN. I've looked at the Java example but still unclear on getting the modulus and exponent. Also, encrypting using the newly created RSA key.

Any help is greatly appreciated!

e-commerce api
10 |2000

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

hildamaiorano avatar image
hildamaiorano answered edpdev commented

I’m not sure about the specific details of your request. However, you may want to check out the Clover developer community page for more information on how to encrypt card data in C#

1 comment
10 |2000

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

edpdev avatar image edpdev commented ·

Specifically, how do I obtain the modulus and exponent from the parsed Base64 public key using C#/.NET 4.6.1?

// This method returns the CDN keys, deserializes JSON and assigns key values to a class. No problem here.

CDNKeys keys = GetCDNKeys();

// This method creates a byte[512] array from the dev public key. No problem here.

byte[] keybytes = Convert.FromBase64String(keys.TA_PUBLIC_KEY_DEV);

How do I obtain the modulus and exponent from the byte[512] array?

I have a method that converts bintohexstring and the converted data seems to match some of the php posts on this community forum. The same post says the first 256 characters represent the modulus and the second 256 characters represent the exponent. Either way, using .NET Cryptography or BouncyCastle I'm not doing something correctly with the byte[512] array.

Ultimately, I want to use a RSACryptoServiceProvider, load RSAParameters with valid modulus and exponent to encrypt card data.

Any help is appreciated!

0 Likes 0 ·
parquet76 avatar image
parquet76 answered parquet76 edited

You said you looked at the java example, what example? https://clover.app.box.com/s/rz18bni3bpmdrc8wc92xm4w8h9grrtty

You should be able to convert that over to c# pretty easily (or maybe ask ChatGPT to), the code for extracting the modulus and exponent is pretty clear:

    final BigInteger modulus1 = new BigInteger(ArrayUtils.addAll(unsignedPrefix, Arrays.copyOfRange(key, 0, 256)));
    final BigInteger exponent1 = new BigInteger(Arrays.copyOfRange(key, 256, 512));


6 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.

edpdev avatar image edpdev commented ·

Thank you @parguet76. Your Java example link was not the example I was looking at.

I was able to create a C# RSACryptoServiceProvider and import parameters (modulus and exponent) obtained from the CDN Key. Encrypt card and pass it in on card token request (encrypted_pan). Now I'm getting the following error:

{"message": "400 Bad Request",

"error": {

"type":"invalid_request_error",

"code":"invalid_request",

"message":"An error occurred while processing the token request.",

"param":"failure response from transarmor provider","gatewayResponseCode":"016"}}"}


The Java example does not pass a transarmor_key_id and neither am I. Do I need to or am I seeing an encryption issue?

Thanks!


0 Likes 0 ·
parquet76 avatar image parquet76 edpdev commented ·

Not sure what you are doing wrong. But, if you didn't have the correct example maybe you haven't seen the docs? https://docs.clover.com/docs/ecommerce-generating-a-card-token.

0 Likes 0 ·
edpdev avatar image edpdev parquet76 commented ·
Seen it but there are inconsistencies in the documentation and the Java example.
0 Likes 0 ·
Show more comments
marcusdoyle avatar image
marcusdoyle answered

@ only up

Hello,

Encrypting card data using RSA encryption in C# can be achieved using the RSACryptoServiceProvider class. Here's an example that demonstrates how to encrypt data using an RSA key:

csharp
using System;
using System.Security.Cryptography;
using System.Text;


class Program
{
    static void Main()
    {
        try
        {
            // Replace with your own modulus and exponent values
            string modulus = "YOUR_MODULUS";
            string exponent = "YOUR_EXPONENT";


            // Create an instance of the RSA provider
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();


            // Import the modulus and exponent into the RSA provider
            rsa.ImportParameters(
                new RSAParameters
                {
                    Modulus = Convert.FromBase64String(modulus),
                    Exponent = Convert.FromBase64String(exponent)
                }
            );


            // Encrypt the card data
            string cardData = "CARD_DATA_TO_ENCRYPT";
            byte[] dataToEncrypt = Encoding.UTF8.GetBytes(cardData);
            byte[] encryptedData = rsa.Encrypt(dataToEncrypt, false);


            // Convert the encrypted data to a base64 string
            string encryptedBase64 = Convert.ToBase64String(encryptedData);


            Console.WriteLine("Encrypted Data: " + encryptedBase64);
        }
        catch (Exception e)
        {
            Console.WriteLine("Encryption Failed: " + e.Message);
        }
    }
}

In this example, you need to replace "YOUR_MODULUS" and "YOUR_EXPONENT" with the actual values you obtain from the CDN. These values typically come as base64-encoded strings.

Please note that this example assumes you have the correct modulus and exponent values required for encryption. Make sure you obtain the correct values from the appropriate source to ensure the encryption is performed correctly.

10 |2000

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

prasath avatar image
prasath answered prasath published


Hello All,

In C# - Use Bouncy Castle Library Package to Encryption and refer the below code to get the encrypted string.

byte[] input = Encoding.UTF8.GetBytes(tokenModelDto.Prefix + tokenModelDto.CardNumber);


var cipher = CipherUtilities.GetCipher("RSA/None/OAEPWithSHA1AndMGF1Padding");

cipher.Init(true, new RsaKeyParameters(false, new Org.BouncyCastle.Math.BigInteger("yourModulus"), new Org.BouncyCastle.Math.Integer("yourexponent")));


byte[] cipherText = cipher.DoFinal(input);


encryptedData = Convert.ToBase64String(cipherText);

10 |2000

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

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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