How to Encrypt and Decrypt String in C#.NET

In Cryptography, Encryption is the process of transforming information using an algorithm to make it unreadable to anyone except those possessing special knowledge, usually referred to as a key. The result of the process is encrypted information. And the reversed form of Encryption is called Decryption. The process of making the encrypted information readable again is called Decryption. So my friends! How to use this technique in .NET so that we can make our confidential information unreadable to others and also we can easily change the information in readable form when required. Today I am going to share a project on Encryption and Decryption in .NET.

Free Download

Encryption and Decryption: Demo Project

Controls and Properties 

<form id="form1" runat="server">

        <asp:Panel ID="Panel1" runat="server" BackColor="#FFCCFF"
          Height="264px" Width="511px">

            <asp:Label ID="Label4" runat="server" Text=
             "Enter a string" Font-Bold="True" Font-Italic="False"
              Font-Names="Verdana" Font-Size="11pt"></asp:Label>
            <asp:Label ID="Label5" runat="server" Font-Bold="True"
               Font-Names="Tahoma"  Text=":"></asp:Label>
            <asp:TextBox ID="txtString" runat="server" BackColor="#CC6699"
                Font-Names="Verdana" Font-Size="11pt" Width="228px"></asp:TextBox>
            <br />

            <asp:Button ID="btnencrypt" runat="server" BackColor="#6699FF" Font-Bold="True"
                Font-Names="Verdana" Font-Size="11pt" onclick="btnencrypt_Click"
                Text="Encrypt" />
            <br />
            <br />

            <asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Italic="False"
                Font-Names="Verdana" Font-Size="11pt" Text="Encrypted String"></asp:Label>
            <asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Names="Tahoma"
            <asp:TextBox ID="txtString0" runat="server" BackColor="#CC6699"
                Font-Names="Verdana" Font-Size="11pt" Width="228px"></asp:TextBox>
            <br />

            <asp:Button ID="btndecrypt" runat="server" BackColor="#6699FF" Font-Bold="True"
                Font-Names="Verdana" Font-Size="11pt" onclick="btndecrypt_Click"
                Text="Decrypt" />
            <br />
            <br />

            <asp:Label ID="Label6" runat="server" Font-Bold="True" Font-Italic="False"
                Font-Names="Verdana" Font-Size="11pt" Text="Decrypted String"></asp:Label>
            <asp:Label ID="Label7" runat="server" Font-Bold="True" Font-Names="Tahoma"
            <asp:TextBox ID="txtString1" runat="server" BackColor="#CC6699"
                Font-Names="Verdana" Font-Size="11pt" Width="228px"></asp:TextBox>



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

aspx.cs Page

public string

 encrypPassword(string sData)

                             byte[] encData_byte =new byte[sData.Length];

            encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);

                            string encodedData = Convert.ToBase64String(encData_byte);

                            return encodedData;


               catch (Exception ex)


                            throw new

            Exception("Error in base64Encode" + ex.Message);

public string decryptPassword(string sData)

        System.Text.UTF8Encoding encoder =new System.Text.UTF8Encoding();
        System.Text.Decoder utf8Decode = encoder.GetDecoder();

                   byte[] todecode_byte = Convert.FromBase64String(sData);

                   int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

                 char[] decoded_char =new char[charCount];

        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);

                string result =new string(decoded_char);

               return result;


protected void btnencrypt_Click(object sender, EventArgs e)


               string data = txtString.Text.Trim();

      txtString0.Text=  encrypPassword(data);

protected void btndecrypt_Click(object sender, EventArgs e)


             string data = txtString0.Text.Trim();

     txtString1.Text = decryptPassword(data);

I am Samith Jhon a content writer and a Professional Blogger.  Certified with IBM 000-119 Exam test questions which is very popular these days and have a great scope in the field of IT Certification. I always like to take certification Exam test questions and now I have planned to pass IBM 000-189 Exam test questions. These kinds of Exam test questions could secure your future as well as your job.

admin (157 Posts)

Comments ( 23 )

  1. ZoeAugust 1, 2011 15 // Reply

    This is no more encryption than ROT13 is. It will not make ANYTHing "unreadable to anyone except those possessing special knowledge" as anyone can just disassemble the binary and see the exact algorithm you're using and get right at the plain text data. It's obfuscation, and pretty bad obfuscation at that. Usable for situations where you don't want users to see the plaintext, but don't care if they find it out anyway. NOT for anything requiring actual encryption.

  2. Tim SkaugeAugust 1, 2011 15 // Reply

    Nobody should use the code you provided in this blog post. What you do is not encrypt and decrypt a string - it's merely a base64 encoding and decoding. If you were to do an encryption and decryption you would need some kind of key. Kudos for writing about how to do it - it's a good topic - but please sent a bit more time to find out how to do proper encryption and decryption.

  3. jorgeAugust 1, 2011 15 // Reply

    catch (Exception ex) { throw new Exception("Error in base64Encode" + ex.Message); } The above is plain wrong. If there is an error in base64 encode, let the user know what it was. In the worst case, you should be doing this: throw new Exception("Error in base64", ex);

  4. Steve ArdisAugust 2, 2011 15 // Reply

    Base64 != encryption It is a way of encoding binary data and there is nothing secret or private about it. Look into PKI (public/private key encryption) or a shared symmetric key.

  5. free ipad 2August 3, 2011 15 // Reply

    GREAT REVIEW! I totally agree with all you said in your post, especially at the end of your article. Thank you, your post is very valuable as always. Keep up the good work! You've got +1 more reader of your blog:) Isabella S.

  6. jazzAugust 4, 2011 15 // Reply

    Yeah i agree with tim and zoe. this is no encryption, as it use no special key, rather it uses utf8 encoding, i.e just changing the form of display to the user. its merely of no use. Infact you should use >System.Security >System.Security.Cryptography to use DES encryption and other. Regards, and yeah base64 is no mode of encryption. its totally waste of time to implement this.

  7. Lakeisha CasarellaAugust 4, 2011 15 // Reply

    Outstanding post, I believe blog owners should learn a lot from this web site its rattling user genial . "A happy childhood has spoiled many a promising life." by Robertson Davies.

  8. win a free 1000 walmart gift cardAugust 5, 2011 15 // Reply

    Do you have a Facebook page or Twitter? Would love to follow you there, I'm on my iPhone and love reading your stuff!

    • Sourabh SharmaAugust 5, 2011 15 // Reply

      Hello, First thanks for your response. and Yes I am on facebook and Twitter... you can follow me there... you can get my profile from footer of this site...

  9. iPad2 giveawayAugust 5, 2011 15 // Reply

    Great review! This is exactly the type of post that should be shared around the web. Sad on the Google for not positioning this blog post higher!

  10. MingAugust 6, 2011 15 // Reply

    using base64 can be only say encode&decode , but not encrypt/decrypt...

  11. HassanAugust 6, 2011 15 // Reply

    I need to thank you a whole lot for that job you have made in writing this posting. I am hoping the same perfect work by you in the future as well.

  12. Joshua AmeroAugust 8, 2011 15 // Reply

    Normally I do not read article on blogs, however I would like to say that this write-up very compelled me to take a look at and do it! Your writing taste has been surprised me. Thanks, quite nice post.

  13. bernina sewing machinesAugust 9, 2011 15 // Reply

    Hello. impressive job. I did not anticipate this. This is a great story. Thanks!

  14. betsson poker voucher codeAugust 10, 2011 16 // Reply

    Some truly good articles on this site, regards for contribution.

  15. tv onlineAugust 12, 2011 16 // Reply

    I don't know what to say to be good. Everyone can to have an oppinion, i say just our oppinion is not the same.

  16. Hye BeaAugust 13, 2011 16 // Reply

    I am not very superb with English but I line up this rattling easygoing to understand.

  17. {Georgia Health Insurance|Health Insurance}August 14, 2011 16 // Reply

    I was reading some of your posts on this website and I believe this website is very informative ! Keep on putting up.

  18. büyük kral jo youngAugust 15, 2011 16 // Reply

    I like the helpful information you provide in your articles. I’ll bookmark your blog and check again here regularly. I am quite sure I’ll learn lots of new stuff right here! Best of luck for the next!

  19. Andrew J HulseAugust 16, 2011 16 // Reply

    The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  20. cheap vigrx capsulesAugust 17, 2011 16 // Reply

    This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article

Leave a reply

Your email address will not be published.

Comment moderation is enabled. Your comment may take some time to appear.