---
title: "Encrypt and Decrypt Data with C#"  
description: "In this blog I’m explaining how to encrypt or decrypt data with C#.  Encryption:Encryption is the process of translating plain text data into somethin"  
author: "Anonymous User"  
published: 2014-08-26  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/687/encrypt-and-decrypt-data-with-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Encrypt and Decrypt Data with C#

In this blog I’m explaining how to encrypt or decrypt data with C#.

\
Encryption:

Encryption is the process of translating plain [text data](https://answers.mindstick.com/qa/112298/how-do-i-implement-sentiment-analysis-in-text-data) into something that appears random and meaning less.

##### Decryption:

Decryption is the process of translating random and meaningless data to plain text.

Why we need to use this [Encryption and decryption](https://www.mindstick.com/forum/158184/how-do-you-implement-session-encryption-and-decryption-in-a-web-application) processes. By using this process we can hide original data and display some junk data based on this we can provide some security for [our data](https://yourviews.mindstick.com/view/70567/data-protection-bill-understanding-our-data).

Here I will explain how to encrypt data and how to save that data into database after that I will show how to decrypt that encrypted [data in database](https://www.mindstick.com/forum/157686/explain-the-various-functions-available-for-manipulating-the-character-data-in-database) and how we can display that decrypted data on form.

\
I have a form with four fields username, password, first name, last name here I am encrypting password data and saving that data into database after that I [am getting](https://www.mindstick.com/forum/34179/i-am-getting-error-while-assigning-the-data-to-the-array-list) from database and decrypting the encrypted password data and displaying that data using [grid view](https://www.mindstick.com/articles/893/data-bound-with-grid-view-in-c-sharp-dot-net).

Default.aspx

```
<div>            Enter Text(Plain/Encrypt) <asp:TextBox ID="input_Text" runat="server"></asp:TextBox>            Result <asp:TextBox ID="output_Text" runat="server"></asp:TextBox><br /><br /><br />            <asp:Button ID="btnEncrypt" runat="server" Text="Encrypt" OnClick="btnEncrypt_Click" />            <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" OnClick="btnDecrypt_Click" />        </div>
```

##### Default.aspx.cs

\

##### Example: How to Encrypt Data in C#

##### \

```
protected void btnEncrypt_Click(object sender, EventArgs e)    {        string strmsg = string.Empty;        byte[] encode = new byte[input_Text.Text.Length];        encode = Encoding.UTF8.GetBytes(input_Text.Text);        strmsg = Convert.ToBase64String(encode);        output_Text.Text = strmsg;    }
```

\

Make byte type array and set [length of array](https://www.mindstick.com/forum/155/find-the-length-of-array) equal to length of [input string](https://www.mindstick.com/forum/72/input-string-was-not-in-a-correct-format).

\

byte[] encode = new byte[input_Text.Text.Length];

\

Change the input string into ASCII values and set into byte type array.

\

encode = Encoding.UTF8.GetBytes(input_Text.Text);

\

Convert byte type array value using ToBase64String() method

\

strmsg = Convert.ToBase64String(encode);

##### Output:

![Encrypt and Decrypt Data with C#](https://www.mindstick.com/blogs/378d0bb8-021d-4995-80a4-94d87c0e5483/images/b1ec0e9a-852e-438a-8591-4dab600c4f43.png)

##### Default.aspx.cs

Example: How to Decrypt Data in C#

```
protected void btnDecrypt_Click(object sender, EventArgs e)    {        string decryptstring = string.Empty;        UTF8Encoding encodestring = new UTF8Encoding();        Decoder Decode = encodestring.GetDecoder();        byte[] todecode_byte = Convert.FromBase64String(input_Text.Text);        int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);        char[] decoded_char = new char[charCount];        Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);        decryptstring = new String(decoded_char);        output_Text.Text = decryptstring;    }
```

##### Output:

![Encrypt and Decrypt Data with C#](https://www.mindstick.com/blogs/378d0bb8-021d-4995-80a4-94d87c0e5483/images/e6cee83f-3a3a-4254-8cec-288e80a6bd2f.png)

in my next post i'll explain about [Introduction of Threading in C#](https://www.mindstick.com/blog/688/Introduction%20of%20Threading%20in%20C#.VPVM2_mUdz8)

---

Original Source: https://www.mindstick.com/blog/687/encrypt-and-decrypt-data-with-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
