Home > DeveloperSection > Forums > How to encrypt and decrypt string in c#?
Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    October-08-2014 7:30 AM

 C# ASP.Net  C# 
Ratings:


 2 Reply(s)
 1080  View(s)
Rate this:
How to string encrypt and decrypt?


Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-08-2014 7:31 AM

try this encrypt and decrypt method

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Security.Cryptography;

using System.Text;

using System.Web;

 

/// <summary>

/// Summary description for Security

/// </summary>

public class Security

{

    private const int keysize = 256;

    private const string initVector = "tu89geji340t89u2";

 

       public Security()

       {

          

       }

 

    //encrypt method

    public static string Encrypt(string plainText, string passPhrase)

    {

        byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);

        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);

        byte[] keyBytes = password.GetBytes(keysize / 8);

        RijndaelManaged symmetricKey = new RijndaelManaged();

        symmetricKey.Mode = CipherMode.CBC;

        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);

        MemoryStream memoryStream = new MemoryStream();

        CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);

        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

        cryptoStream.FlushFinalBlock();

        byte[] cipherTextBytes = memoryStream.ToArray();

        memoryStream.Close();

        cryptoStream.Close();

        return Convert.ToBase64String(cipherTextBytes);

    }

 

    //decrypt method

    public static string Decrypt(string cipherText)

    {

        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);

        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

        PasswordDeriveBytes password = new PasswordDeriveBytes(initVector, null);

        byte[] keyBytes = password.GetBytes(keysize / 8);

        RijndaelManaged symmetricKey = new RijndaelManaged();

        symmetricKey.Mode = CipherMode.CBC;

        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);

        MemoryStream memoryStream = new MemoryStream(cipherTextBytes);

        CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);

        byte[] plainTextBytes = new byte[cipherTextBytes.Length];

        int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);

        memoryStream.Close();

        cryptoStream.Close();

        return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);

    }

}


Horas Panjaitan

Total Post:3

Points:21
Posted on    November-06-2014 2:14 AM

how to update in the gridview, I have a file that should be in the Update,
What if I click the edit button, the file that it does not appear

Don't want to miss updates? Please click the below button!

Follow MindStick