A simple example of the use of 3DES encryption

Source: Internet
Author: User
Tags pkcs7
Encryption | Sample Feed namespaces: System.Security.Cryptography.TripleDES class Simple Description: Represents the base class for triple data Encryption standard algorithms, all implementations of TripleDES must derive from this base class, but TripleDES is derived from the SymmetricAlgorithm in the class. TripleDES uses the DES algorithm for three consecutive iterations.      It can use two or three 56-bit keys. Purpose of Use: Secure encryption a way, the difference between the key and the vector, will produce different cryptographic strings.    Because it is the three consecutive iterations of the DES algorithm, and the algorithm is reversible, it is good for data confidentiality and recoverability. How to: direct string input and output. Code example This code refers to some of the code examples on MSDN and, based on its own circumstances, supplements a subset of the content not mentioned on MSDN using System;using system.security;using System.security.cryptography;using system.io;using system.text;using system.threading; Namespace trip3des{/// Summary description of the///Class1. /// public class Dllencrypt {//Key Private Const string SKey = "QJZGEH6HESZDVJECNFPGUXZAIB7NLQM3";      Vector, vector can be empty private Const string SIV = "qcdy6x+aplw=";         Constructs a symmetric algorithm private symmetricalgorithm MCSP = new TripleDESCryptoServiceProvider (); Public Dllencrypt () {} #region public string encryptstring (string Value)/// ///Encrypted string////// The string entered/// The encrypted string      public string encryptstring (string Value)       {          ICryptoTransform ct;         MemoryStream ms;         CryptoStream cs;          byte[] byt;         Mcsp.key = convert.frombase64string ( SKey);         Mcsp.iv = convert.frombase64string (SIV);          //Specifies the cryptographic operation mode          Mcsp.mode = system.security.cryptography.ciphermode.ecb;       //Get or set the fill mode of the encryption algorithm           mcsp.padding = system.security.cryptography.paddingmode.pkcs7;          ct = mcsp.createencryptor (Mcsp.key, MCSP.IV);     & NBsp;    byt = Encoding.UTF8.GetBytes (Value);         ms = new MemoryStream ();         cs = new CryptoStream (MS, CT, cryptostreammode.write );         cs. Write (byt, 0, Byt. LENGTH);         cs. FlushFinalBlock ();         cs. Close ();         return convert.tobase64string (Ms. ToArray ());     }      #endregion         #region public string decryptstring (string Value)      /// ///decryption String////// A string that's been added too tight/// The decrypted string      public string decryptstring (string Value)       {          ICryptoTransform ct;         MemoryStream ms;         CryptoStream cs;          byte[] byt;         Mcsp.key = convert.frombase64string ( SKey);         Mcsp.iv = convert.frombase64string (SIV);          Mcsp.mode = system.security.cryptography.ciphermode.ecb;          mcsp.padding = system.security.cryptography.paddingmode.pkcs7;          ct = mcsp.createdecryptor (Mcsp.key, MCSP.IV);          BYT = convert.frombase64string (Value);         ms = new MeMorystream ();         cs = new CryptoStream (MS, CT, cryptostreammode.write);          cs. Write (byt, 0, Byt. LENGTH);         cs. FlushFinalBlock ();         cs. Close ();          return Encoding.UTF8.GetString (Ms. ToArray ());     }      #endregion    }}  III, Summary of the class library for key and vector storage more convenient, input and output are all string variables, so it is also more convenient, key generation can be used MSCP. GenerateKey () to generate, the generation of vectors can also be generated using MCSP.GENERATEIV (). You can also be flexible to write their own 3DES algorithm.

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.