C # DES

Source: Internet
Author: User

DES encryption

Using System.Security.Cryptography;

As with AES, we only know the usage of the ECB CBC CFB Model

 Static classMydes {Staticpaddingmode[] padding ={PADDINGMODE.PKCS7, paddingmode.ansix923, paddingmode.iso10126, PADDINGM Ode.        None, Paddingmode.zeros}; Static  Public stringEncrypt (stringMessage,stringKeystringIV, Ciphermode Mode,intpad) {            Try            {                byte[] Keybytes =Encoding.UTF8.GetBytes (key); byte[] Keyiv =Encoding.UTF8.GetBytes (IV); byte[] Inputbytearray =Encoding.UTF8.GetBytes (Message); DESCryptoServiceProvider Desprovider=NewDESCryptoServiceProvider (); //Java default is the ECB mode, pkcs5padding;c# the default CBC mode, pkcs7padding so here we use the ECB way by defaultDesprovider.mode =Mode; Desprovider.padding=Padding[pad]; MemoryStream Memstream=NewMemoryStream (); CryptoStream Crypstream=NewCryptoStream (Memstream, Desprovider.createencryptor (Keybytes, Keyiv), cryptostreammode.write); Crypstream.write (Inputbytearray,0, inputbytearray.length);                Crypstream.flushfinalblock (); returnconvert.tobase64string (Memstream.toarray ()); }            Catch{MessageBox.Show ("Encryption failed"); return ""; }        }               Static  Public stringDecrypt (stringMessage,stringKeystringIV, Ciphermode Mode,intpad) {            Try            {                byte[] Keybytes =Encoding.UTF8.GetBytes (key); byte[] Keyiv =Encoding.UTF8.GetBytes (IV); byte[] Inputbytearray =convert.frombase64string (Message); DESCryptoServiceProvider Desprovider=NewDESCryptoServiceProvider (); //Java default is the ECB mode, pkcs5padding;c# the default CBC mode, pkcs7padding so here we use the ECB way by defaultDesprovider.mode =Mode; Desprovider.padding=Padding[pad]; MemoryStream Memstream=NewMemoryStream (); CryptoStream Crypstream=NewCryptoStream (Memstream, Desprovider.createdecryptor (Keybytes, Keyiv), cryptostreammode.write); Crypstream.write (Inputbytearray,0, inputbytearray.length);                Crypstream.flushfinalblock (); returnEncoding.Default.GetString (Memstream.toarray ()); }            Catch{MessageBox.Show ("Decryption failed"); return ""; }        }           }
Mydes

C # DES

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.