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. Replies
    1. Great Article
      Cyber Security Projects

      projects for cse

      Networking Security Projects

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. It is the intent to provide valuable information and best practices, including an understanding of the regulatory process.
    Data Science Training in Bangalore

    ReplyDelete
  10. 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
  11. 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
  12. This comment has been removed by the author.

    ReplyDelete
  13. 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
  14. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

    Simple Linear Regression

    Correlation vs Covariance

    ReplyDelete
  15. 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
    data science interview questions

    ReplyDelete
  16. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.data science malaysia

    ReplyDelete
  17. Amazing post found to be very impressive on how to Encrypt and Decrypt the text in C. Thanks for sharing and keep posting such an informative content.

    360DigiTMG Data Analytics Course

    ReplyDelete
  18. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
    analytics

    ReplyDelete
  19. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
    info

    ReplyDelete
  20. I have bookmarked your article, information on the blog was very useful and enjoyed reading it well done thank you.
    Data Science Training in Hyderabad

    ReplyDelete
  21. have bookmarked your website because this site contains valuable information in it. I am really happy with articles quality and presentation. Thanks a lot .
    360DigiTMG Data Analytics Certification

    ReplyDelete
  22. Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work.
    cyber security course training in Guwahati

    ReplyDelete
  23. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! 360DigiTMG

    ReplyDelete
  24. Amazing 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.
    Simple Linear Regression
    Correlation vs covariance
    data science interview questions
    KNN Algorithm
    Logistic Regression explained

    ReplyDelete
  25. This Was An Amazing ! I Haven't Seen This Type of Blog Ever ! Thankyou For Sharing, data science course

    ReplyDelete
  26. Thankyou for this wondrous post, I am cheerful I watched this site on yahoo.
    artificial intelligence course in delhi

    ReplyDelete
  27. 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.data science courses

    ReplyDelete

  28. Fantastic article I ought to say and thanks to the info. Instruction is absolutely a sticky topic. But remains one of the top issues of the time. I love your article and look forward to more.

    Data Science Course In Bangalore With Placement

    ReplyDelete

  29. This Is a Fantastic article, signifying so much information on it, These Kind of posts keeps the users attention from the Site, and continue sharing more... good luck
    Data Science Course In Bangalore With Placement

    ReplyDelete
  30. Additionally, this is an excellent article which I truly like studying. It's not everyday I have the option to see something similar to this.
    Data Science Training Institute in Bangalore

    ReplyDelete

Post a Comment