Simple use of. Net Encryption

Source: Internet
Author: User
Symmetric encryption algorithm:

Using system;
Using system. Security. cryptography;
Using system. text;
Using system. IO;

/// <Summary>
/// Example of using symmetric encryption
/// </Summary>
Class class1
{

Static void main (string [] ARGs)
{
Class1 c = new class1 ();
C. startdemo ();
}

Public void startdemo ()
{
// Establish datagric Algorithm
Repeated ricalgorithm SA = Rijndael. Create ();

// Key and IV
SA. generatekey (); // generate a random (32*8) Key.
// SA. generateiv (); // initial vector. You do not need to use IV in ECB mode.
SA. mode = ciphermode. ECB; // block processing mode
SA. Padding = paddingmode. Zeros; // fill mode of the end data block


Console. writeline ("Key is :");
For (INT I = 0; I <SA. Key. length; I ++)
{
Console. Write ("{0: X2}", SA. Key [I]);
}
Console. writeline ("\ n ");


// Establish crypto stream
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, SA. createencryptor (), cryptostreammode. Write );

String plaintext; // the original text.
Byte [] cipherbytes; // encrypted data
Byte [] finalbytes; // decrypted data

Plaintext = "how are you? This is a line of text. ";
Byte [] plainbytes = encoding. utf8.getbytes (plaintext );


Console. writeline ("raw text: \ n {0} \ n", plaintext );
// Display plaint text byte array in hex format
Console. writeline ("raw data is :");
For (INT I = 0; I <plainbytes. length; I ++)
{
Console. Write ("{0: X2}", plainbytes [I]);
}
Console. writeline ("\ n ");

// Encryption process
CS. Write (plainbytes, 0, plainbytes. Length );
CS. Close ();
Cipherbytes = Ms. toarray ();
Ms. Close ();

// Display ciphertext byte array in hex format
Console. writeline ("the encrypted data is :");
For (INT I = 0; I <cipherbytes. length; I ++)
{
Console. Write ("{0: X2}", cipherbytes [I]);
}
Console. writeline ("\ n ");


// The following is the encryption process
MS = new memorystream (cipherbytes );
Cs = new cryptostream (MS, SA. createdecryptor (), cryptostreammode. Read );
Finalbytes = new byte [plainbytes. Length];
CS. Read (finalbytes, 0, plainbytes. Length );

Console. writeline ("the decrypted data is :");
For (INT I = 0; I <finalbytes. length; I ++)
{
Console. Write ("{0: X2}", finalbytes [I]);
}
Console. writeline ("\ n ");

String finaltext = encoding. utf8.getstring (finalbytes );

Console. writeline ("decrypted text: \ n {0} \ n", finaltext );
Console. writeline ("press any key to continue ");
Console. Readline ();


}
}

Asymmetric encryption algorithm:

Using system;
Using system. IO;
Using system. text;
Using system. Security. cryptography;

/** // <Summary>
/// A simple example of using the. NET asymmetric encryption algorithm
/// The program in this example is very simple. It is only used to explain how to use the asymmetric (RSA) algorithm in. net.
/// Kwanhong 2005.9
/// </Summary>
Class class1
{
Public static void main (string [] ARGs)
{
Class1 c = new class1 ();
C. startdemo ();
}

Public void startdemo ()
{
// RSA encryption and decryption process:
// There are two RSA objects: rsa1 and rsa2.
// If you want rsa2 to send a piece of information to rsa1, rsa1 first sends the "Public Key" to rsa2
// The Public Key obtained by rsa2 is used to encrypt the data to be sent.
// After obtaining the encrypted content, rsa1 decrypts it with its own private key to obtain the original data content.

Rsacryptoserviceprovider rsa1 = new rsacryptoserviceprovider ();
Rsacryptoserviceprovider rsa2 = new rsacryptoserviceprovider ();

String publickey;
Publickey = rsa1.toxmlstring (false); // export the public key of rsa1

String plaintext;
Plaintext = "how are you? This is the string used for testing. "; // Raw data
Console. writeline ("raw data is \ n {0} \ n", plaintext );

Rsa2.fromxmlstring (publickey); // rsa2 imports the public key of rsa1 for information encryption.

// Start rsa2 Encryption
Byte [] cipherbytes;
Cipherbytes = rsa2.encrypt (
Encoding. utf8.getbytes (plaintext ),
False );

/**//*////////////////////////////////// ////////////*/
Console. writeline ("the encrypted data is :");
For (INT I = 0; I <cipherbytes. length; I ++)
{
Console. Write ("{0: X2}", cipherbytes [I]);
}
Console. writeline ("\ n ");
/**//*////////////////////////////////// ////////////*/

// Start decryption with rsa1
Byte [] plaintbytes;
Plaintbytes = rsa1.decrypt (cipherbytes, false );

Console. writeline ("the decrypted data is :");
Console. writeline (encoding. utf8.getstring (plaintbytes ));

Console. Readline ();
}

}

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.