Brief introduction. All kinds of encryption and decryption algorithms under NET Framework

Source: Internet
Author: User
Tags decrypt readline

The article will continue to be revenue. NET encryption algorithms, these algorithms will adopt a policy model for unified management.

Using System;  
Using System.Collections.Generic;  
Using System.Text;  
      
Namespace Encryptanddecrypt  
{public  
   class Encryptanddecrythandler  
    {  
        Iencrytanddecrypt _ Encryptanddecrypt;  
        Public Encryptanddecrythandler (Iencrytanddecrypt iencrytanddecrypt)  
        {  
            _encryptanddecrypt = Iencrytanddecrypt;  
        }  
        public string Encrypt (string data)  
        {return  
            _encryptanddecrypt.encrypt (data);  
        }  
        public string Decrypt (string data)  
        {return  
            _encryptanddecrypt.decrypt (data);  
        }  
    }  
}

Cryptographic decryption of the caller

Using System;  
Using System.Collections.Generic;  
Using System.Text;  
      
Namespace Encryptanddecrypt  
{public  
    interface Iencrytanddecrypt  
    {  
       string Encrypt (string data);  
       String Decrypt (string data);  
    }  
}

Policy interface

 using System;  
Using System.Collections.Generic;  
Using System.Text;  
Using System.Security.Cryptography;  
      
Using System.IO; Namespace Encryptanddecrypt {public class Descrypt:iencrytanddecrypt {private byte[] Rgbkey = NE  
        W byte[] {1, 9, 8, 7, 0, 1, 0, 3};  
        Private byte[] Rgbiv = new byte[] {1, 9, 8, 7, 0, 1, 0, 3}; #region Iencrytanddecrypt member public string Encrypt (string data) {MemoryStream ms  
            Tream = new MemoryStream ();  
            DESCryptoServiceProvider Deskey = new DESCryptoServiceProvider (); CryptoStream cstream = new CryptoStream (Mstream, Deskey.  
            CreateEncryptor (RGBKEY,RGBIV), cryptostreammode.write);  
            StreamWriter sw = new StreamWriter (Cstream); Sw.  
            WriteLine (data); Sw.  
            Close (); Cstream.  
            Close (); byte[] bytes = Mstream.  
            ToArray (); Mstream.  
            Close (); Return convert.toBase64string (bytes);  
            public string Decrypt (string data) {string tmp;  
            MemoryStream mstream = new MemoryStream (convert.frombase64string (data));  
            DESCryptoServiceProvider Deskey = new DESCryptoServiceProvider (); CryptoStream cstream = new CryptoStream (Mstream, Deskey.  
            CreateDecryptor (Rgbkey, Rgbiv), cryptostreammode.read);  
            StreamReader sr = new StreamReader (cstream); TMP = Sr.  
            ReadLine (); Sr.  
            Close (); Cstream.  
            Close (); Mstream.  
            Close ();  
        return TMP; } #endregion}} 

DES encryption algorithm

 using System;  
Using System.Collections.Generic;  
Using System.Text;  
Using System.Security.Cryptography;  
      
Using System.IO;   
       Namespace Encryptanddecrypt {public class Tripledescrypt:iencrytanddecrypt {private byte[] rgbkey;  
       Private byte[] RGBIV;  
         Public Tripledescrypt () {Rgbkey = new byte[130];  
         RGBIV = new byte[130]; for (int i = 0; i < 130 i++) {Rgbkey[i] = rgbiv[i] = (byte) (New Random ().  
         Next (1, 255));  
        #region Iencrytanddecrypt member public string Encrypt (string data)  
                  
            {TripleDESCryptoServiceProvider tripledes=new tripledescryptoserviceprovider ();  
            MemoryStream mstream = new MemoryStream ();  
            CryptoStream cstream = new CryptoStream (Mstream, Tripledes.createencryptor (Rgbkey, Rgbiv), cryptostreammode.write); Streamwriter SW = new StreamWriter (Cstream); Sw.  
            WriteLine (data); Cstream.  
            Close (); Byte[] result = Mstream.  
            ToArray (); Mstream.  
            Close ();  
      
        return convert.tobase64string (Result);  public string Decrypt (string data) {TripleDESCryptoServiceProvider TripleDes =  
            New TripleDESCryptoServiceProvider ();  
            MemoryStream mstream = new MemoryStream (convert.frombase64string (data));  
            CryptoStream cstream = new CryptoStream (Mstream, Tripledes.createdecryptor (Rgbkey, Rgbiv), cryptostreammode.read);  
            StreamReader sr = new StreamReader (cstream); string result = Sr.  
            ReadLine (); Sr.  
            Close (); Cstream.  
            Close (); Mstream.  
            Close ();  
        return result; } #endregion}} 

TripleDES encryption 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.