Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Security.Cryptography;
- using System.IO;
- using System.Linq;
- namespace Gabs.Helpers
- {
- public static class EncryptionRijndael
- {
- private const string EncryptionKey = "gabs123";
- // This constant is used to determine the keysize of the encryption algorithm in bits.
- // We divide this by 8 within the code below to get the equivalent number of bytes.
- private const int Keysize = 256;
- // This constant determines the number of iterations for the password bytes generation function.
- private const int DerivationIterations = 1000;
- public static string Encrypt(string clearText, string passPhrase = EncryptionKey)
- {
- // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
- // so that the same Salt and IV values can be used when decrypting.
- var saltStringBytes = Generate256BitsOfRandomEntropy();
- var ivStringBytes = Generate256BitsOfRandomEntropy();
- var plainTextBytes = Encoding.UTF8.GetBytes(clearText);
- using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
- {
- var keyBytes = password.GetBytes(Keysize / 8);
- using (var symmetricKey = new RijndaelManaged())
- {
- symmetricKey.BlockSize = 256;
- symmetricKey.Mode = CipherMode.CBC;
- symmetricKey.Padding = PaddingMode.PKCS7;
- using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
- {
- using (var memoryStream = new MemoryStream())
- {
- using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
- {
- cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
- cryptoStream.FlushFinalBlock();
- // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
- var cipherTextBytes = saltStringBytes;
- cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
- cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
- memoryStream.Close();
- cryptoStream.Close();
- return Convert.ToBase64String(cipherTextBytes);
- }
- }
- }
- }
- }
- }
- public static string Decrypt(string cipherText, string passPhrase = EncryptionKey)
- {
- // Get the complete stream of bytes that represent:
- // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
- var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
- // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
- var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
- // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
- var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
- // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
- var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
- using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
- {
- var keyBytes = password.GetBytes(Keysize / 8);
- using (var symmetricKey = new RijndaelManaged())
- {
- symmetricKey.BlockSize = 256;
- symmetricKey.Mode = CipherMode.CBC;
- symmetricKey.Padding = PaddingMode.PKCS7;
- using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
- {
- using (var memoryStream = new MemoryStream(cipherTextBytes))
- {
- using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
- {
- var plainTextBytes = new byte[cipherTextBytes.Length];
- var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
- memoryStream.Close();
- cryptoStream.Close();
- return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
- }
- }
- }
- }
- }
- }
- private static byte[] Generate256BitsOfRandomEntropy()
- {
- var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
- using (var rngCsp = new RNGCryptoServiceProvider())
- {
- // Fill the array with cryptographically secure random bytes.
- rngCsp.GetBytes(randomBytes);
- }
- return randomBytes;
- }
- }
- public static class EncryptionAES
- {
- private const string EncryptionKey = "gabs123";
- public static string Encrypt(string clearText, string passPhrase = EncryptionKey)
- {
- byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
- using (Aes encryptor = Aes.Create())
- {
- Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(passPhrase, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
- encryptor.Key = pdb.GetBytes(32);
- encryptor.IV = pdb.GetBytes(16);
- using (MemoryStream ms = new MemoryStream())
- {
- using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
- {
- cs.Write(clearBytes, 0, clearBytes.Length);
- cs.Close();
- }
- clearText = Convert.ToBase64String(ms.ToArray());
- }
- }
- return clearText;
- }
- public static string Decrypt(string cipherText, string passPhrase = EncryptionKey)
- {
- cipherText = cipherText.Replace(" ", "+");
- byte[] cipherBytes = Convert.FromBase64String(cipherText);
- using (Aes encryptor = Aes.Create())
- {
- Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(passPhrase, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
- encryptor.Key = pdb.GetBytes(32);
- encryptor.IV = pdb.GetBytes(16);
- using (MemoryStream ms = new MemoryStream())
- {
- using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
- {
- cs.Write(cipherBytes, 0, cipherBytes.Length);
- cs.Close();
- }
- cipherText = Encoding.Unicode.GetString(ms.ToArray());
- }
- }
- return cipherText;
- }
- }
- public static class EncryptionForUrl
- {
- private const string EncryptionKey = "gabs1234"; // Strictly 8 characters
- private static byte[] key = { };
- private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
- public static string Encrypt(string clearText, string passPhrase = EncryptionKey)
- {
- passPhrase = FixPassphrase(passPhrase);
- try
- {
- key = System.Text.Encoding.UTF8.GetBytes(passPhrase);
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] inputByteArray = Encoding.UTF8.GetBytes(clearText);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms,
- des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- return System.Web.HttpUtility.UrlEncode(Convert.ToBase64String(ms.ToArray())); //.Replace('+', '-').Replace('/', '_'));
- }
- catch (Exception e)
- {
- return e.Message;
- }
- }
- public static string Decrypt(string cipherText, string passPhrase = EncryptionKey)
- {
- passPhrase = FixPassphrase(passPhrase);
- cipherText = System.Web.HttpUtility.UrlDecode(cipherText); //.Replace('-', '+').Replace('_', '/');
- byte[] inputByteArray = new byte[cipherText.Length + 1];
- try
- {
- key = System.Text.Encoding.UTF8.GetBytes(passPhrase);
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- inputByteArray = Convert.FromBase64String(cipherText);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms,
- des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- System.Text.Encoding encoding = System.Text.Encoding.UTF8;
- return encoding.GetString(ms.ToArray());
- }
- catch (Exception e)
- {
- return e.Message;
- }
- }
- private static string FixPassphrase(string passPhrase)
- {
- if (passPhrase.Length > 8)
- passPhrase = new string(passPhrase.Take(8).ToArray());
- else if (passPhrase.Length > 0 && passPhrase.Length < 8)
- passPhrase = passPhrase + (new String('0', 8 - passPhrase.Length));
- else if (string.IsNullOrWhiteSpace(passPhrase))
- passPhrase = EncryptionKey;
- return passPhrase;
- }
- }
- public static class EncodingForBase64
- {
- public static string Encode(string plainText)
- {
- var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
- return System.Convert.ToBase64String(plainTextBytes);
- }
- public static string Decode(string base64EncodedData)
- {
- var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
- return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement