none
Encrypt items in list box, then decrypt after double-clicking RRS feed

  • Question

  • I am very new to .Net, so bare with me please.  I have to list a range of numbers in a listbox that are encrypted.  When the user double-clicks an encryption in the listbox, the decrypted value must be displayed in a textbox.  I have been googling for help, but so far I have come up empty.  any help would be greatly appreciated.
    Thursday, August 8, 2013 1:34 PM

Answers

  • Hi stevetot10,

    Welcome to the MSDN forum.

    Per your description, I think you want to know how to encrypt a string and how to decrypt a string.

    We can use DES Class to encrypt a string and how to decrypt a string.

    You can try the following steps:

    1. Create a new instance of the TripleDESCryptoServiceProvider class. This generates a new key and initialization vector (IV). 
    2. Encrypt the string to an array of bytes.
    3. Decrypt the bytes to a string. 

    I give a sample to solve your issue.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Windows.Forms;
    namespace decryptString
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            
            static  byte[] bytes;
            TripleDESCryptoServiceProvider myTripleDES = new TripleDESCryptoServiceProvider();
            private void Form1_Load(object sender, EventArgs e)
            {
                bytes = EncryptStringToBytes("a",myTripleDES.Key,myTripleDES.IV);
                StringBuilder sb = new StringBuilder();
                foreach (var item in bytes)
                {
                    sb.Append(item);
                }
                this.listBox1.Items.Add(sb.ToString());
            }
            static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
            {
                // Check arguments. 
                if (plainText == null || plainText.Length <= 0)
                    throw new ArgumentNullException("plainText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("Key");
                byte[] encrypted;
                // Create an TripleDESCryptoServiceProvider object 
                // with the specified key and IV. 
                using (TripleDESCryptoServiceProvider tdsAlg = new TripleDESCryptoServiceProvider())
                {
                    tdsAlg.Key = Key;
                    tdsAlg.IV = IV;
                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform encryptor = tdsAlg.CreateEncryptor(tdsAlg.Key, tdsAlg.IV);
                    // Create the streams used for encryption. 
                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                //Write all data to the stream.
                                swEncrypt.Write(plainText);
                            }
                            encrypted = msEncrypt.ToArray();
                        }
                    }
                }
                // Return the encrypted bytes from the memory stream. 
                return encrypted;
            }
            static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
            {
                // Check arguments. 
                if (cipherText == null || cipherText.Length <= 0)
                    throw new ArgumentNullException("cipherText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("Key");
                // Declare the string used to hold 
                // the decrypted text. 
                string plaintext = null;
                // Create an TripleDESCryptoServiceProvider object 
                // with the specified key and IV. 
                using (TripleDESCryptoServiceProvider tdsAlg = new TripleDESCryptoServiceProvider())
                {
                    tdsAlg.Key = Key;
                    tdsAlg.IV = IV;
                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform decryptor = tdsAlg.CreateDecryptor(tdsAlg.Key, tdsAlg.IV);
                    // Create the streams used for decryption. 
                    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                // Read the decrypted bytes from the decrypting stream 
                                // and place them in a string.
                                plaintext = srDecrypt.ReadToEnd();
                            }
                        }
                    }
                }
                return plaintext;
            }
            private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                this.textBox1.Text = DecryptStringFromBytes(bytes,myTripleDES.Key,myTripleDES.IV);
            }
        }
    }

    Have a nice day!

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 9, 2013 10:02 AM

All replies

  • Hi stevetot10,

    Welcome to the MSDN forum.

    Per your description, I think you want to know how to encrypt a string and how to decrypt a string.

    We can use DES Class to encrypt a string and how to decrypt a string.

    You can try the following steps:

    1. Create a new instance of the TripleDESCryptoServiceProvider class. This generates a new key and initialization vector (IV). 
    2. Encrypt the string to an array of bytes.
    3. Decrypt the bytes to a string. 

    I give a sample to solve your issue.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Windows.Forms;
    namespace decryptString
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            
            static  byte[] bytes;
            TripleDESCryptoServiceProvider myTripleDES = new TripleDESCryptoServiceProvider();
            private void Form1_Load(object sender, EventArgs e)
            {
                bytes = EncryptStringToBytes("a",myTripleDES.Key,myTripleDES.IV);
                StringBuilder sb = new StringBuilder();
                foreach (var item in bytes)
                {
                    sb.Append(item);
                }
                this.listBox1.Items.Add(sb.ToString());
            }
            static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
            {
                // Check arguments. 
                if (plainText == null || plainText.Length <= 0)
                    throw new ArgumentNullException("plainText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("Key");
                byte[] encrypted;
                // Create an TripleDESCryptoServiceProvider object 
                // with the specified key and IV. 
                using (TripleDESCryptoServiceProvider tdsAlg = new TripleDESCryptoServiceProvider())
                {
                    tdsAlg.Key = Key;
                    tdsAlg.IV = IV;
                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform encryptor = tdsAlg.CreateEncryptor(tdsAlg.Key, tdsAlg.IV);
                    // Create the streams used for encryption. 
                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                //Write all data to the stream.
                                swEncrypt.Write(plainText);
                            }
                            encrypted = msEncrypt.ToArray();
                        }
                    }
                }
                // Return the encrypted bytes from the memory stream. 
                return encrypted;
            }
            static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
            {
                // Check arguments. 
                if (cipherText == null || cipherText.Length <= 0)
                    throw new ArgumentNullException("cipherText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("Key");
                // Declare the string used to hold 
                // the decrypted text. 
                string plaintext = null;
                // Create an TripleDESCryptoServiceProvider object 
                // with the specified key and IV. 
                using (TripleDESCryptoServiceProvider tdsAlg = new TripleDESCryptoServiceProvider())
                {
                    tdsAlg.Key = Key;
                    tdsAlg.IV = IV;
                    // Create a decrytor to perform the stream transform.
                    ICryptoTransform decryptor = tdsAlg.CreateDecryptor(tdsAlg.Key, tdsAlg.IV);
                    // Create the streams used for decryption. 
                    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                // Read the decrypted bytes from the decrypting stream 
                                // and place them in a string.
                                plaintext = srDecrypt.ReadToEnd();
                            }
                        }
                    }
                }
                return plaintext;
            }
            private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                this.textBox1.Text = DecryptStringFromBytes(bytes,myTripleDES.Key,myTripleDES.IV);
            }
        }
    }

    Have a nice day!

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 9, 2013 10:02 AM
  • Hi stevetot10,

    I have not received your reply a few days

    I am writing to check the status of the issue. What about this problem now?

    I will temporarily mark my last response as an answer. You can unmark it if they provide no help.

    Have a nice day!

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 14, 2013 9:24 AM