Files
caliverse_server/ServerCore/Security/EncryptionHelper.cs
2025-05-01 07:20:41 +09:00

178 lines
6.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ServerCore;
public class EncryptionHelper
{
public static string encryptTextByDES(string text, byte[] encryptKey, byte[] encryptIv)
{
try
{
// Create a MemoryStream.
using (MemoryStream mStream = new MemoryStream())
{
// Create a new DES object.
using (var des = DES.Create())
// Create a DES encryptor from the key and IV
using (ICryptoTransform encryptor = des.CreateEncryptor(encryptKey, encryptIv))
// Create a CryptoStream using the MemoryStream and encryptor
using (var cStream = new CryptoStream(mStream, encryptor, CryptoStreamMode.Write))
{
// Convert the provided string to a byte array.
byte[] toEncrypt = Encoding.UTF8.GetBytes(text);
// Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length);
// Ending the using statement for the CryptoStream completes the encryption.
}
// Get an array of bytes from the MemoryStream that holds the encrypted data.
byte[] ret = mStream.ToArray();
// Return the encrypted buffer.
return Convert.ToBase64String(ret);
}
}
catch (CryptographicException e)
{
Log.getLogger().error($"Exception !!!, A Cryptographic error occurred : {e}");
throw;
}
}
public static string decryptTextByDES(string encrypted, byte[] encryptKey, byte[] encryptIv)
{
try
{
// Create a buffer to hold the decrypted data.
// DES-encrypted data will always be slightly bigger than the decrypted data.
//byte[] StrByte = Encoding.UTF8.GetBytes(encrypted);
var encryptedBytes = Convert.FromBase64String(encrypted);
byte[] decrypted = new byte[encrypted.Length];
int offset = 0;
// Create a new MemoryStream using the provided array of encrypted data.
using (MemoryStream mStream = new MemoryStream(encryptedBytes))
{
// Create a new DES object.
using (var des = DES.Create())
// Create a DES decryptor from the key and IV
using (ICryptoTransform decryptor = des.CreateDecryptor(encryptKey, encryptIv))
// Create a CryptoStream using the MemoryStream and decryptor
using (var cStream = new CryptoStream(mStream, decryptor, CryptoStreamMode.Read))
{
// Keep reading from the CryptoStream until it finishes (returns 0).
int read = 1;
while (read > 0)
{
read = cStream.Read(decrypted, offset, decrypted.Length - offset);
offset += read;
}
}
}
// Convert the buffer into a string and return it.
return Encoding.UTF8.GetString(decrypted, 0, offset);
}
catch (CryptographicException e)
{
Log.getLogger().error($"Exception !!!, A Cryptographic error occurred : {e}");
throw;
}
}
public static string encryptTextByAES(string plainText, byte[] encryptKey, byte[] encryptIv)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
NullReferenceCheckHelper.throwIfNull(plainText, () => $"plainText is null !!!");
if (encryptKey == null || encryptKey.Length <= 0)
NullReferenceCheckHelper.throwIfNull(encryptKey, () => $"encryptKey is null !!!");
if (encryptIv == null || encryptIv.Length <= 0)
NullReferenceCheckHelper.throwIfNull(encryptIv, () => $"encryptIv is null !!!");
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = encryptKey;
aesAlg.IV = encryptIv;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return Convert.ToBase64String(encrypted);
}
public static string decryptTextByAES(string cipherText, byte[] encryptKey, byte[] encryptIv)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
NullReferenceCheckHelper.throwIfNull(cipherText, () => $"cipherText is null !!!");
if (encryptKey == null || encryptKey.Length <= 0)
NullReferenceCheckHelper.throwIfNull(encryptKey, () => $"encryptKey is null !!!");
if (encryptIv == null || encryptIv.Length <= 0)
NullReferenceCheckHelper.throwIfNull(encryptIv, () => $"encryptIv is null !!!");
// Declare the string used to hold
// the decrypted text.
string plaintext = string.Empty;
byte[] chipherBin = Convert.FromBase64String(cipherText);
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = encryptKey;
aesAlg.IV = encryptIv;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(chipherBin))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}