123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- using System;
- using System.IO;
- using System.Text;
- using System.Security.Cryptography;
- namespace EasyDevCore.Common.Security
- {
- /// <summary>
- ///
- /// </summary>
- public enum SymmetricAlgorithmMethod
- {
- /// <summary>
- /// The DES crypto service provider
- /// </summary>
- DESCryptoServiceProvider,
- /// <summary>
- /// The rijndael managed
- /// </summary>
- RijndaelManaged,
- /// <summary>
- /// The R c2 crypto service provider
- /// </summary>
- RC2CryptoServiceProvider,
- /// <summary>
- /// The triple DES crypto service provider
- /// </summary>
- TripleDESCryptoServiceProvider
- }
- /// <summary>
- ///
- /// </summary>
- public enum AsymmetricAlgorithmMethod
- {
- /// <summary>
- /// The DSA
- /// </summary>
- DSA,
- /// <summary>
- /// The EC diffie hellman
- /// </summary>
- ECDiffieHellman,
- /// <summary>
- /// The EC DSA
- /// </summary>
- ECDsa,
- /// <summary>
- /// The RSA
- /// </summary>
- RSA
- }
- /// <summary>Security Helper</summary>
- public sealed class EncryptionHelper
- {
- #region Private Variables
- //Original key to ensure it complex
- private static string _KeyStringDump = "1AXDHKDASF932CXK";
- #endregion
- #region Public Methods
- /// <summary>
- /// Sets the dump key string - original key to ensure it enough complex.
- /// </summary>
- /// <param name="dumpKey">The dump key.</param>
- public static void SetDumpKeyString(string dumpKey)
- {
- _KeyStringDump = dumpKey;
- }
- private static SymmetricAlgorithm GetCryptorServiceProvider(SymmetricAlgorithmMethod algorithm)
- {
- switch (algorithm)
- {
- case SymmetricAlgorithmMethod.DESCryptoServiceProvider:
- return System.Security.Cryptography.TripleDES.Create();
- case SymmetricAlgorithmMethod.RC2CryptoServiceProvider:
- return System.Security.Cryptography.RC2.Create();
- case SymmetricAlgorithmMethod.RijndaelManaged:
- return System.Security.Cryptography.Aes.Create();
- case SymmetricAlgorithmMethod.TripleDESCryptoServiceProvider:
- return System.Security.Cryptography.TripleDES.Create();
- default:
- throw new NotSupportedException(algorithm.ToString());
- }
- }
- /// <summary>
- /// RSAs the generate keys.
- /// </summary>
- /// <param name="xmlPublicKey">The XML public key.</param>
- /// <param name="xmlPrivateKey">The XML private key.</param>
- /// <param name="keySize">Size of the key.</param>
- public static void RSAGenerateKeys(ref string xmlPublicKey, ref string xmlPrivateKey, int keySize)
- {
- using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize))
- {
- xmlPublicKey = rsa.ToXmlString(false);
- xmlPrivateKey = rsa.ToXmlString(true);
- }
- }
- /// <summary>
- /// RSAs the encrypt data.
- /// </summary>
- /// <param name="xmlPublicKey">The XML public key.</param>
- /// <param name="data">The data.</param>
- /// <returns></returns>
- public static byte[] RSAEncryptData(string xmlPublicKey, byte[] data)
- {
- using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
- {
- rsa.FromXmlString(xmlPublicKey);
- return rsa.Encrypt(data, false);
- }
- }
- /// <summary>
- /// RSAs the encrypt string.
- /// </summary>
- /// <param name="xmlPublicKey">The XML public key.</param>
- /// <param name="dataString">The data string.</param>
- /// <returns></returns>
- public static string RSAEncryptString(string xmlPublicKey, string dataString)
- {
- string valueEncodeString = string.Empty;
- //convert data to byte array
- byte[] byteData = Encoding.UTF8.GetBytes(dataString);
- byte[] encryptedData = RSAEncryptData(xmlPublicKey, byteData);
- //convert to string and Base64 encode
- valueEncodeString = Convert.ToBase64String(encryptedData);
- return valueEncodeString;
- }
- /// <summary>
- /// RSAs the decrypt data.
- /// </summary>
- /// <param name="xmlPrivateKey">The XML private key.</param>
- /// <param name="data">The data.</param>
- /// <returns></returns>
- public static byte[] RSADecryptData(string xmlPrivateKey, byte[] data)
- {
- using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
- {
- rsa.FromXmlString(xmlPrivateKey);
- return rsa.Decrypt(data, false);
- }
- }
- /// <summary>
- /// RSAs the decrypt string.
- /// </summary>
- /// <param name="xmlPPrivateKey">The XML P private key.</param>
- /// <param name="encryptedDataString">The encrypted data string.</param>
- /// <returns></returns>
- public static string RSADecryptString(string xmlPPrivateKey, string encryptedDataString)
- {
- //convert data to byte array
- byte[] encryptedData = Convert.FromBase64String(encryptedDataString);
- byte[] byteData = RSADecryptData(xmlPPrivateKey, encryptedData);
- // convert to string
- System.Text.Encoding objEncoding = System.Text.Encoding.UTF8;
- return objEncoding.GetString(byteData);
- }
- /// <summary>
- /// Encrypts the data.
- /// </summary>
- /// <param name="keyString">The key string.</param>
- /// <param name="algorithm">The algorithm.</param>
- /// <param name="data">The data.</param>
- /// <returns></returns>
- public static byte[] EncryptData(string keyString, SymmetricAlgorithmMethod algorithm, byte[] data)
- {
- string valueEncodeString = string.Empty;
- using (MemoryStream objMemoryStream = new MemoryStream())
- {
- if ((data.Length > 0) && keyString != string.Empty)
- {
- //convert key to atleast KeyString.Length characters for complex
- if (keyString.Length < _KeyStringDump.Length)
- {
- keyString += _KeyStringDump.Substring(0, _KeyStringDump.Length - keyString.Length);
- }
- //create encryption keys
- byte[] byteKey = Encoding.UTF8.GetBytes(keyString.Substring(0, keyString.Length / 2));
- byte[] byteVector = Encoding.UTF8.GetBytes(keyString.Substring(keyString.Length / 2));
- //encrypt
- using (var objCryptor = GetCryptorServiceProvider(algorithm))
- {
- CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objCryptor.CreateEncryptor(byteKey, byteVector), CryptoStreamMode.Write);
- objCryptoStream.Write(data, 0, data.Length);
- objCryptoStream.FlushFinalBlock();
- }
- }
- else
- {
- objMemoryStream.Write(data, 0, data.Length);
- }
- return objMemoryStream.ToArray();
- }
- }
- /// <summary>
- /// Decrypts the data.
- /// </summary>
- /// <param name="keyString">The key string.</param>
- /// <param name="data">The data.</param>
- /// <returns></returns>
- public static byte[] DecryptData(string keyString, byte[] data)
- {
- return DecryptData(keyString, SymmetricAlgorithmMethod.DESCryptoServiceProvider, data);
- }
- /// <summary>
- /// Decrypts the data.
- /// </summary>
- /// <param name="keyString">The key string.</param>
- /// <param name="algorithm">The algorithm.</param>
- /// <param name="data">The data.</param>
- /// <returns></returns>
- public static byte[] DecryptData(string keyString, SymmetricAlgorithmMethod algorithm, byte[] data)
- {
- using (MemoryStream objMemoryStream = new MemoryStream())
- {
- if ((data.Length > 0) && keyString != string.Empty)
- {
- //convert key to atleast KeyString.Length characters for complex
- if (keyString.Length < _KeyStringDump.Length)
- {
- keyString += _KeyStringDump.Substring(0, _KeyStringDump.Length - keyString.Length);
- }
- //create encryption keys
- byte[] byteKey = Encoding.UTF8.GetBytes(keyString.Substring(0, keyString.Length / 2));
- byte[] byteVector = Encoding.UTF8.GetBytes(keyString.Substring(keyString.Length / 2));
-
- // decrypt
- using (var objCryptor = GetCryptorServiceProvider(algorithm))
- {
- CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objCryptor.CreateDecryptor(byteKey, byteVector), CryptoStreamMode.Write);
- objCryptoStream.Write(data, 0, data.Length);
- objCryptoStream.FlushFinalBlock();
- }
- }
- else
- {
- objMemoryStream.Write(data, 0, data.Length);
- }
- return objMemoryStream.ToArray();
- }
- }
- /// <summary>
- /// Encrypts the string.
- /// </summary>
- /// <param name="keyString">The key string.</param>
- /// <param name="dataString">The data string.</param>
- /// <returns></returns>
- public static string EncryptString(string keyString, string dataString)
- {
- string valueEncodeString = string.Empty;
- if (keyString != string.Empty && dataString != null)
- {
- //convert key to atleast KeyString.Length characters for complex
- if (keyString.Length < _KeyStringDump.Length)
- {
- keyString += _KeyStringDump.Substring(0, _KeyStringDump.Length - keyString.Length);
- }
- //create encryption keys
- byte[] byteKey = Encoding.UTF8.GetBytes(keyString.Substring(0, keyString.Length / 2));
- byte[] byteVector = Encoding.UTF8.GetBytes(keyString.Substring(keyString.Length / 2));
- //convert data to byte array
- byte[] byteData = Encoding.UTF8.GetBytes(dataString);
- //encrypt
- using(var objCryptor = GetCryptorServiceProvider(SymmetricAlgorithmMethod.DESCryptoServiceProvider))
- {
- MemoryStream objMemoryStream = new MemoryStream();
- CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objCryptor.CreateEncryptor(byteKey, byteVector), CryptoStreamMode.Write);
- objCryptoStream.Write(byteData, 0, byteData.Length);
- objCryptoStream.FlushFinalBlock();
- //convert to string and Base64 encode
- valueEncodeString = Convert.ToBase64String(objMemoryStream.ToArray());
- }
- }
- else
- {
- valueEncodeString = dataString;
- }
- return valueEncodeString;
- }
- /// <summary>
- /// Decrypts the string.
- /// </summary>
- /// <param name="keyString">The key string.</param>
- /// <param name="dataString">The data string.</param>
- /// <returns></returns>
- public static string DecryptString(string keyString, string dataString)
- {
- if (string.IsNullOrWhiteSpace(dataString))
- {
- return dataString;
- }
- string valueEncodeString = string.Empty;
- if (keyString != string.Empty)
- {
- //convert key to atleast KeyString.Length characters for complex
- if (keyString.Length < _KeyStringDump.Length)
- {
- keyString += _KeyStringDump.Substring(0, _KeyStringDump.Length - keyString.Length);
- }
- //create encryption keys
- byte[] byteKey = Encoding.UTF8.GetBytes(keyString.Substring(0, keyString.Length / 2));
- byte[] byteVector = Encoding.UTF8.GetBytes(keyString.Substring(keyString.Length / 2));
- //convert data to byte array and Base64 decode
- byte[] byteData = new byte[dataString.Length];
- try
- {
- byteData = Convert.FromBase64String(dataString);
- }
- catch // invalid length
- {
- valueEncodeString = dataString;
- }
- if (valueEncodeString == string.Empty)
- {
- try
- {
- // decrypt
- using(var objCryptor = GetCryptorServiceProvider(SymmetricAlgorithmMethod.DESCryptoServiceProvider))
- {
- MemoryStream objMemoryStream = new MemoryStream();
- CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objCryptor.CreateDecryptor(byteKey, byteVector), CryptoStreamMode.Write);
- objCryptoStream.Write(byteData, 0, byteData.Length);
- objCryptoStream.FlushFinalBlock();
- // convert to string
- System.Text.Encoding objEncoding = System.Text.Encoding.UTF8;
- valueEncodeString = objEncoding.GetString(objMemoryStream.ToArray());
- }
- }
- catch // decryption error
- {
- return string.Empty;
- }
- }
- return valueEncodeString;
- }
- else
- {
- return dataString;
- }
- }
- /// <summary>
- /// Complex Encrypts the string.
- /// </summary>
- /// <param name="keyString">The key string.</param>
- /// <param name="dataString">The data string.</param>
- /// <returns></returns>
- public static string ComplexEncryptString(string keyString, string dataString)
- {
- if (string.IsNullOrEmpty(dataString))
- {
- return string.Empty;
- }
- if(dataString.IndexOf((char)0) > -1)
- {
- throw new NotSupportedException($"{nameof(dataString)} contains invalid characters !");
- }
- Random rand = new Random(DateTime.Now.Millisecond);
- int dump = rand.Next(1, 30 - (dataString.Length % 20));
- if (dump > 0)
- {
- string dumpString = string.Empty;
- for (int i = 0; i < dump; i++)
- {
- dumpString += (char)(rand.Next(254) + 1);
- }
- if ((dumpString.Length + dataString.Length) % 2 == 0)
- {
- dataString += (char)0 + dumpString;
- }
- else
- {
- dataString = dumpString + (char)0 + dataString;
- }
- }
- return EncryptString(keyString, dataString);
- }
- /// <summary>
- /// Complex Decrypts the string.
- /// </summary>
- /// <param name="keyString">The key string.</param>
- /// <param name="dataString">The data string.</param>
- /// <returns></returns>
- public static string ComplexDecryptString(string keyString, string dataString)
- {
- if (string.IsNullOrEmpty(dataString))
- {
- return string.Empty;
- }
- string decryptString = DecryptString(keyString, dataString);
- if (dataString.Length > 0 && decryptString.Length == 0) decryptString = dataString;
- int pos = decryptString.IndexOf((char)0);
- if (pos > 0)
- {
- if ((decryptString.Length - 1) % 2 == 0)
- {
- decryptString = decryptString.Substring(0, pos);
- }
- else
- {
- decryptString = decryptString.Substring(pos + 1);
- }
- }
- return decryptString;
- }
- #endregion
- }
- }
|