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 #