patbef-ServiceInside/Support/Crypto/AES.cs

179 lines
5.4 KiB
C#
Raw Permalink Normal View History

2024-01-29 16:26:54 +01:00
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.IO;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Support.Crypto
{
public class AES
{
private static string MemoryMasterKey = "!ZEqy2Zsb#VK6<8`H6;W~VxJ$r:w.{ffzwDt=<yKC6m6N3;T<9nyeF&+.4&D@rhK";
public const string PGS_ENCRYPT_PARTIAL_KEY = "wA6j@x.CcmM>~5Ss^C_!#,zch)$YsDsd59,::>dW#F`@U]Ye5ETZcMT7}&+#*!%z";
public static string Encrypt(string value, byte[] deriveKey = null)
{
string result = null;
try
{
byte[] keyBytes = deriveKey;
if (deriveKey == null)
{
keyBytes = GetKey(MemoryMasterKey);
}
IBufferedCipher encCipher = CipherUtilities.GetCipher("AES/GCM/NoPadding");
KeyParameter key = new KeyParameter(keyBytes);
byte[] nonce = new byte[12];
RandomNumberGenerator.Create().GetBytes(nonce);
encCipher.Init(true, new ParametersWithIV(key, nonce));
byte[] plainText = Encoding.UTF8.GetBytes(value);
byte[] chiperBuffer = null;
byte[] tag = new byte[16];
byte[] resultBuffer = null;
using (MemoryStream memStream = new MemoryStream())
{
using (CipherStream encStream = new CipherStream(memStream, null, encCipher))
{
encStream.Write(plainText, 0, plainText.Length);
}
chiperBuffer = memStream.ToArray();
resultBuffer = nonce.Concat(chiperBuffer).ToArray();
}
result = Convert.ToBase64String(resultBuffer);
}
catch
{
result = null;
}
return result;
}
public static string Decrypt(string value, byte[] deriveKey = null)
{
string result = null;
try
{
byte[] keyBytes = deriveKey;
if (deriveKey == null)
{
keyBytes = GetKey(MemoryMasterKey);
}
IBufferedCipher decCipher = CipherUtilities.GetCipher("AES/GCM/NoPadding");
KeyParameter key = new KeyParameter(keyBytes);
byte[] combined = Convert.FromBase64String(value);
byte[] tag = combined.Skip(combined.Length - 16).ToArray();
byte[] nonce = combined.Take(12).ToArray();
byte[] chiperText = combined.Skip(nonce.Length).Take(combined.Length - nonce.Length).ToArray();
byte[] plainTextBuffer = null;
decCipher.Init(false, new ParametersWithIV(key, nonce));
using (MemoryStream memStream = new MemoryStream(chiperText, false))
{
MemoryStream dataStream = new MemoryStream();
using (dataStream)
{
using (CipherStream decStream = new CipherStream(memStream, (IBufferedCipher)decCipher, null))
{
int ch;
while ((ch = decStream.ReadByte()) >= 0)
{
dataStream.WriteByte((byte)ch);
}
}
}
plainTextBuffer = dataStream.ToArray();
result = Encoding.UTF8.GetString(plainTextBuffer);
}
}
catch
{
result = null;
}
return result;
}
public static byte[] GetKey(string password)
{
byte[] result = null;
try
{
if (!string.IsNullOrEmpty(password))
{
if (password.Length < 32)
{
password = password.PadRight(32, '@');
}
else if (password.Length > 32)
{
password = MD5Encrypt(password);
}
result = Encoding.ASCII.GetBytes(password);
}
}
catch (Exception ex)
{
}
return result;
}
public static string MD5Encrypt(string value)
{
string result = null;
try
{
byte[] bytes = Encoding.UTF8.GetBytes(value);
using (System.Security.Cryptography.MD5 hash = System.Security.Cryptography.MD5.Create())
{
byte[] hashedInputBytes = hash.ComputeHash(bytes);
StringBuilder hashedInputStringBuilder = new StringBuilder(128);
foreach (var b in hashedInputBytes)
{
hashedInputStringBuilder.Append(b.ToString("X2"));
}
result = hashedInputStringBuilder.ToString().ToLower();
}
}
catch
{
result = null;
}
return result;
}
}
}