---
title: "How to encrypt and decrypt string in c#?"  
description: "How to encrypt and decrypt string in c#?"  
author: "Jayden Bell"  
published: 2014-10-08  
updated: 2014-11-06  
canonical: https://www.mindstick.com/forum/2338/how-to-encrypt-and-decrypt-string-in-c-sharp  
category: "c#"  
tags: ["asp.net", "c#"]  
reading_time: 2 minutes  

---

# How to encrypt and decrypt string in c#?

How to [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) encrypt and decrypt?

## Replies

### Reply by Horas Panjaitan

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

### Reply by Anonymous User

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);    }}
```


---

Original Source: https://www.mindstick.com/forum/2338/how-to-encrypt-and-decrypt-string-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
