//on this class, we encrypt and decrypt the message public static EncryptedMessage Cryptograph(byte[] simetricKey, string message) { EncryptedMessage ret = new EncryptedMessage(); using (Aes aes = new AesCryptoServiceProvider()) { aes.Key = simetricKey; ret.iv = CommonBusiness.bytesToString(aes.IV); // Encrypt the message using (MemoryStream ciphertext = new MemoryStream()) using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write)) { byte[] plaintextMessage = Encoding.UTF8.GetBytes(message); cs.Write(plaintextMessage, 0, plaintextMessage.Length); cs.Close(); ret.encryptedText = CommonBusiness.bytesToString(ciphertext.ToArray()); } } return(ret); }
public static string Decryptograph(byte[] simetricKey, EncryptedMessage message) { string ret; using (Aes aes = new AesCryptoServiceProvider()) { aes.Key = simetricKey; aes.IV = CommonBusiness.stringToBytes(message.iv); // Decrypt the message using (MemoryStream plaintext = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Write)) { byte[] encBytes = CommonBusiness.stringToBytes(message.encryptedText); cs.Write(encBytes, 0, encBytes.Length); cs.Close(); ret = Encoding.UTF8.GetString(plaintext.ToArray()); } } } return(ret); }