SOURCE Recommendation: Use C # to write DES encryption program framework

Source: Internet
Author: User
Program | Encryption source recommendation: Use C # to write DES encryption program framework
Batman
Or my old point of view, we have to keep practicing to learn new things. So
I will not write some abstract concept, I intend to give some actual code later, hehe
In Microsoft's help, like to take it with examples called its new technology framework, I
It really does feel in real programming, and we often write our own based on these instance codes.
Practical application, so I also used the term framework. :)
This example demonstrates how to use the encryption package in C # to encrypt the DES algorithm because
Ngwnet help is really simple, and not the same, hehe, but we can use
This example is a glimpse of the use of Des cryptography.
The Des_demo.cs code is as follows:

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

public class Encryptstringdes {

public static void Main (string[] args) {
if (args. Length < 1) {
Console.WriteLine ("Usage:des_demo <string-to-encrypt>", args[0]);
Return
}

Using the UTF8 function to encrypt input parameters
UTF8Encoding utf8encoding = new UTF8Encoding ();
byte[] Inputbytearray = Utf8encoding.getbytes (args[0). ToCharArray ());

Mode one: invokes the default Des implementation method DES_CSP.
Des des = des. Create ();
Mode two: Entities directly using DES_CSP () to implement DES
DES_CSP DES = new DES_CSP ();

Initializes the DES-encrypted key and a random, 8-bit initialization vector (IV)
Byte[] key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xef};
Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xef};
Des. key = key;
DES.IV = IV;

Create an encrypted stream
Symmetricstreamencryptor SSE = des. CreateEncryptor ();

To obtain the output of the encryption process using the Cryptomemorystream method
Cryptomemorystream cms = new Cryptomemorystream ();

Output encrypted data from the Symmetricstreamencryptor stream to the Cryptomemorystream
Sse. Setsink (CMS);

Complete encryption, output the results to the console
Sse. Write (Inputbytearray);
Sse. Closestream ();

Get Encrypted data
byte[] EncryptedData = cms. Data;

Output after encryption results
Console.WriteLine ("Encrypted Result:");
for (int i = 0; i < encrypteddata.length; i++) {
Console.Write ("{0:x2}", Encrypteddata[i]);
}
Console.WriteLine ();

This shows how to encrypt, and the following shows how to decrypt it
Symmetricstreamdecryptor SSD = des. CreateDecryptor ();
CMS = new Cryptomemorystream ();
Ssds. Setsink (CMS);
Ssds. Write (EncryptedData);
Ssds. Closestream ();

byte[] Decrypteddata = cms. Data;
char[] Decryptedchararray = Utf8encoding.getchars (Decrypteddata);
Console.WriteLine ("Decrypted Data:");
Console.Write (Decryptedchararray);
Console.WriteLine ();
}
}

Compile:
D:\CSHARP&GT;CSC Des_demo.cs
Microsoft (R) C # Compiler Version 7.00.8905 [NGWS runtime 2000.14.1812.10]
Copyright (C) Microsoft Corp. 2000. All rights reserved.

Run instance:
D:\csharp>des_demo.exe the framework for using C # to write DES encryption programs
Encryption results:
3D C6 D1 C4 C3 CF + CE 2F D0 E1 2A 4D ED 7A A8 0E F9 BA 38
7B 8D B5 E9 3F 0D C3 D1 F9 6D 4 b 6E A7 41 68 40
Post-decryption data:
Using C # to write the framework of DES encryption programs

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.