C# Encryption v2
Encryption Class
Create a new Encryption.cs file and paste in the following c# code.
/* .NET Core SDK version 2.2
* https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code
*/
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
class Encryption
{
public string ivKey;
public string secretKey;
public Encryption (string ivKey, string secretKey) {
this.ivKey = ivKey;
this.secretKey = secretKey;
}
private string HashString(string str)
{
var stringBuilder = new StringBuilder();
using (var hash = SHA256.Create())
{
var result = hash.ComputeHash(Encoding.UTF8.GetBytes(str));
foreach (var x in result)
{
stringBuilder.Append(x.ToString("x2"));
}
}
return stringBuilder.ToString();
}
public string Encrypt(string payload)
{
byte[] encrypted;
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.KeySize = 256;
aes.BlockSize = 128;
aes.Key = Encoding.UTF8.GetBytes(HashString(this._ivKey).Substring(0, 16));
aes.IV = Encoding.UTF8.GetBytes(HashString(this._secretKey).Substring(0, 16));
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform enc = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, enc, CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(payload);
}
encrypted = ms.ToArray();
}
}
}
return Convert.ToBase64String(Encoding.UTF8.GetBytes(Convert.ToBase64String(encrypted)));
}
}
Encryption Class Usage (.NET)
For this example, we will use .NET to give a realistic example. Use the examples below to setup this C# demo if you do not have a .NET application already. A more comprehensive guide can be found here.
Step 1: Create a project directory $ mkdir checkout-demo
Step 2: Go into the project directory $ cd checkout-demo
Step 3: Generate a new project following the Web API template $ dotnet new webapi
Then create a file CheckoutController.cs
and paste in the code below. Open a terminal session in your project directory and run the application using the command dotnet run
using System;
using Checkout.Models;
using CheckoutEncryption.utils;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace CheckoutEncryption.Controllers
{
public class CheckoutController : Controller
{
[HttpPost("checkout-encryption")]
public ActionResult CheckoutEncryption([FromBody] Payload payload)
{
string accessKey = "";
string ivKey = "";
string secretKey = "";
Encryption encryption = new Encryption(ivKey, secretKey);
string json = JsonConvert.SerializeObject(payload).Replace("/", "\\/");
Console.WriteLine(json);
string encParams = encryption.Encrypt(json);
return Json(new {param = encParams, accessKey = accessKey, countryCode = payload.countryCode});
}
}
}
// Payload Model
public class Payload
{
public string merchantTransactionID { get; set; }
public string customerFirstName { get; set; }
public string customerLastName { get; set; }
public string MSISDN { get; set; }
public string customerEmail { get; set; }
public string requestAmount { get; set; }
public string currencyCode { get; set; }
public string accountNumber { get; set; }
public string serviceCode { get; set; }
public string dueDate { get; set; }
public string requestDescription { get; set; }
public string countryCode { get; set; }
public string languageCode { get; set; }
public string successRedirectUrl { get; set; }
public string failRedirectUrl { get; set; }
public string paymentWebhookUrl { get; set; }
}