0% found this document useful (0 votes)
13 views3 pages

New Text Document

Uploaded by

mmouris
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)
13 views3 pages

New Text Document

Uploaded by

mmouris
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 Org.BouncyCastle.Crypto.

Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Iscore.Application.Common.Serivce
{
public class AesGcm256Service
{
private static readonly SecureRandom Random = new();

public static readonly int NonceBitSize = 128;


public static readonly int MacBitSize = 128;
public static readonly int KeyBitSize = 256;

public static readonly byte[] key =


HexToByte("4391AE9EB2D3B498B9D64EC4EB1F3C903A823EC4B01C713CD08A5F8C2722A5CE");
public static readonly byte[] iv =
HexToByte("304A7B19DECA8477376D5B8B240BF373");

public static byte[] NewKey()


{
var key = new byte[KeyBitSize / 8];
Random.NextBytes(key);
return key;
}

public static byte[] NewIv()


{
var iv = new byte[NonceBitSize / 8];
Random.NextBytes(iv);
return iv;
}

public static byte[] HexToByte(string hexStr)


{
byte[] bArray = new byte[hexStr.Length / 2];
for (int i = 0; i < (hexStr.Length / 2); i++)
{
byte firstNibble = byte.Parse(hexStr.Substring((2 * i), 1),
System.Globalization.NumberStyles.HexNumber); //
[x,y)
byte secondNibble = byte.Parse(hexStr.Substring((2 * i) + 1, 1),
System.Globalization.NumberStyles.HexNumber);
int finalByte = (secondNibble) | (firstNibble << 4); // bit-
operations
// only with
numbers, not bytes.
bArray[i] = (byte)finalByte;
}
return bArray;
}
public static string ToHex(byte[] data)
{
string hex = string.Empty;
foreach (byte c in data)
{
hex += c.ToString("X2");
}
return hex;
}

public static string ToHex(string asciiString)


{
string hex = string.Empty;
foreach (char c in asciiString)
{
int tmp = c;
hex += string.Format("{0:x2}",
System.Convert.ToUInt32(tmp.ToString()));
}
return hex;
}

public static string Encrypt(string PlainText)


{
try
{
byte[] plainBytes = Encoding.UTF8.GetBytes(PlainText);

GcmBlockCipher cipher = new(new AesEngine());


AeadParameters parameters = new(new KeyParameter(key), 128, iv,
null);

cipher.Init(true, parameters);

byte[] encryptedBytes = new


byte[cipher.GetOutputSize(plainBytes.Length)];
int retLen = cipher.ProcessBytes(plainBytes, 0, plainBytes.Length,
encryptedBytes, 0);
cipher.DoFinal(encryptedBytes, retLen);
return Convert.ToBase64String(encryptedBytes,
Base64FormattingOptions.None);
}
catch (Exception)
{
return string.Empty;
}
}

public static string Decrypt(string encryptedText)


{
try
{
var d = CreateSalt(encryptedText);
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);

GcmBlockCipher cipher = new(new AesEngine());


AeadParameters parameters = new(new KeyParameter(key), 128, iv,
null);

cipher.Init(false, parameters);
byte[] plainBytes = new
byte[cipher.GetOutputSize(encryptedBytes.Length)];
int retLen = cipher.ProcessBytes(encryptedBytes, 0,
encryptedBytes.Length, plainBytes, 0);
cipher.DoFinal(plainBytes, retLen);

return Encoding.UTF8.GetString(plainBytes).TrimEnd("\r\n\
0".ToCharArray());
}
catch (Exception)
{
return string.Empty;
}
}

private static byte[] CreateSalt(string pass)


{
var buffer = new byte[16];
var rng = new RNGCryptoServiceProvider();
rng.GetBytes(buffer);
byte[] encryptedBytes = Convert.FromBase64String(pass);

return buffer;
}
}
}

You might also like