There are several ways to encrypt and decrypt data but here i am going to use symmetric key ( Rijndael algorithm ) Rijndael class found in namespace called System.Security.Cryptography.
Here is the code with comment inline for understanding.
[java]
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace EncryptionDecrytion
{
class Program
{
static void Main(string[] args)
{
const string inputText = "abcs";
Console.WriteLine("Original string :-- " + inputText);
string enryptedText = SymmetricCryptor.Encrypt(inputText);
Console.WriteLine("Encrypted text :-- "+enryptedText);
string decryptedText = SymmetricCryptor.Decrypt(enryptedText);
Console.WriteLine("Decrypted text :-- " + decryptedText);
Console.ReadLine();
}
}
public class SymmetricCryptor
{
private const string ENCRYPTION_KEY = "123456";
#region Encryption/decryption
/// <summary>
/// The salt value used to strengthen the encryption.
/// </summary>
private static readonly byte[] Salt = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString());
/// <summary>
/// Encrypts any string using the Rijndael algorithm.
/// </summary>
/// <param name="inputText">The string to encrypt.</param>
/// <returns>A Base64 encrypted string.</returns>
public static string Encrypt(string inputText)
{
var rijndaelCipher = new RijndaelManaged();
byte[] plainText = Encoding.Unicode.GetBytes(inputText.Replace(" ", "+"));
//Rfc2898DeriveBytes secretKey1 = new Rfc2898DeriveBytes(ENCRYPTION_KEY, SALT);
var SecretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, Salt);
using (
ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32),
SecretKey.GetBytes(16)))
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainText, 0, plainText.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
}
}
/// <summary>
/// Decrypts a previously encrypted string.
/// </summary>
/// <param name="inputText">The encrypted string to decrypt.</param>
/// <returns>A decrypted string.</returns>
public static string Decrypt(string inputText)
{
var rijndaelCipher = new RijndaelManaged();
byte[] encryptedData = Convert.FromBase64String(inputText);
//Rfc2898DeriveBytes secretKey1 = new Rfc2898DeriveBytes(ENCRYPTION_KEY, SALT);
var secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, Salt);
using (
ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32),
secretKey.GetBytes(16)))
{
using (var memoryStream = new MemoryStream(encryptedData))
{
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
var plainText = new byte[encryptedData.Length];
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
return Encoding.Unicode.GetString(plainText, 0, decryptedCount).Replace("+", " ");
}
}
}
}
#endregion
}
}
[/java]
Good and easy to understand..thanks
ReplyDeleteBeautiful post so will you more information to visit Best fitness apps for men
ReplyDeletePleasant to be going by your web journal once more,Jetbet iran it has been months for me.
ReplyDelete