Thread: [C#] howto encrypth and decrypth a string value

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55

    [C#] howto encrypth and decrypth a string value

    Hello everybody.

    I'm bussy with learning the basics about Classes and howto use other Classes.
    to practice i would like to make a program that encrypth a string and can decrypth a string value
    by using: System.Security.Cryptography. but i dont know where to start :P

    Could annyone provide me a simple tutorial or other explanation about how i have to handle it.

    I looked at google to but all i found was prof stuff :P

    thanks for your support

    Jelte.
    Last edited by Jelte; 09-18-2008 at 01:17 PM. Reason: Grammar =)

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think that this is probably a good place to start.

  3. #3
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    mmm maybe it was smart to tell that i am Dutch and my English is not like yours. but it is understandable, isn't it?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok fair enough. My dutch is way horrible so I won't give you a hard time.
    MSDN is always a good resource to start out with. Also I found this link for a tutorial (don't worry, it isn't a sarcastic link this time).

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058

  6. #6
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Thank you very much BobS0327 and master5001. it helps alot !

    but know i made this by using a sample code http://www.dotnetspider.com/resource...pt-string.aspx

    Code:
    class Encryption
        {
            public string Crypter(string Input)
            {
                //maakt werk object TextToUni.
                System.Text.UTF8Encoding TextToUni = new System.Text.UTF8Encoding();
                //Haalt de Bytes uit TextToUni en slaat het op.
                byte [] Bytes = TextToUni.GetBytes(Input);
    
                //maakt werkobject aan Encoder voor het encrypten.
                System.Security.Cryptography.MD5CryptoServiceProvider Crypther = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte [] ByteHash = Crypther.ComputeHash(Bytes);
    
                string output = Convert.ToString(ByteHash);
                return (output);
            }
    
       
    
        }
    But now I want that the returning value: output, will be showed in a textbox named tbTextNormal. i cant get that done.

    thank you for your help.

    Jelte.
    Last edited by Jelte; 09-18-2008 at 01:43 PM.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    tbTextNormal.Text = output, right? What exactly is the point in which it is not working?

  8. #8
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Where do I have to declare that: tbTextNormal.Text = output. Cause if I place it in Form1.cs by the botton click event is does not recognize. i can't reach it

    if you know what I mean :P

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I get what you mean. Why is it in the click event anyway? Should not you put it in the function that actually does the encryptions/decryptions?

  10. #10
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    this is maybe a very dumb question but I am a beginner :P

    what is return(output) doing. i shall say that it publicate the value that i want to get by using the function, to the rest of the application(outside de class) and so can be used for a textbox for example.

    I hope you understand what I'm saying :P

    could you show me how you show place the returned value output in a textbox ?
    Last edited by Jelte; 09-18-2008 at 02:28 PM.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There are no such things as stupid questions, just rude replies. return(output) is giving you back a string that contains the encrypted version of the input string. So you would call it doing something like:

    Example:
    Code:
    Encryption e;
    tbTextNormal.Text  = e.Crypter("hello world");
    I am no C# guru, so bear with me. I have only written one program that used exactly one module that I wrote in C#. So if my syntax is slightly out of whack, I appologize in advance.

  12. #12
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    i get what you say, but still have the same problem. where do i have declare tbTextNormal.Text = output and on a way that i can reach ouput.

    But nevermind i go to bed, maybe tomorrow I will awake like a C# genius

    master5001 thank you very much for supporting me

    Jelte.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    No problem. Get some rest and we can work on it more tomorrow. Hopefully I wake up a C# genius. I am more of a C guy.

  14. #14
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    could you show me how you show place the returned value output in a textbox ?
    Code:
    using System;
    using System.Text;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class Crypto : Form {
    
        Button buttonDisplayCrypto  = new Button();
        TextBox textBoxCrypto = new TextBox();
        Label labelCrypto = new Label();
    
        public Crypto() {
            buttonDisplayCrypto.Location = new Point(50, 100);
    		buttonDisplayCrypto.Size = new Size(200, 30);
            buttonDisplayCrypto.Text = "Post Crypto Value to Text Box";
    
            buttonDisplayCrypto.Click += new System.EventHandler(this.buttonCalculate_Click);
    
            this.Controls.Add(buttonDisplayCrypto);
    
            textBoxCrypto.Location = new Point(10, 20);
            textBoxCrypto.Size = new Size(250, 30);
            textBoxCrypto.Text = "";
            this.Controls.Add(textBoxCrypto);
    
            labelCrypto.Location = new Point(10, 5);
            labelCrypto.Size = new Size(154, 15);
            labelCrypto.Text = "Crypto Value for Hello World";
            this.Controls.Add(labelCrypto);
        }
    		static private string Crypter(string Input)
        {
            //maakt werk object TextToUni.
            System.Text.UTF8Encoding TextToUni = new System.Text.UTF8Encoding();
            //Haalt de Bytes uit TextToUni en slaat het op.
            byte [] Bytes = TextToUni.GetBytes(Input);
    
            //maakt werkobject aan Encoder voor het encrypten.
            System.Security.Cryptography.MD5CryptoServiceProvider Crypther =
    				new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte [] ByteHash = Crypther.ComputeHash(Bytes);
            StringBuilder sBuilder = new StringBuilder();
            for(int i = 0; i < ByteHash.Length; i++)
            {
                sBuilder.Append(ByteHash[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
    
        private void buttonCalculate_Click(object sender, System.EventArgs e) {
       		textBoxCrypto.Text = Crypter("Hello World");
        }
    
        public static void Main(string[] args) {
            Application.Run(new Crypto());
        }
    }

  15. #15
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Thanks BobS032 it's working

    but one little question.

    Could you tell me what this is doing ?
    Code:
    StringBuilder sBuilder = new StringBuilder();
            for(int i = 0; i < ByteHash.Length; i++)
            {
                sBuilder.Append(ByteHash[i].ToString("x2"));
            }
            return sBuilder.ToString();
    And what means x2? :P
    Last edited by Jelte; 09-19-2008 at 03:40 AM.

Popular pages Recent additions subscribe to a feed