This topic has not yet been rated - Rate this topic

Encrypting and Decrypting Data

To encrypt and decrypt data, you must use a key with an encryption algorithm that performs a transformation on the data. The .NET Framework provides several classes that enable you to perform cryptographic transformations on data using several standard algorithms. This section describes how to create and manage keys and how to encrypt and decrypt data using public-key and secret-key algorithms.

Generating Keys for Encryption and Decryption

Describes symmetric and asymmetric algorithms used for encryption and decryption.

How to: Store Asymmetric Keys in a Key Container

Describes how to store private keys in a key container.

Encrypting Data

Explains how to do symmetric and asymmetric encryption.

Decrypting Data

Explains how to do symmetric and asymmetric decryption.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
encryption and decryption data
using System;
using System.Xml;
using System.Text;
using System.Security.Cryptography;

///
/// Summary description for converter
///
public class converter
{
public converter()
{
//
// TODO: Add constructor logic here
//
}

public static string Decrypt(string TextToBeDecrypted)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();

string Password = "CSC";
string DecryptedData;

try
{
byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);

byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
//Making of the key for decryption
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
//Creates a symmetric Rijndael decryptor object(chahal).
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));

MemoryStream memoryStream = new MemoryStream(EncryptedData);
//Defines the cryptographics stream for decryption.THe stream contains decrpted data
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);

byte[] PlainText = new byte[EncryptedData.Length];
int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
memoryStream.Close();
cryptoStream.Close();

//Converting to string
DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
}
catch
{
DecryptedData = TextToBeDecrypted;
}
return DecryptedData;
}

public static string Encrypt(string TextToBeEncrypted)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
string Password = "CSC";
byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(TextToBeEncrypted);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
//Creates a symmetric encryptor object(chahal).
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream();
//Defines a stream that links data streams to cryptographic transformations
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
cryptoStream.Write(PlainText, 0, PlainText.Length);
//Writes the final state and clears the buffer
cryptoStream.FlushFinalBlock();
byte[] CipherBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string EncryptedData = Convert.ToBase64String(CipherBytes);

return EncryptedData;
}
}