/////////////////////////////////////////////////////////////
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});
}