Encrypt and Decrypted a String using symetric key

Encrypt and decrypt a string value to a encrypted base64 string and back by Rijndael.

You can use it as snipped. The keys are hard coded for simplification…


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CryptoTest
{
/// <summary>
/// Can encrypt and decrypt a string
/// </summary>
public class CryptoService
{
static byte[] Key = new Guid("{1F2D888B-9566-4F99-92DA-13C37A7ACBDA}").ToByteArray();
static byte[] IV = new Guid("{2A4261C5-A53E-47B3-BF4A-97D944C93813}").ToByteArray();

public string EncryptString(string plainText)
{
// Instantiate a new RijndaelManaged object to perform string symmetric encryption
using (var rijndaelCipher = new RijndaelManaged())
{
// Set key and IV

rijndaelCipher.Key = Key;
rijndaelCipher.IV = IV;

// Instantiate a new MemoryStream object to contain the encrypted bytes
using (var memoryStream = new MemoryStream())
{
// Instantiate a new encryptor from our RijndaelManaged object
using (var rijndaelEncryptor = rijndaelCipher.CreateEncryptor())
{

// Instantiate a new CryptoStream object to process the data and write it to the
// memory stream
using (var cryptoStream = new CryptoStream(memoryStream, rijndaelEncryptor, CryptoStreamMode.Write))
{
// Convert the plainText string into a byte array
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);

// Encrypt the input plaintext string
cryptoStream.Write(plainBytes, 0, plainBytes.Length);

// Complete the encryption process
cryptoStream.FlushFinalBlock();

// Convert the encrypted data from a MemoryStream to a byte array
byte[] cipherBytes = memoryStream.ToArray();

// Convert the encrypted byte array to a base64 encoded string
string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);

// Return the encrypted data as a string
return cipherText;
}
}
}
}
}


public string DecryptString(string cipherText)
{
// Instantiate a new RijndaelManaged object to perform string symmetric encryption
using (RijndaelManaged rijndaelCipher = new RijndaelManaged())
{
// Set key and IV
rijndaelCipher.Key = Key;
rijndaelCipher.IV = IV;

// Instantiate a new MemoryStream object to contain the encrypted bytes
using (var memoryStream = new MemoryStream())
{
// Instantiate a new encryptor from our RijndaelManaged object
using (var rijndaelDecryptor = rijndaelCipher.CreateDecryptor())
{
// Instantiate a new CryptoStream object to process the data and write it to the
// memory stream
using (var cryptoStream = new CryptoStream(memoryStream, rijndaelDecryptor, CryptoStreamMode.Write))
{

// Will contain decrypted plaintext
string plainText = String.Empty;

// Convert the ciphertext string into a byte array
byte[] cipherBytes = Convert.FromBase64String(cipherText);

// Decrypt the input ciphertext string
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);

// Complete the decryption process
cryptoStream.FlushFinalBlock();

// Convert the decrypted data from a MemoryStream to a byte array
byte[] plainBytes = memoryStream.ToArray();

// Convert the encrypted byte array to a base64 encoded string
plainText = Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length);

// Return the encrypted data as a string
return plainText;
}
}
}
}
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: