Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class CryptationUtility
- {
- public static string BytesToString(byte[] bytesToConvert)
- {
- return Encoding.ASCII.GetString(bytesToConvert);
- }
- public static byte[] StringToBytes(string stringToConvert)
- {
- return Encoding.ASCII.GetBytes(stringToConvert);
- }
- public static string ToUrlSafeBase64(byte[] input)
- {
- return Convert.ToBase64String(input).Replace("+", "-").Replace("/", "_");
- }
- public static byte[] FromUrlSafeBase64(string input)
- {
- return Convert.FromBase64String(input.Replace("-", "+").Replace("_", "/"));
- }
- }
- public class AesCryptator : IAesCryptator
- {
- private RijndaelManaged _rijndaelObj;
- public AesCryptator()
- {
- _rijndaelObj = new RijndaelManaged();
- // Set encryption mode to Cipher Block Chaining
- _rijndaelObj.Mode = CipherMode.CBC;
- _rijndaelObj.Padding = PaddingMode.PKCS7;
- }
- public void SetRijndaelObject(RijndaelManaged rijndaelObj)
- {
- _rijndaelObj = rijndaelObj;
- }
- public RijndaelManaged GetRijndaelObject()
- {
- return _rijndaelObj;
- }
- public AesData GetAesData()
- {
- string key = Encoding.ASCII.GetString(_rijndaelObj.Key);
- string iv = Encoding.ASCII.GetString(_rijndaelObj.IV);
- return new AesData(key, iv);
- }
- public string EncryptData(string stringToEncrypt)
- {
- byte[] bytesToEncrypt = CryptationUtility.StringToBytes(stringToEncrypt);
- string encryptedString;
- try
- {
- // Generate encryptor from the existing key bytes and initialization vector.
- ICryptoTransform encryptor = _rijndaelObj.CreateEncryptor();
- // Define memory stream which will be used to hold encrypted data.
- using (var msEncrypt = new MemoryStream())
- {
- // Define cryptographic stream (always use Write mode for encryption).
- using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
- {
- //Define writer to write to stream
- using (var swEncrypt = new StreamWriter(csEncrypt))
- {
- //Write to stream
- swEncrypt.Write(stringToEncrypt);
- //Finish encrypting
- csEncrypt.FlushFinalBlock();
- //Get encrypted data
- byte[] encryptedBytes = msEncrypt.ToArray();
- //Convert to string
- encryptedString = CryptationUtility.ToUrlSafeBase64(encryptedBytes);
- }
- }
- }
- return encryptedString;
- }
- catch (Exception e)
- {
- throw new AesCryptationException("Error while encrypting", e);
- }
- }
- public string DecryptData(string dataToDecrypt)
- {
- //Convert from base64
- byte[] bytesToDecrypt = CryptationUtility.FromUrlSafeBase64(dataToDecrypt);
- string decryptedString;
- try
- {
- // Generate decryptor from the existing key bytes and initialization vector.
- ICryptoTransform decryptor = _rijndaelObj.CreateDecryptor();
- // Define memory stream which will be used to hold encrypted data.
- using (var msDecrypt = new MemoryStream(bytesToDecrypt))
- {
- // Define cryptographic stream (always use Write mode for encryption).
- using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
- {
- //Define writer to write to stream
- using(var srDecrypt = new StreamReader(csDecrypt))
- {
- //Get encrypted data
- decryptedString = srDecrypt.ReadToEnd();
- }
- }
- }
- return decryptedString;
- }
- catch (Exception e)
- {
- throw new AesCryptationException("Error while decrypting", e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement