A string is a frequently used type. How to save the string to be the safest? The answer is encryption.
You can use cryptostream in C # to encrypt and decrypt strings. The cryptostream method is as follows:
The program running result is as follows:
The response code for the main window is as follows:
Private void btn_encrypt_click (Object sender, eventargs e) {If (txt_password.text.length = 4) // determine whether the encryption key length is correct {try {txt_encryptstr.text = // call the instance toencrypt method to obtain the encrypted string new encrypt (). toencrypt (txt_password.text, txt_str.text); // encrypt p_encrypt = new encrypt (); // p_encrypt.toencrypt (""} catch (exception ex) // catch exception {MessageBox. show (ex. message); // output exception information} else {MessageBox. show ("the key length does not match! "," Prompt "); // prompts the user to enter an incorrect key length} private void btn_unencrypt_click (Object sender, eventargs e) {If (txt_password2.text.length = 4) // determine whether the encryption key length is correct {try {txt_str2.text = // call the todecrypt method to obtain the decrypted string new encrypt (). todecrypt (txt_password2.text, txt_encryptstr2.text);} catch (exception ex) // catch exceptions {MessageBox. show (ex. message); // output exception information} else {MessageBox. show ("the key length does not match! "," Prompt "); // The system prompts the user to enter an incorrect key length }}
The custom class code is as follows:
Public class encrypt {internal string toencrypt (string encryptkey, string Str) {try {byte [] p_byte_key = // convert the key string to the byte sequence encoding. unicode. getbytes (encryptkey); byte [] p_byte_data = // convert the string to the byte sequence encoding. unicode. getbytes (STR); memorystream p_stream_ms = // create a memory stream object new memorystream (); cryptostream p_cryptstream_stream = // create an encrypted Stream object new cryptostream (p_stream_ms, new descryptoserviceprovider (). createencryptor (p_byte_key, p_byte_key), cryptostreammode. write); p_cryptstream_stream.write (// write the byte sequence p_byte_data, 0, p_byte_data.length) to the encrypted stream; p_cryptstream_stream.flushfinalblock (); // press data into the basic stream byte [] p_bt_temp = // obtain the byte sequence p_stream_ms.toarray (); p_cryptstream_stream.close (); // close the encrypted stream p_stream_ms.close (); // disable the memory stream return // method to return the encrypted string convert. tobase64string (p_bt_temp);} catch (cryptographicexception CE) {Throw new exception (CE. message) ;}} internal string todecrypt (string encryptkey, string Str) {try {byte [] p_byte_key = // convert the key string to the byte sequence encoding. unicode. getbytes (encryptkey); byte [] p_byte_data = // convert the encrypted string to the byte sequence convert. frombase64string (STR); memorystream p_stream_ms = // create a memory stream object and write data to new memorystream (p_byte_data); cryptostream p_cryptstream_stream = // create an encrypted Stream object new cryptostream (p_stream_ms, new descryptoserviceprovider (). createdecryptor (p_byte_key, p_byte_key), cryptostreammode. read); byte [] p_bt_temp = new byte [200]; // create a byte sequence object memorystream p_memorystream_temp = // create a memory stream object new memorystream (); int I = 0; // create the while (I = p_cryptstream_stream.read (// use the while loop to obtain the decrypted data p_bt_temp, 0, p_bt_temp.length)> 0) {p_memorystream_temp.write (// put the decrypted data into the memory stream p_bt_temp, 0, I);} return // return the decrypted string encoding. unicode. getstring (p_memorystream_temp.toarray ();} catch (cryptographicexception CE) {Throw new exception (CE. message );}}}
Code: Download