WinForm Small program------encryption and decryption, CryptoStream () use

Source: Internet
Author: User
Tags key string

One:

Two: Code

Main interface Code

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacestringencrypt{ Public Partial classFrm_main:form { PublicFrm_main () {InitializeComponent (); }        Private voidBtn_encrypt_click (Objectsender, EventArgs e) {            if(Txt_password. Text.length = =4)//determine if the encryption key length is correct            {                Try{Txt_encryptstr.text=//Call instance Toencrypt method to get the encrypted string                        NewEncrypt (). Toencrypt (Txt_password. Text, Txt_str.                    Text); //Encrypt p_encrypt = new Encrypt (); //p_encrypt.toencrypt (""                }                Catch(Exception ex)//Catching exceptions{MessageBox.Show (ex. Message);//Output Exception Information                }            }            Else{MessageBox.Show ("Key length does not match! ","Tips");//The user is prompted to enter the key length incorrectly            }        }        Private voidBtn_unencrypt_click (Objectsender, EventArgs e) {            if(Txt_password2. Text.length = =4)//determine if the encryption key length is correct            {                Try{txt_str2. Text=//call the Todecrypt method to get the decrypted string                        NewEncrypt (). Todecrypt (Txt_password2.                Text, Txt_encryptstr2.text); }                Catch(Exception ex)//Catching exceptions{MessageBox.Show (ex. Message);//Output Exception Information                }            }            Else{MessageBox.Show ("Key length does not match! ","Tips");//The user is prompted to enter the key length incorrectly            }        }    }}

Encrypt and decrypt class code

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Security.Cryptography;usingSystem.Text;namespacestringencrypt{ Public classEncrypt {/// <summary>        ///Encrypt/// </summary>        /// <param name= "Encryptkey" >secret key</param>        /// <param name= "str" >Information</param>        /// <returns></returns>        Internal stringToencrypt (stringEncryptkey,stringstr) {            Try            {                byte[] P_byte_key =//converting a key string to a sequence of bytesEncoding.Unicode.GetBytes (Encryptkey); byte[] P_byte_data =//Convert a string to a sequence of bytesEncoding.Unicode.GetBytes (str); MemoryStream P_stream_ms=//Create a memory stream object                    NewMemoryStream (); CryptoStream P_cryptstream_stream=//creating an encrypted stream object                    NewCryptoStream (P_stream_ms,NewDESCryptoServiceProvider ().                CreateEncryptor (P_byte_key, P_byte_key), cryptostreammode.write); P_cryptstream_stream.write (//writes a sequence of bytes to an encrypted streamP_byte_data,0, P_byte_data.                Length); P_cryptstream_stream.flushfinalblock ();//presses the data into the underlying stream, updates the underlying data source or repository with the current state of the buffer, and then clears the buffer.                 byte[] P_bt_temp =//get a sequence of bytes from a memory streamP_stream_ms.                ToArray (); P_cryptstream_stream.close ();//Close Encrypted StreamP_stream_ms. Close ();//close the memory stream                return //method returns the encrypted stringconvert.tobase64string (p_bt_temp); }            Catch(cryptographicexception CE) {Throw NewException (CE.            Message); }        }        Internal stringTodecrypt (stringEncryptkey,stringstr) {            Try            {                byte[] P_byte_key =//converting a key string to a sequence of bytesEncoding.Unicode.GetBytes (Encryptkey); byte[] P_byte_data =//converts the encrypted string to a sequence of bytesconvert.frombase64string (str); MemoryStream P_stream_ms=//Create a memory stream object and write data                    NewMemoryStream (P_byte_data); CryptoStream P_cryptstream_stream=//creating an encrypted stream object                    NewCryptoStream (P_stream_ms,NewDESCryptoServiceProvider ().                CreateDecryptor (P_byte_key, P_byte_key), cryptostreammode.read); byte[] P_bt_temp =New byte[ $];//creating a byte sequence objectMemoryStream p_memorystream_temp=//Create a memory stream object                    NewMemoryStream (); inti =0;//Creating the Registers                 while((i = P_cryptstream_stream.read (//use while loop to get decrypted dataP_bt_temp,0, P_bt_temp. Length)) >0)//(1\ reads 200 bytes from the current stream-and stores them in the p_bt_temp byte offset in 2\p_bt_temp-stores the total number of bytes read from the current stream 3\ read into the buffer, starting at that offset){p_memorystream_temp. Write (//put the decrypted data into the memory streamP_bt_temp,0, i); }                return //method returns the decrypted stringEncoding.Unicode.GetString (p_memorystream_temp.            ToArray ()); }            Catch(cryptographicexception CE) {Throw NewException (CE.            Message); }        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.