Secure data with des symmetric encryption algorithm using one of dotnet cipher systems

Source: Internet
Author: User
Tags decrypt dotnet


/////////////////////////////////////////////////////////////
Author:stardicky//
e-mail: [email protected]//
qqnumber:9531511//
Companyname:ezone International//
class:hbs-0308//
Title: Using dotnet Password system to ensure data security//
/////////////////////////////////////////////////////////////
Note: A des symmetric encryption algorithm using one of the dotnet cipher Systems ensures data security//
/////////////////////////////////////////////////////////////

Using System;
Using System.IO;
Using System.Text;
Using System.Security.Cryptography;

Namespace Ezoneinternationalsecuritycryptography
{
Class Ezonesecuritycryptographydemo
{

[STAThread]
public static void Main (string[] args)
{
Encrypt data (from memory to file)
Ezoneencryptordemo ();
Decrypt data (from file to memory)
Ezonedecryptordemo ();
}

<summary>
Encryption
</summary>
public static void Ezoneencryptordemo ()
{
Create a file object, the file's mode is to create a new file, the file access permission is writable!
FileStream fs=new FileStream ("EzoneDemo.txt", filemode.create,fileaccess.write);
Console.WriteLine ("Please enter the string you want to encrypt:");
Enter the string you want to encrypt
String Yourinput=console.readline ();
Convert a string to bytes
Byte[] Yourinputstorage=system.text.encoding.utf8.getbytes (yourinput);
Creating an encryption class for a DES algorithm
DESCryptoServiceProvider myserviceprovider=new DESCryptoServiceProvider ();
From the CreateEncryptor method of the encryption class object of the DES algorithm, create a cryptographic transform interface object
The meaning of the first parameter is: The secret key of the symmetric algorithm (64 bits long, which is 8 bytes)
Can be entered manually, or randomly generated method is: Myserviceprovider.generatekey ();
The second parameter means: the initialization vector of the symmetric algorithm (64 bits long, which is 8 bytes)
Can be entered manually, or randomly generated method is: Myserviceprovider.generateiv ();
ICryptoTransform Mytransform=myserviceprovider.createencryptor (New byte[]{100,110,120,130,100,110,120,130},new byte[]{100,110,120,130,100,110,120,130});
The purpose of the CryptoStream object is to stream the data to the encrypted transform
CryptoStream mycryptostream=new CryptoStream (fs,mytransform,cryptostreammode.write);
Writes data from a byte array to the encrypted stream
Mycryptostream.write (yourinputstorage,0,yourinputstorage.length);
Close encrypted Stream object
Mycryptostream.close ();

}

///<summary> 
///decrypt  
// /</summary> 
public static void Ezonedecryptordemo ()  

FileStream fs=new FileStream ("EzoneDemo.txt", filemode.open,fileaccess.read);  
DESCryptoServiceProvider myserviceprovider=new DESCryptoServiceProvider ();  
//The CreateEncryptor method of the cryptographic class object from the DES algorithm, creates a decryption transformation interface object  
//[The secret key of the symmetric algorithm] Must be encrypted when the [symmetric algorithm's secret key]  the initialization vector of the
//[symmetric algorithm] must be the initialization vector]  of the symmetric algorithm when it is encrypted;
//If not, an exception is thrown.  
ICryptoTransform mytransform=myserviceprovider.createdecryptor (New byte[]{ 100,110,120,130,100,110,120,130},new byte[]{100,110,120,130,100,110,120,130});  
}

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.