RSA asymmetric encryption algorithm based on C #

Source: Internet
Author: User
Tags array length base64 asymmetric encryption

Recently in the design of single sign-on, in the design of the need for a token token of the encrypted transmission, the token in the entire connection point of the various stations play a connection authentication function, if the imitation will have unpredictable losses, but this token is reversible. So like that kind of md5,sha irreversible encryption can not be used, and then reversible encryption is mainly divided into symmetric encryption and asymmetric encryption.

    • Symmetric encryption: Decryption with an encrypted key, such as Des,aes encryption and decryption.
    • Asymmetric encryption: A key is encrypted and decrypted with another key.

Look directly at the following method:

1. First generate key pair

<summary>///RSA cryptographic key structure public key and private key///</summary>public struct rsakey{publicly string PublicKey {get; set;} public string Privatekey {get; set;}} #region get RSA key pair//<summary>///get RSA key pair//</summary>///<returns></returns>public static    Rsakey Getraskey () {Rsacryptoserviceprovider.usemachinekeystore = true;    RSACryptoServiceProvider Rsaprovider = new RSACryptoServiceProvider (dwkeysize);    RSAParameters p = rsaprovider.exportparameters (true); return new Rsakey () {PublicKey = Componentkey (p.exponent, p.modulus), Privatekey = Componentkey (P.D, p.m. Odulus)};} #endregion #region combine keys into Base64 strings///<summary>///combine keys into Base64 encoded strings///</summary>private static string    Componentkey (byte[] B1, byte[] b2) {list<byte> List = new list<byte> (); List. Add ((Byte) B1.    Length); List.    AddRange (B1); List.    AddRange (B2); Byte[] B = list.    Toarray<byte> (); return convert.tobase64string (b);} <summary>///from Base64 string, parse original key///</summary>private static void Resolvekey (String key, out byte[] B1, out byte[] B2    {//Parse from Base64 string to original byte array byte[] b = convert.frombase64string (key);    Initialize parameter's array length B1 = new Byte[b[0]];    b2 = new Byte[b.length-b[0]-1];            Place the corresponding value in the corresponding array for (int n = 1, i = 0, j = 0; n < b.length; n++) {if (n <= b[0]) {        b1[i++] = B[n];        } else {b2[j++] = B[n]; }}} #endregion

RSA asymmetric encryption algorithm based on C #

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.