How to Encrypt and Decrypt String in C#.NET

Filed in ASP.NET, Web AppTags: , , , , , , , , , , , , ,

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">
    <div>

        <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 />
            &nbsp;&nbsp;
            <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"
                Text=":"></asp:Label>
            <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"
                Text=":"></asp:Label>
            <asp:TextBox ID="txtString1" runat="server" BackColor="#CC6699"
                Font-Names="Verdana" Font-Size="11pt" Width="228px"></asp:TextBox>
        </asp:Panel>

    </div>
    </form>


Namespaces

    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)
 {
        try
        {

                             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.

Sourabh Sharma (150 Posts)


Feedback

Comments

21 Responses to “How to Encrypt and Decrypt String in C#.NET”
  1. Zoe says:

    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 Skauge says:

    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. jorge says:

    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 Ardis says:

    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 2 says:

    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. jazz says:

    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. 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. Do you have a Facebook page or Twitter? Would love to follow you there, I’m on my iPhone and love reading your stuff!

  9. 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. Ming says:

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

  11. Hassan says:

    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 Amero says:

    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. Hello. impressive job. I did not anticipate this. This is a great story. Thanks!

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

  15. tv online says:

    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 Bea says:

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

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

  18. 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. 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. 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

Trackbacks

  1. How to Encrypt and Decrypt String in C#.NET « Netcrema – creme de la social news via digg + delicious + stumpleupon + reddit
  2. How to Encrypt and Decrypt String in C#.NET : Popular Links : eConsultant

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>