Sample Code in C#.net

Encryption and Decryption

Authentication sample code for Version(1.03) Auth

   using System;
   using System.Collections.Generic;
   using System.Linq
   using System.Text;
   using System.Threading.Tasks;
   namespace EInvoiceAuthDotNet
{
   using Newtonsoft.Json;
   using Org.BouncyCastle.Crypto;
   using Org.BouncyCastle.Crypto.Parameters;
   using Org.BouncyCastle.Security;
   using System;
   using System.Collections.Generic;
   using System.IO;
   using System.Linq;
   using System.Net;
   using System.Net.Http;
   using System.Net.Http.Headers;
   using System.Security.Cryptography;
   using System.Text;
   using System.Threading.Tasks;
   using Newtonsoft.Json.Linq;
   namespace EinvoiceAuth
{
   class EwbAuth
   {
    static void Main(string[] args)
   {
   call_server_api();
    }
    public static void call_server_api()
    {
    try
    {
    string public_key = "<Public Key>";
    HttpClient client = new HttpClient();
    string uri = "<URL>/v1.03/auth";
    client.DefaultRequestHeaders.Add("client-id", "<Client Id>");
    client.DefaultRequestHeaders.Add("client-secret", "<Client Secret>");
    string userName = "< User Id >";
    string password = "< Password >";
    client.DefaultRequestHeaders.Add("gstin", "<GSTIN>");
    byte[] _aeskey = generateSecureKey();
    string straesKey = Convert.ToBase64String(_aeskey);
    RequestPayloadN aRequestPayload = new RequestPayloadN();

    Auth data = new Auth();
    data.Action = "ACCESSTOKEN";
    data.Password = password;
    data.App_Key = straesKey;
    data.UserName = userName;
    string authStr = JsonConvert.SerializeObject(data);
    byte[] authBytes = System.Text.Encoding.UTF8.GetBytes(authStr);
    aRequestPayload.Data = Encrypt(Convert.ToBase64String(authBytes), public_key);
    string abc = JsonConvert.SerializeObject(aRequestPayload);
    HttpResponseMessage res = client.PostAsJsonAsync(uri, aRequestPayload).Result;
    if (res.IsSuccessStatusCode)
    {
    Console.WriteLine("Call is success");
    string verification = res.Content.ReadAsStringAsync().Result;
    Console.WriteLine($"Response{verification}");
    AuthResponse authResponse = res.Content.ReadAsAsync<AuthResponse>().Result;
    string sek = DecryptBySymmerticKey(authResponse.Data.Sek, _aeskey);
    Console.WriteLine($"Sek {sek}");
}
   else
{
    var stream = res.Content.ReadAsStreamAsync().Result;
    StreamReader reader = new StreamReader(stream);
    string text = reader.ReadToEnd();
    string err = res.ReasonPhrase;
    Console.WriteLine($"error Response{text} reason{err}");
}
}
   catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}
}

   public static string DecryptBySymmerticKey(string encryptedText, byte[] key)
    {
    try
    {
    byte[] dataToDecrypt = Convert.FromBase64String(encryptedText);
    var keyBytes = key;
    AesManaged tdes = new AesManaged();
    tdes.KeySize = 256;
   tdes.BlockSize = 128;
    tdes.Key = keyBytes;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;
   ICryptoTransform decrypt__1 = tdes.CreateDecryptor();
    byte[] deCipher = decrypt__1.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length);
    tdes.Clear();
    string EK_result = Convert.ToBase64String(deCipher);
    // var EK = Convert.FromBase64String(EK_result);
    // return EK;
    return EK_result;
   }
    catch (Exception ex)
    {
   throw ex;
    }
    }

    public static byte[] generateSecureKey()
    {
    Aes KEYGEN = Aes.Create();
    byte[] secretKey = KEYGEN.Key;
    return secretKey;
    }

   public static string Encrypt(string data, string key)
{
    byte[] keyBytes =
    Convert.FromBase64String(key); // your key here
    AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(keyBytes);
    RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter;
    RSAParameters rsaParameters = new RSAParameters();
    rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned();
    rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned();
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    rsa.ImportParameters(rsaParameters);
    byte[] plaintext = Encoding.UTF8.GetBytes(data);
    byte[] ciphertext = rsa.Encrypt(plaintext, false);
    string cipherresult = Convert.ToBase64String(ciphertext);
    //string cipherresult = Encoding.ASCII.GetString(ciphertext);
    return cipherresult;
}
}
    public class Auth
    {
    public string Password { get; set; }
    public string App_Key { get; set; }
    public string UserName { get; set; }
    public string Action { get; set; }
    }

    public class RequestPayloadN
    {
    public string Data { get; set; }
    }

    public class AuthResponse
    {
    public string Status { get; set; }
    public List ErrorDetails { get; set; }
    public List InfoDtls { get; set; }
    public class ErrorDetail
    {
    public string ErrorCode { get; set; }
    public string ErrorMessage { get; set; }
    }

    public class InfoDtl
    {
    public string InfCd { get; set; }
    public List Desc { get; set; }
    }

    public class Infodata
    {
    public string ErrorCode { get; set; }
    public string ErrorMessage { get; set; }
    }

    public data Data { get; set; }
    public class data
    {
    public string ClientId { get; set; }
    public string UserName { get; set; }
    public string AuthToken { get; set; }
    public string Sek { get; set; }
    public string TokenExpiry { get; set; }
    public static implicit operator data(string v)
    {
    throw new NotImplementedException();
    }
   }
   }
   }
   }

Symmetric Key Encryption (AES)

The following C#.Net code snippet can be used for encrypting the data using the symmetric key.

 public static string EncryptBySymmetricKey(string text, string sek)
    {
    //Encrypting SEK
    try
    {
      byte[] dataToEncrypt = Convert.FromBase64String(text);
      var keyBytes = Convert.FromBase64String(sek);
      AesManaged tdes = new AesManaged();
      tdes.KeySize = 256;
      tdes.BlockSize = 128;
      tdes.Key = keyBytes;
      tdes.Mode = CipherMode.ECB;
      tdes.Padding = PaddingMode.PKCS7;
      pICryptoTransform encrypt__1 = tdes.CreateEncryptor();
      byte[] deCipher = encrypt__1.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
      tdes.Clear();
      string EK_result = Convert.ToBase64String(deCipher);
      return EK_result;
   }
   catch (Exception ex)
    {
      throw ex;
   }
 }

Symmetric Decryption (AES)

The following C#.Net code snippet can be used for decrypting the encrypted sek using the appkey.

  public static byte[] DecryptBySymmetricKey(string encryptedText, byte[] key)
  {

   //Decrypting SEK
   try
   {

     byte[] dataToDecrypt = Convert.FromBase64String(encryptedText);
     var keyBytes = key;
     AesManaged tdes = new AesManaged();
     tdes.KeySize = 256;
     tdes.BlockSize = 128;
     tdes.Key = keyBytes;
     tdes.Mode = CipherMode.ECB;
     tdes.Padding = PaddingMode.PKCS7;
     ICryptoTransform decrypt__1 = tdes.CreateDecryptor();
     byte[] deCipher = decrypt__1.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length);
     tdes.Clear();
     string EK_result = Convert.ToBase64String(deCipher);
     return EK_result;
   }
   catch (Exception ex)
   {
     throw ex;
   }
 }

Sample code to generate E-way Bill

Public GeneratedEwayBill()
Step 1: Prepare the request object by setting the URL, client-id, client secret, GSTIN, and Auth token received from the previous API.

{
   HttpWebRequest request =
   (HttpWebRequest)WebRequest.Create("<URL>/EwayApi");
   request.Method = "POST";
    request.KeepAlive = true;
   request.AllowAutoRedirect = false;
   request.Accept = "*/*";
   request.ContentType = "application/json";
    request.Headers.Add("client-id", "TESTCLIENTID");
    request.Headers.Add("client-secret", "CLIENTSECRET");
    request.Headers.Add("gstin", "29AAACGIIIII1Z3");
    request.Headers.Add("authtoken", "0aAjBKdo7rcNYJB30g5DS2u8z");

Step 2: Prepare the JSON string with all parameters. Post the request and receive the response. Refer the annexure for the parameter details.

   ewayapi.Entities.EwayBillApiRequest ewbReq = new
    ewayapi.Entities.EwayBillApiRequest();
   ewbReq.action = "GENEWAYBILL";
//Serialised JSON Data
// Following is for illustration purpose only , Actual JSOM format from the Sample JSON / Schema may be referred
string jsonData =
“{\"supplyType\":\"O\",\"subSupplyType\":\"1\",\"DocType\":\"INV\",\"docNo\":\"65656565\",\"docDate\":\"10/03/2018\",\"fromGstin\":\"08AAAFW1939J1Z
X\",\"actFromStateCode\":\"29\",\"actToStateCode\":\"29\",\"totInvValue\":\"10.0\",\"fromTrdName\":\"test\",\"fromAddr1\":\"4556\",\"fromAddr2\":\"
hulimavu\",\"fromPlace\":\"bannargatta\",\"fromPincode\":545454,\"fromStateCode\":29,\"toGstin\":\"05AAAAZ1723G1ZR\",\"toTrdName\":\"test2\",\"toAd
dr1\":\"toaddress1\",\"toAddr2\":\"toaddress2\",\"toPlace\":\"toplace\",\"toPincode\":899879,\"toStateCode\":29,\"totalValue\":68000,\"cgstValue\":
10,\"sgstValue\":10,\"igstValue\":0,\"cessValue\":1800,\"transporterName\":\"\",\"transporterId\":\"29AAACG0569P1Z3\",\"transDocNo\":\"12345\",\"tr
ansMode\":\"1\",\"transDocDate\":\"10/03/2018\",\"transDistance\":\"20\",\"vehicleType\":\"r\",\"vehicleNo\":\"RJ191G5024
\",\"ItemList\":[{\"productName\":\"CEMENT\",\"hsnCode\":25210010,\"quantity\":2,\"cgstRate\":10,\"sgstRate\":10,\"igstRate\":0,\"cessRate\":1,\"ce
ssAdvol\":0},{\"productName\":\"steel\",\"productDesc\":\"steel
rods\",\"hsnCode\":2402,\"quantity\":4,\"qtyUnit\":\"NOS\",\"cgstRate\":10,\"sgstRate\":10,\"igstRate\":0,\"cessRate\":3,\"cessAdvol\":0}]}";

ewbReq.data = encdec.EncryptBySymmetricKey(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(jsonData)), sek); //Use decrypted sek here
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
   string json = serial1.Serialize(ewbReq);
   streamWriter.Write(json);
   streamWriter.Flush();
   streamWriter.Close();
}
   WebResponse response = request.GetResponse();
   string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
   serial1 = new JavaScriptSerializer();


Step 3: Receive the response. Decrypt using sek and Decode the Base 64 string to plain text

   ewayapi.Entities.EwayBillApiResponse ewbres =
   serial1.Deserialize(result);
   string data = encdec.DecryptBySymmetricKey(ewbres.data, Convert.FromBase64String(sek));
   byte[] reqDatabytes = Convert.FromBase64String(data);
   string requestData = System.Text.Encoding.UTF8.GetString(reqDatabytes);
}

Sample code to update Vehicle

Public void UpdateVehicle
   {
      HttpWebRequest request = (HttpWebRequest)WebRequest
      .Create("<URL>/ewaybillapi/EwayApi");
      request.Method = "POST";
      request.KeepAlive = true;
      request.AllowAutoRedirect = false;
      request.Accept = "*/*";
      request.ContentType = "application/json";
      request.Headers.Add("client-id", "TESTCLIENTID");
      request.Headers.Add("client-secret", "CLIENTSECRET");
      request.Headers.Add("gstin", "29AAACGIIIII1Z3");
      request.Headers.Add("authtoken", "0aAjBKdo7rcNYJB30g5DS2u8z");
      ewayapi.Entities.EwayBillApiRequest ewbReq = new
      ewayapi.Entities.EwayBillApiRequest();
      ewbReq.action = "VEHEWB";
      JavaScriptSerializer serial1 = new JavaScriptSerializer();
// Following is for illustration purpose only , Actual JSOM format from the Sample JSON / Schema may be referred
string jsonData =
"{\"ewbNo\":111000609282,\"vehicleNo\":\"PQR1234\",\"fromPlace\":\"BANGALORE\",\"fromState\":29,\"reasonCode\":\"1\",\"reasonRem\":\"vehicle broke
down\", \"transMode\":\"1\", \"transDocNo\":\"1\",\"transDocDate\":\"10/11/2017\"}";
   ewbReq.data = encdec.EncryptBySymmetricKey(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(jsonData)), sek);
   using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
      string json = serial1.Serialize(ewbReq);
      streamWriter.Write(json);
      streamWriter.Flush();
      streamWriter.Close();
    }
      WebResponse response = request.GetResponse();
      string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
      serial1 = new JavaScriptSerializer();
      ewayapi.Entities.EwayBillApiResponse ewbres =
      serial1.Deserialize(result);
      string data = encdec.DecryptBySymmetricKey(ewbres.data, Convert.FromBase64String(sek));
      yte[] reqDatabytes = Convert.FromBase64String(data);
       string requestData = System.Text.Encoding.UTF8.GetString(reqDatabytes);}
   }

Sample code to Generate Consolidated Eway bill

Public void GenerateConsolidatedEwb()
   {
       HttpWebRequest request = (HttpWebRequest)WebRequest
      . Create("<URL>/ewaybillapi/EwayApi");
       request.Method = "POST";
      request.KeepAlive = true;
       request.AllowAutoRedirect = false;
      request.Accept = "*/*";
      request.ContentType = "application/json";
       request.Headers.Add("client-id", "TESTCLIENTID");
       request.Headers.Add("client-secret", "CLIENTSECRET");
       request.Headers.Add("gstin", "29AAACGIIIII1Z3");
       request.Headers.Add("authtoken", "0aAjBKdo7rcNYJB30g5DS2u8z");
       ewayapi.Entities.EwayBillApiRequest ewbReq = new
       ewayapi.Entities.EwayBillApiRequest();
       ewbReq.action = "GENCEWB";
       JavaScriptSerializer serial1 = new JavaScriptSerializer();

// Following is for illustration purpose only , Actual JSOM format from the Sample JSON / Schema may be referred
    string jsonData = "{\"fromPlace\":\"BANGALORE SOUTH\",\"fromState\":\"29\",\"vehicleNo\":\"KA12AB1234\",\"transMode\":\"1\",
   \"TransDocNo\":\"1\",\"TransDocDate\":\"10/11/2017\", \"tripSheetEwbBills\":[{\"ewbNo\":111000609282},{\"ewbNo\":181000609270}]}";
ewbReq.data = encdec.EncryptBySymmetricKey(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(jsonData)), sek);
    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
      string json = serial1.Serialize(ewbReq);
      streamWriter.Write(json);
      streamWriter.Flush();
      streamWriter.Close();
   }
       WebResponse response = request.GetResponse();
      string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
       serial1 = new JavaScriptSerializer();
       ewayapi.Entities.EwayBillApiResponse ewbres =
      serial1.Deserialize(result);
       string data = encdec.DecryptBySymmetricKey(ewbres.data, Convert.FromBase64String(sek));
       yte[] reqDatabytes = Convert.FromBase64String(data);
       string requestData = System.Text.Encoding.UTF8.GetString(reqDatabytes);}
}

Sample code to Cancel Eway bill

Public void CancelEwayBill()
   {
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<URL>/EwayApi");
   request.Method = "POST";

       /*Optional*/
       request.KeepAlive = true;
       request.AllowAutoRedirect = false;
       request.Accept = "*/*";
       request.ContentType = "application/json";
      request.Headers.Add("client-id", "TESTCLIENTID");
      request.Headers.Add("client-secret", "CLIENTSECRET");
   request.Headers.Add("gstin", "29AAACGIIIII1Z3");
      request.Headers.Add("authtoken", "0aAjBKdo7rcNYJB30g5DS2u8z");
      ewayapi.Entities.EwayBillApiRequest ewbReq = new
      ewayapi.Entities.EwayBillApiRequest();
      ewbReq.action = "CANEWB";
      JavaScriptSerializer serial1 = new JavaScriptSerializer();

// Following is for illustration purpose only , Actual JSOM format from the Sample JSON / Schema may be referred
   string jsonData = "{\"ewbNo\":111000609282,\"cancelRsnCode\":2,\"cancelRmrk\":\"Cancelled the order\"}";
   ewbReq.data = encdec. EncryptBySymmetricKey (Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(jsonData)), sek);
      using (var streamWriter = new StreamWriter(request.GetRequestStream()))
      {
         string json = serial1.Serialize(ewbReq);
         streamWriter.Write(json);
         streamWriter.Flush();
         streamWriter.Close();
       }
         WebResponse response = request.GetResponse();
         string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
         serial1 = new JavaScriptSerializer();

         ewayapi.Entities.EwayBillApiResponse ewbres =
         serial1.Deserialize(result);
         string data = encdec.DecryptBySymmetricKey(ewbres.data, Convert.FromBase64String(sek));
         yte[] reqDatabytes = Convert.FromBase64String(data);
         string requestData = System.Text.Encoding.UTF8.GetString(reqDatabytes);}
}

Sample code to Reject Eway bill

Public void RejectEwayBill()
   {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<URL>/EwayApi");
   request.Method = "POST";

       /*Optional*/
       request.KeepAlive = true;
       request.AllowAutoRedirect = false;
       request.Accept = "*/*";
       request.ContentType = "application/json";
       request.Headers.Add("client-id", "TESTCLIENTID");
        request.Headers.Add("client-secret", "CLIENTSECRET");
        request.Headers.Add("gstin", "29AAACGIIIII1Z3");
       request.Headers.Add("authtoken", "0aAjBKdo7rcNYJB30g5DS2u8z");
        ewayapi.Entities.EwayBillApiRequest ewbReq = new ewayapi.Entities.EwayBillApiRequest();
       ewbReq.action = "REJEWB";
       JavaScriptSerializer serial1 = new JavaScriptSerializer();
       string jsonData = = "{\"ewbNo\":111000609282}";
       ewbReq.data = encdec. EncryptBySymmetricKey (Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(jsonData)), sek);
       using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
       string json = serial1.Serialize(ewbReq);
       streamWriter.Write(json);
        streamWriter.Flush();
       streamWriter.Close();
   }
       WebResponse response = request.GetResponse();
       string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
        serial1 = new JavaScriptSerializer();
       ewayapi.Entities.EwayBillApiResponse ewbres =
       serial1.Deserialize(result);
       string data = encdec. DecryptBySymmetricKey (ewbres.data, Convert.FromBase64String(sek));
       yte[] reqDatabytes = Convert.FromBase64String(data);
       string requestData = System.Text.Encoding.UTF8.GetString(reqDatabytes);}
}

Sample code to Update Transporter

Public void RejectEwayBill()
    {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<URL>/EwayApi");
       request.Method = "POST";

       /*Optional*/
       request.KeepAlive = true;
       request.AllowAutoRedirect = false;
       request.Accept = "*/*";
       request.ContentType = "application/json";
      request.Headers.Add("client-id", "TESTCLIENTID");
      request.Headers.Add("client-secret", "CLIENTSECRET");
       request.Headers.Add("gstin", "29AAACGIIIII1Z3");
       request.Headers.Add("authtoken", "0aAjBKdo7rcNYJB30g5DS2u8z");
      ewayapi.Entities.EwayBillApiRequest ewbReq = new ewayapi.Entities.EwayBillApiRequest();
      ewbReq.action = " UPDATETRANSPORTER ";
      JavaScriptSerializer serial1 = new JavaScriptSerializer();

// Following is for illustration purpose only , Actual JSOM format from the Sample JSON / Schema may be referred

      string jsonData = = "{\"ewbNo\": 171000611199, \"transporterId\":\" 05AAAAZ1723G1ZR \" } ";
      ewbReq.data = encdec. EncryptBySymmetricKey
      (Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(jsonData)), sek);
       using (var streamWriter = new StreamWriter(request.GetRequestStream()))
   {
       string json = serial1.Serialize(ewbReq);
       streamWriter.Write(json);
      streamWriter.Flush();
      streamWriter.Close();
   }
       WebResponse response = request.GetResponse();
      string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
       serial1 = new JavaScriptSerializer();
      ewayapi.Entities.EwayBillApiResponse ewbres =
      .Deserialize(result);
       string data = encdec. DecryptBySymmetricKey (ewbres.data, Convert.FromBase64String(sek));
      yte[] reqDatabytes = Convert.FromBase64String(data);
      string requestData = System.Text.Encoding.UTF8.GetString(reqDatabytes);}
}

Sample Code for Extend Validity of E-way Bill

Public void RejectEwayBill()
   {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<URL>/v1/EwayApi");
   request.Method = "POST";

       /*Optional*/
       request.KeepAlive = true;
      request.AllowAutoRedirect = false;
      request.Accept = "*/*";
       request.ContentType = "application/json";
      request.Headers.Add("client-id", "TESTCLIENTID");
      request.Headers.Add("client-secret", "CLIENTSECRET");
      request.Headers.Add("gstin", "29AAACGIIIII1Z3");
      request.Headers.Add("authtoken", "0aAjBKdo7rcNYJB30g5DS2u8z");
       ewayapi.Entities.EwayBillApiRequest ewbReq = new ewayapi.Entities.EwayBillApiRequest();
      ewbReq.action = " EXTENDVALIDITY ";
      JavaScriptSerializer serial1 = new JavaScriptSerializer();

// Following is for illustration purpose only , Actual JSOM format from the Sample JSON / Schema may be referred

   string jsonData = "{\"ewbNo\": 171000611199,\"vehicleNo\":
   \"PQR1234\",\"fromPlace\":\"Bengaluru\",\"fromState\":29,\"remainingDistance\":50,\"transDocNo\": \"1234 \",\"transDocDate\": \"12/04/2018 \",
\"transMode\": \"1\", \"extnRsnCode\":1,\"extnRemarks\":\"Flood\"}";
    ewbReq.data = encdec. EncryptBySymmetricKey (Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(jsonData)), sek);
    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
   {
      string json = serial1.Serialize(ewbReq);
      streamWriter.Write(json);
      streamWriter.Flush();
      streamWriter.Close();
    }
      WebResponse response = request.GetResponse();
       string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
      serial1 = new JavaScriptSerializer();
      ewayapi.Entities.EwayBillApiResponse ewbres =
       serial1.Deserialize(result);
      string data = encdec. DecryptBySymmetricKey (ewbres.data, Convert.FromBase64String(sek));
       yte[] reqDatabytes = Convert.FromBase64String(data);
      string requestData = System.Text.Encoding.UTF8.GetString(reqDatabytes);}
}

Sample code for Regenerate Consolidated E-way Bill

Public void RejectEwayBill()
   {
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<URL>/EwayApi");
      request.Method = "POST";
       /*Optional*/
       request.KeepAlive = true;
      request.AllowAutoRedirect = false;
      request.Accept = "*/*";
       request.ContentType = "application/json";
      request.Headers.Add("client-id", "TESTCLIENTID");
      request.Headers.Add("client-secret", "CLIENTSECRET");
      request.Headers.Add("gstin", "29AAACGIIIII1Z3");
      request.Headers.Add("authtoken", "0aAjBKdo7rcNYJB30g5DS2u8z");
      ewayapi.Entities.EwayBillApiRequest ewbReq = new ewayapi.Entities.EwayBillApiRequest();
      ewbReq.action = " REGENTRIPSHEET ";
      JavaScriptSerializer serial1 = new JavaScriptSerializer();

// Following is for illustration purpose only , Actual JSOM format from the Sample JSON / Schema may be referred

   string jsonData = "{\"tripSheetNo\": 7810004903,\"vehicleNo\":
   \"PQR1234\",\"fromPlace\":\"Bengaluru\",\"fromState\":29,\"transDocNo\": \"1234 \",\"transDocDate\": \"12/10/2017 \", \"transMode\": \"2\",
   \"reasonCode\":6,\"reasonRem\":\"Flood\"}";
   ewbReq.data = encdec. EncryptBySymmetricKey (Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(jsonData)), sek);
    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
   {
      string json = serial1.Serialize(ewbReq);
      streamWriter.Write(json);
       streamWriter.Flush();
      streamWriter.Close();
   }
       WebResponse response = request.GetResponse();
       string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
      serial1 = new JavaScriptSerializer();
      ewayapi.Entities.EwayBillApiResponse ewbres =
      serial1.Deserialize(result);
      string data = encdec. DecryptBySymmetricKey (ewbres.data, Convert.FromBase64String(sek));
      yte[] reqDatabytes = Convert.FromBase64String(data);
       string requestData = System.Text.Encoding.UTF8.GetString(reqDatabytes);}
}

Sample code for Get Methods

Public void GetEwayBill()
   {
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create("<URL>/EwayApi/GetEwayBill?ewbNo=191000001846");
      request.Method = "GET";
      request.KeepAlive = true;
      request.AllowAutoRedirect = false;
       request.Accept = "*/*";
      request.ContentType = "application/json";
      request.Headers.Add("client-id", "TESTCLIENTID");
      request.Headers.Add("client-secret", "CLIENTSECRET");
      request.Headers.Add("gstin", "29AAACGIIIII1Z3");
      request.Headers.Add("authtoken", "0aAjBKdo7rcNYJB30g5DS2u8z");
      WebResponse response = request.GetResponse();
       string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
      JavaScriptSerializer serial1 = new JavaScriptSerializer();
      ewayapi.Entities.EwayBillApiResponseForGet ewbres = serial1.Deserializ(result);
      string rek = encdec.DecryptBySymmetricKey (ewbres.rek, Convert.FromBase64String(sek));
      string data = encdec.DecryptBySymmetricKey (ewbres.data, Convert.FromBase64String(rek));
      byte[] reqDatabytes = Convert.FromBase64String(data);

          //byte[] reqDatabytes = Convert.FromBase64String(ewbres.data);
          string requestData = System.Text.Encoding.UTF8.GetString(reqDatabytes);
          //string hmac = encdec.GenerateHMAC(requestData, Convert.FromBase64String(rek));
          string hmac = encdec.GenerateHMAC(data, Convert.FromBase64String(rek));
          if (ewbres.hmac == hmac){
      }
}