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();
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 GeneratedEwayBill()
Step 1: Prepare the request object by setting the URL, client-id, client secret, GSTIN, and Auth token received from the previous API.
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
// 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);}
}
// 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();