0% found this document useful (0 votes)
29 views

5

The document contains a C# program that implements an authentication process using an API. It includes methods for generating a secure key, encrypting data with RSA, and decrypting data with AES. The program sends a request to a server API with user credentials and handles the response, including error handling and logging output to the console.

Uploaded by

avrajput0141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

5

The document contains a C# program that implements an authentication process using an API. It includes methods for generating a secure key, encrypting data with RSA, and decrypting data with AES. The program sends a request to a server API with user credentials and handles the response, including error handling and logging output to the console.

Uploaded by

avrajput0141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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 Program
{
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.04/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.Password = password;
data.AppKey = straesKey;
data.UserName = userName;
data.ForceRefreshAccessToken = false;
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 AppKey { get; set; }
public string UserName { get; set; }
public Boolean ForceRefreshAccessToken { get; set; }
}
public class RequestPayloadN

You might also like