Decrypting Data


RSA

Triangle uses 2048-bit RSA to encrypt the card information that it scans. Each application has its own set of unique keys. In order to use the information you obtain from the API, you need to decrypt the information and pass it to your payment processor to transfer the funds. To decrypt the information, look up the private keys associated to your application in the Applications section of the keys page.

Most major frameworks have a built-in implementation of RSA that you can use to decrypt the information. Below is some sample code in different languages for your some copy/paste pleasure.

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

namespace Triangle
{
    class Program
    {
        static void Main(string[] args)
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(dwKeySize: 2048))
            {
                // Import parameters unique to your application (you can get these from the keys page)
                rsa.ImportParameters(new RSAParameters()
                {
                    Modulus = Convert.FromBase64String("Obtain from the keys page"),
                    D = Convert.FromBase64String("Obtain from the keys page"),
                    Exponent = Convert.FromBase64String("Obtain from the keys page"),
                    P = Convert.FromBase64String("Obtain from the keys page"),
                    DP = Convert.FromBase64String("Obtain from the keys page"),
                    DQ = Convert.FromBase64String("Obtain from the keys page"),
                    InverseQ = Convert.FromBase64String("Obtain from the keys page"),
                    Q = Convert.FromBase64String("Obtain from the keys page")
                });

                // We don't want the keys to be persisted anywhere
                rsa.PersistKeyInCsp = false;

                byte[] encryptedValue = Convert.FromBase64String("encrypted value obtained from the APIs");
                byte[] decryptedValue = rsa.Decrypt(encryptedValue, fOAEP: false);

                // During encryption, Triangle uses a UTF8 encoding to translate the plaintext to bytes
                // Convert the bytes to a readable string
                string decryptedString = Encoding.UTF8.GetString(decryptedValue);

                // Do something with decrypted string...
            }

        }
    }
}
                
import java.util.Base64;

import javax.crypto.Cipher;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

public class RSA
{
    public static final String RSAKeyFactory = "RSA";
    public static final String RSAKeyAlgorithm = "RSA/ECB/PKCS1Padding";
    public static final String UTF_8 = "UTF-8";
    public static final int BASE_64_FLAG = Base64.NO_WRAP;

    private PrivateKey privateKey;

    public RSA(String modulus, String d) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException
    {
        KeyFactory keyFactory = KeyFactory.getInstance(RSAKeyFactory);
        privateKey = keyFactory.generatePrivate(new RSAPrivateKeySpec(
                new BigInteger(1, Base64.decode(modulus, BASE_64_FLAG)),
                new BigInteger(1, Base64.decode(d, BASE_64_FLAG))));
    }

    public String decrypt(String cipherText) throws Exception
    {
        byte[] cipherBytes = Base64.decode(cipherText, BASE_64_FLAG);
        Cipher cipher = Cipher.getInstance(RSAKeyAlgorithm);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] plainData = cipher.doFinal(cipherBytes);
        return new String(plainData, UTF_8);
    }
}
        

Posts

  Share on Facebook   Tweet