Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Encrypt/Decrypt
- public static class EncryptDecrypt
- {
- // encryption
- public static string EncryptString(string messageToEncrypt, string key_ )
- {
- CryptoStream cryptoStream; MemoryStream memoryStream;
- helper__encrypt_decrypt_stream(out memoryStream, out cryptoStream, secterKey);
- string encryptedText = String.Empty;
- try
- {
- byte[] plainBytes = Encoding.ASCII.GetBytes(plainText); // Convert the plainText string into a byte array
- cryptoStream.Write(plainBytes, 0, plainBytes.Length); // Encrypt the input plaintext string
- cryptoStream.FlushFinalBlock(); // Complete the encryption process
- byte[] cipherBytes = memoryStream.ToArray(); // Convert the encrypted data from a MemoryStream to a byte array
- encryptedText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length); // Convert the encrypted byte array to a base64 encoded string
- }
- catch (Exception e)
- {
- return e.Message;
- }
- finally
- {
- memoryStream.Close();
- cryptoStream.Close();
- }
- return encryptedText;
- }
- public static string DecryptString(string encryptedText, string secterKey)
- {
- CryptoStream cryptoStream; MemoryStream memoryStream;
- helper__encrypt_decrypt_stream(out memoryStream, out cryptoStream, secterKey);
- string plainText = String.Empty;
- try
- byte[] cipherBytes = Convert.FromBase64String(encryptedText);// Convert the encryptedText string into a byte array
- cryptoStream.Write(cipherBytes, 0, cipherBytes.Length); // Decrypt the input encryptedText string
- cryptoStream.FlushFinalBlock(); // Complete the decryption process
- byte[] plainBytes = memoryStream.ToArray(); // Convert the decrypted data from a MemoryStream to a byte array
- plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length); // Convert the decrypted byte array to string
- }
- catch (Exception e)
- {
- return e.Message;
- }
- finally
- {
- memoryStream.Close();
- cryptoStream.Close();
- }
- return plainText;
- }
- public static void helper__encrypt_decrypt_stream(out MemoryStream memoryStream, out CryptoStream cryptoStream, string secterKey)
- {
- SHA256 mySHA256 = SHA256Managed.Create();
- byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(secterKey));
- byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
- // string symmetric encryption
- Aes encryptor = Aes.Create();
- encryptor.Mode = CipherMode.CBC;
- //encryptor.KeySize = 256; encryptor.BlockSize = 128; encryptor.Padding = PaddingMode.Zeros;
- encryptor.Key = key;
- encryptor.IV = iv;
- memoryStream = new MemoryStream();
- cryptoStream = new CryptoStream(memoryStream, encryptor.CreateEncryptor(), CryptoStreamMode.Write); // write to memory stream
- }
- }
Add Comment
Please, Sign In to add comment