Skip to main content

Encrypt and decrypt the text in C#

Encryption of data is mandatory followed in some networks or in saving passwords to the database. So encrypting the data and decrypting the already encrypted code has been logged here.


Here there is two functions Encrypt and Decrypt to convert the data, find the below code for reference.



ASP.Net Code

<html>
<head runat="server">
    <title>Encrypt and Decrypt: fourthbottle</title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
  <table>
   <tr>
    <td>
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </td>
    <td>
     <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </td>
   </tr>
   <tr>
    <td>
    <asp:Button ID="btnencrypt" runat="server" Text="Encrypt"
                        onclick="btnencrypt_Click" />
    </td>
    <td>
    <asp:Button ID="btndecrypt" runat="server" Text="Decrypt"
                        onclick="btndecrypt_Click" />
    </td>
   </tr>
  </table>
  </div>
  </form>
</body>
</html>

C# Code

using System;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;

namespace JSVirtualKeyboard
{
  public partial class WebForm2 : System.Web.UI.Page
   {

    private string Encrypt(string txttoEncrypt)
    {
        string EcriptedData = string.Empty;
        byte[] txt_encode = new byte[txttoEncrypt.Length];
        txt_encode = Encoding.UTF8.GetBytes(txttoEncrypt);
        EcriptedData = Convert.ToBase64String(txt_encode);
        return EcriptedData;
    }

    private string Decrypt(string txttoDecrypt)
    {
        UTF8Encoding encode_pwd = new UTF8Encoding();
        string DecryptedData = string.Empty;
        Decoder Decode = encode_pwd.GetDecoder();
        byte[] todecodeByte = Convert.FromBase64String(txttoDecrypt);
        int charCount = Decode.GetCharCount(
                                            todecodeByte,
                                            0,
                                            todecodeByte.Length
                                            );
        char[] decoded_char = new char[charCount];
        Decode.GetChars(todecodeByte, 0, todecodeByte.Length, decoded_char, 0);
        DecryptedData = new String(decoded_char);
        return DecryptedData;
    }

    protected void btnencrypt_Click(object sender, EventArgs e)
    {
        TextBox2.Text = Encrypt(TextBox1.Text);
    }

    protected void btndecrypt_Click(object sender, EventArgs e)
    {
        TextBox1.Text = Decrypt(TextBox2.Text);
    }

  }
}

Comments

  1. I feel satisfied to read your blog, you have been delivering a useful & unique information to our vision.keep blogging.
    salesforce Training in Bangalore
    uipath Training in Bangalore
    blueprism Training in Bangalore

    ReplyDelete
  2. Good post!Thank you so much for sharing this lovely article.It was so good to read and useful to upgrade my understanding...
    salesforce Training in Bangalore
    uipath Training in Bangalore
    blueprism Training in Bangalore

    ReplyDelete
  3. Awesome Post!!! I really enjoyed reading this article. It's really a nice experience to read your post. Thanks for sharing.
    Data Science Course
    Data Science Course in Marathahalli

    ReplyDelete
  4. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    Data Science Course

    ReplyDelete
  5. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.
    Data Science Certification in Bangalore

    ReplyDelete
  6. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression

    ReplyDelete
  7. Wow what a Great Information about World Day its exceptionally pleasant educational post. a debt of gratitude is in order for the post.
    Data Science Course in Bangalore

    ReplyDelete
  8. It is the intent to provide valuable information and best practices, including an understanding of the regulatory process.
    Data Science Training in Bangalore

    ReplyDelete
  9. This post is very simple to read and appreciate without leaving any details out. Great work!
    You completed certain reliable points there. I did a search on the subject and found nearly all persons will agree with your blog.

    ReplyDelete
  10. I am a new user of this site so here i saw multiple articles and posts posted by this site,I curious more interest in some of them hope you will give more information on this topics in your next articles.
    Data Science Course in Bangalore

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    data science course in indore

    ReplyDelete

Post a Comment