SafeDispatch/SafeMobileLIB_DLL/Encryption.cs

160 lines
6.1 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace SafeMobileLib
{
static public class Encryption
{
static public byte[] Encrypt(byte[] inputArray, EncryptionAlgorithm algorithm)
{
if (algorithm == EncryptionAlgorithm.Aes256)
return AesEncrypt(inputArray);
else if (algorithm == EncryptionAlgorithm.Des64)
return DesEncrypt(inputArray);
else if (algorithm == EncryptionAlgorithm.None)
return inputArray;
else
throw new ApplicationException("No ecryption defined for algorithm " + algorithm);
}
static public byte[] Decrypt(byte[] dataToDecrypt, EncryptionAlgorithm algorithm)
{
if (algorithm == EncryptionAlgorithm.Aes256)
return AesDecrypt(dataToDecrypt);
else if (algorithm == EncryptionAlgorithm.Des64)
return DesDecrypt(dataToDecrypt);
else if (algorithm == EncryptionAlgorithm.None)
return dataToDecrypt;
else
throw new ApplicationException("No decryption defined for algorithm " + algorithm);
}
#region DES64 encryption
private static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("SafeMobi"); //DES 64bit !
private static byte[] DesEncrypt(byte[] inputArray)
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
cryptoStream.Write(inputArray,0,inputArray.Length);
cryptoStream.Close(); memoryStream.Close();
return memoryStream.ToArray();
}
private static byte[] DesDecrypt(byte[] DataToDecrypt)
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Write);
cryptoStream.Write(DataToDecrypt, 0, DataToDecrypt.Length);
cryptoStream.Close(); memoryStream.Close();
return memoryStream.ToArray();
}
#endregion
#region AES256 Encryption
private static byte[] aesKey = new byte[32] { 206, 156, 144, 76, 93, 170, 62, 42, 240, 216, 24, 84, 160, 145, 53, 37,
28, 37, 139, 150, 18, 10, 84, 151, 239, 29, 123, 240, 13, 168, 40, 72};
private static byte[] aesIV = new byte[16] { 153, 54, 221, 187, 162, 171, 189, 223, 248, 123, 97, 197, 148, 196, 29, 59 };
private static byte[] AesEncrypt(byte[] inputArray)
{
byte[] encrypted = null;
using (AesCryptoServiceProvider cryptoProvider = new AesCryptoServiceProvider())
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(aesKey, aesIV), CryptoStreamMode.Write))
{
cryptoStream.Write(inputArray, 0, inputArray.Length);
}
encrypted = memoryStream.ToArray();
}
}
return encrypted;
}
private static byte[] AesDecrypt(byte[] DataToDecrypt)
{
byte[] decrypted = null;
using (AesCryptoServiceProvider cryptoProvider = new AesCryptoServiceProvider())
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(aesKey, aesIV), CryptoStreamMode.Write))
{
cryptoStream.Write(DataToDecrypt, 0, DataToDecrypt.Length);
}
decrypted = memoryStream.ToArray();
}
}
return decrypted;
}
#endregion
}
public enum EncryptionAlgorithm
{
Des64,
Aes256,
None
}
public class TESEncryption
{
private static string key = "safemobileTriple";
public static string Encryption(string input)
{
return Encrypt(input, key);
}
public static string Encrypt(string input, string key)
{
byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);
TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
tripleDES.Mode = CipherMode.ECB;
tripleDES.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tripleDES.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
tripleDES.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string Decrypt(string input)
{
return Decrypt(input, key);
}
public static string Decrypt(string input, string key)
{
byte[] inputArray = Convert.FromBase64String(input);
TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
tripleDES.Mode = CipherMode.ECB;
tripleDES.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tripleDES.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
tripleDES.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
}