C # RAS Asymmetric Encryption class supports long strings

Source: Internet
Author: User
Tags asymmetric encryption

/// <summary>///[email protected]/// </summary>Public classmyRAs {/// <summary>        ///RAS Encryption/// </summary>        /// <param name= "Xmlpublickey" >Public Key</param>        /// <param name= "encryptstring" >plaintext</param>        /// <returns>Ciphertext</returns>          Public Static stringRsaencrypt (stringXmlpublickey,stringencryptstring) {            byte[] plaintextbarray; byte[] cyphertextbarray; stringresult=String.Empty; System.Security.Cryptography.RSACryptoServiceProvider RSA=NewRSACryptoServiceProvider (); Rsa.            Fromxmlstring (Xmlpublickey); intT = (int) (Math.ceiling (Double) Encryptstring.length/(Double) -)); //Split PlainText             for(inti =0; I <= t1; i++) {Plaintextbarray= (NewUnicodeEncoding ()). GetBytes (encryptstring.substring (i * -, Encryptstring.length-(I * -) > -? -: Encryptstring.length-(I * -))); Cyphertextbarray= RSA. Encrypt (Plaintextbarray,false); Result+ = Convert.tobase64string (Cyphertextbarray) +"Thisissplit"; }            returnResult; }        /// <summary>        ///RAS Decryption/// </summary>        /// <param name= "Xmlprivatekey" >private Key</param>        /// <param name= "decryptstring" >Ciphertext</param>        /// <returns>plaintext</returns>         Public Static stringRsadecrypt (stringXmlprivatekey,stringdecryptstring) {            byte[] plaintextbarray; byte[] dyphertextbarray; stringresult=String.Empty; System.Security.Cryptography.RSACryptoServiceProvider RSA=NewRSACryptoServiceProvider (); Rsa.            Fromxmlstring (Xmlprivatekey); string[] Split =New string[1]; split[0] ="Thisissplit"; //Split cipher            string[] mis =decryptstring.split (Split, stringsplitoptions.removeemptyentries);  for(inti =0; I < MIS. Length; i++) {Plaintextbarray=convert.frombase64string (Mis[i]); Dyphertextbarray= RSA. Decrypt (Plaintextbarray,false); Result+= (Newunicodeencoding ()).            GetString (Dyphertextbarray); }            returnResult; }         /// <summary>        ///generate public and private key pairs/// </summary>        /// <returns>string[] 0: Private key; 1: Public Key</returns>         Public Static string[] Rsakey () {string[] keys =New string[2]; System.Security.Cryptography.RSACryptoServiceProvider RSA=NewRSACryptoServiceProvider (); keys[0] = RSA. Toxmlstring (true); keys[1] = RSA. Toxmlstring (false); returnkeys; }    }

C # RAS Asymmetric Encryption class supports long strings

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.