A des symmetric encryption algorithm using one of the dotnet cryptography systems to secure data

Source: Internet
Author: User
Tags dotnet interface
Security | encryption | data | Algorithm (Billion International-008) [Original] Use dotnet cryptography system to ensure data security

/////////////////////////////////////////////////////////////
Author:stardicky//
E-mail:stardicky@hotmail.com//
qqnumber:9531511//
Companyname:ezone International//
class:hbs-0308//
Title: Using dotnet cipher system to ensure data security//
/////////////////////////////////////////////////////////////
Note: The DES symmetric encryption algorithm using one of the dotnet cipher Systems guarantees 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 ();
Decrypting data (from file to memory)
Ezonedecryptordemo ();
}

<summary>
Encryption
</summary>
public static void Ezoneencryptordemo ()
{
Creates a file object in which the file's schema is to create a new file, and the file's access rights are 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 ();
Converts a string to bytes
Byte[] Yourinputstorage=system.text.encoding.utf8.getbytes (yourinput);
Create an encryption class for the DES algorithm
DESCryptoServiceProvider myserviceprovider=new DESCryptoServiceProvider ();
Create a Cryptographic transformation interface object from the CreateEncryptor method of the encryption class object of the DES algorithm
The first parameter means: The secret key of the symmetric algorithm (64 bits in length, 8 bytes)
Can be manually input, but also can be randomly generated method is: Myserviceprovider.generatekey ();
The second parameter means the initialization vector of the symmetric algorithm (64 bits in length, that is, 8 bytes)
Can be manually input, but also can be 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 linger the data into the stream of the encrypted transformation
CryptoStream mycryptostream=new CryptoStream (fs,mytransform,cryptostreammode.write);
Writes data from a byte array to the encrypted stream
Mycryptostream.write (yourinputstorage,0,yourinputstorage.length);
Turn off the 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 ();
From the CreateEncryptor method of the encryption class object of DES algorithm, create a decryption Transformation interface object
[Secret key of symmetric algorithm] must be [secret key of Symmetric algorithm] at time of encryption
[Initialization vectors for symmetric algorithms] must be [initialization vectors of symmetric algorithms] at the time of encryption
If it is not the same, 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});
CryptoStream mycryptostream=new CryptoStream (fs,mytransform,cryptostreammode.read);
Byte[] Yourinputstorage=new byte[1000];
int Len=mycryptostream.read (yourinputstorage,0,yourinputstorage.length);
Console.WriteLine ("The string you have just entered is:");
Console.WriteLine (System.Text.Encoding.UTF8.GetString (Yourinputstorage,0,len));
}

}
}


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.