WIDGET DOCUMENTATION


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

namespace EP.Enterprise.Security.TH
{
    /// <summary>
    /// AES256 class uses the AES algorithm with a provided 256 bit key and a random 128 bit IV to meet PCI standards
    /// The IV is randomly generated before each encryption and encoded with the final encrypted string
    /// </summary>
    
    public class AES256
    {
        // Symmetric algorithm interface is used to store the AES service provider

        private SymmetricAlgorithm AESProvider;

        /// <summary>
        /// Constructor for AES class that takes a 32 byte ( 64 hex chars ) key
        /// </summary>
        /// <param name="HexKey">(Hex string length 64)</param>

        public AES256(string HexKey)
        {
            byte[] key = HexStringToByteArray(HexKey);

            // Throw error if key is not 256 bits
            if (key.Length != 32) throw new CryptographicException("Key must be 256 bits (32 bytes)");
            // Initialize AESProvider with AES algorithm service

            AESProvider = new AesCryptoServiceProvider();
            AESProvider.KeySize = 256;

            // Set the key for AESProvider
            AESProvider.Key = key;

        }


        public string[] Encrypt(string plainText)
        {

            string[] result = new string[2];

            // Create new random IV
            AESProvider.GenerateIV();

            result[0] = Convert.ToBase64String(AESProvider.IV);



            // Initialize encryptor now that the IV is set
            ICryptoTransform encryptor = AESProvider.CreateEncryptor();

            // Convert string to bytes
            byte[] plainBytes = UTF8Encoding.UTF8.GetBytes(plainText);

            // Encrypt plain bytes
            byte[] secureBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);


            result[1] = Convert.ToBase64String(secureBytes);
            return result;

        }


        /// <summary>
        /// Decrypts a string with AES algorithm
        /// </summary>
        /// <param name="secureText">Encrypted string with IV prefix</param>
        /// <returns>Decrypted string</returns>
       
      
        public string Decrypt(string Msg, string IV)
        {

            // Convert encrypted string to bytes
            byte[] secureBytes = Convert.FromBase64String(Msg);

            // Take IV from beginning of secureBytes
            AESProvider.IV = Convert.FromBase64String(IV);

            // Initialize decryptor now that the IV is set
            ICryptoTransform decryptor = AESProvider.CreateDecryptor();

            // Decrypt bytes after the IV
            byte[] plainBytes = decryptor.TransformFinalBlock(secureBytes, 0, secureBytes.Length);

            return UTF8Encoding.UTF8.GetString(plainBytes);


        }

        private byte[] HexStringToByteArray(string hex)
        {
            if (hex.Length % 2 == 1)
                throw new Exception("The binary key cannot have an odd number of digits");

            byte[] arr = new byte[hex.Length >> 1];

            for (int i = 0; i < hex.Length >> 1; ++i)
            {
                arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
            }

            return arr;
        }
        private int GetHexVal(char hex)
        {
            int val = (int)hex;
            //For uppercase A-F letters: 
            return val - (val < 58 ? 48 : 55);
            //For lowercase a-f letters: 
            //return val - (val < 58 ? 48 : 87); 
            //Or the two combined, but a bit slower: 
            //return val - (val < 58 ? 48 : (val < 97 ? 55 : 87)); 
        }

    }

}

Build Encrypted URL

            string TextToEncrypt = "?Key=" + txtAccountCode.Text.Trim() + "|" + txtToken.Text.Trim() + "&inputs=" + txtTextToEncrypt.Text.Trim();
            string query = TextToEncrypt;
            string EncryptedParameters = "";
                                                    
AES256 EP = new AES256(Encryption-Key); try { string[] encQuery = EP.Encrypt(query); EncryptedParameters = "m=" + encQuery[1].ToString() + "&i=" + encQuery[0].ToString(); } catch (Exception ee) { MessageBox.Show(ee.Message); return; } string EncryptedURL = txtURL.Text + EncryptedParameters; txtEncryptedURL.Text = EncryptedURL + "&Eindex=300";

Decrypt Returned URL

            int FindStart = txtEncryptedResponse.Text.Trim().IndexOf('?');
            string QryString = txtEncryptedResponse.Text.Trim().Substring(FindStart);

            NameValueCollection Items = HttpUtility.ParseQueryString(QryString);

            string m = Items["m"];
            string i = Items["i"];



            string FullQuery = "";



             AES256 EP = new AES256(Encryption-Key);

            try
            {
                /// note always replace any SPACE characters with + characters since these base 64 chars get translated over the web 
                FullQuery = EP.Decrypt(m.Replace(" ", "+"), i.Replace(" ", "+"));
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                return;
            }


<?php

   /// get post values needed to encrypt 
   $enckey = $_POST["EncKey"];
   $plaintext = $_POST["PlainText"];

   // Store the cipher method in variable
   $cipher = "AES-256-CBC"; 
   // Get the cipher iv length
   $iv_length = openssl_cipher_iv_length($cipher); 
   /// compute random IV bytes
   $iv = openssl_random_pseudo_bytes($iv_length);
   /// Base 64 encode IV
   $iv2 = base64_encode($iv);
   /// PREPARE ENCRYPTION KEY
   $enc_key = pack("H*", $enckey);
   /// COMPUTE THE ENCRYPTED RESULT 
   $encrypted_string = openssl_encrypt($plaintext, $cipher, $enc_key,  0, $iv); 

?>
<?php

   /// get post values needed to encrypt from previous page
   $iv =  $_GET['i'];;
   $msg =  $_GET['m'];

   $iv = str_replace(" ","+",$iv);
   $msg = str_replace(" ","+",$msg);

   $enckey = 'A142278ED69B3FE6C5E8F6XXXXXXXX519F3A82BEA9CB2719938DB88D5C35EC60';

   // Store the cipher method in variable
   $cipher = "AES-256-CBC"; 

   /// PREPARE ENCRYPTION KEY
   $enckey = pack("H*", $enckey);

   /// PREPARE IV 
   $iv2 = base64_decode($iv);
 
  // perform decryption
  $decrypted_string=openssl_decrypt ($msg, $cipher, $enckey,  0, $iv2); 

?>
private static String encrypt(String value, String encryptionKey) {

        Encoder encoder = Base64.getEncoder();

        try {
            IvParameterSpec iv = new IvParameterSpec(generateIV());

            SecretKeySpec skeySpec = new SecretKeySpec(hexStringToByteArray(encryptionKey), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] encrypted = cipher.doFinal(value.getBytes());

            String encText = encoder.encodeToString(encrypted);

            return encText;

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
}

private static byte[] generateIV() {

        Encoder encoder = Base64.getEncoder();

        SecureRandom random = new SecureRandom();
        byte[] iv = random.generateSeed(16);

        ivString = encoder.encodeToString(iv);

        return iv;
}

private static byte[] hexStringToByteArray(String hex) {
        int l = hex.length();
        byte[] data = new byte[l / 2];
        for (int i = 0; i < l; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i + 1), 16));
        }
        return data;
}
private static String decrypt(String encrypted, String ivString, String encryptionKey) {

        Decoder decoder = Base64.getDecoder();

        try {

            IvParameterSpec iv = new IvParameterSpec(decoder.decode(ivString));
            SecretKeySpec skeySpec = new SecretKeySpec(hexStringToByteArray(encryptionKey), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] original = cipher.doFinal(decoder.decode(encrypted));

            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
}

private static byte[] hexStringToByteArray(String hex) {
        int l = hex.length();
        byte[] data = new byte[l / 2];
        for (int i = 0; i < l; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i + 1), 16));
        }
        return data;
}
const crypto = require('crypto');

let iv = crypto.randomBytes(16);
let base64IV = iv.toString("base64");

let text_to_encrypt = "?Key=EP8698321|450D25CFBE1B4BEEADXXXXXXXX20D692&inputs=MERCHID|1|REFID|A12345NO-99|FIRSTNAME|matt|LASTNAME|smith|ADDRESS1|23 elm st|ADDRESS2||CITY|Tulsa|STATE|OK|ZIP|92107|AMOUNT|10.000|WTYPE|MPC|REDIRECT|https://easypay7.com/PostingApp/";


let key1 = 'A142278ED69B3FE6C5E8F63CB8XXXXXXXX3A82BEA9CB2719938DB88D5C35EC60';

let key2 = Buffer.from(key1, 'hex');

let cipher = crypto.createCipheriv('aes-256-cbc', key2, iv);
let encrypted = cipher.update(text_to_encrypt, 'utf-8', 'base64');
encrypted += cipher.final('base64');

//console.log('Msg ' + encrypted);

let url = 'https://easypay5.com/widgetlogger/?m=' + encrypted + '&i=' + base64IV + '&Eindex=300';

console.log(url);
const crypto = require('crypto');


//// decrypt 
// use same key from above 

let key3 = 'A142278ED69B3FE6C5E8F63CB8205XXXXXXXX2BEA9CB2719938DB88D5C35EC60';

let key4 = Buffer.from(key3, 'hex');

let IVfromEasyPay = 'zS4mIvs6SFNwdpcp+NoOsw==';
let MsgFromEasyPay = 'C+2IEgtpINKS2vsnQz+5ynBRF9pWVQ9hwKLa6A29Qppl95UvH+rtyHJ4iGfSy/so8tZJNB0o8ztF/xwINyiZVKS8c34ROXRcrJPDaL4bxAPtCMzxyYS/OnByxTM51CIV';

/// this is important !! 
IVfromEasyPay = IVfromEasyPay.replace(' ','+');
MsgFromEasyPay = MsgFromEasyPay.replace(' ','+'); 


let IvBytes = Buffer.from(IVfromEasyPay, 'base64');

let decipher = crypto.createDecipheriv('aes-256-cbc', key4, IvBytes);
let decrypted = decipher.update(MsgFromEasyPay, 'base64', 'utf-8');
decrypted += decipher.final('utf-8');
console.log('plain text ' + decrypted);
  • Refresh
  • Download
  • FolderDownloads