<summary>////Use private key to encrypt strings///</summary>//<param name= "key" > Characters to be encrypted </param>//<param name= "KeyPath" > Private key Certificate file address </param> public string Encryptkey (string key, String keypath) {X509Certificate2 C2 = new X509Certificate2 (AppDomain.CurrentDomain.BaseDirectory + "bi N\\client.cer "); String keyPublic2 = c2. PublicKey.Key.ToXmlString (FALSE); String cypher2 = Rsaencrypt (KeyPublic2, key); Encrypt return cypher2; }//<summary>//RSA decryption//</summary>/<param name= "Xmlprivateke Y "></param>//<param name=" m_strdecryptstring "></param>//<returns></retur ns> static string Rsadecrypt (String Xmlprivatekey, String m_strdecryptstring) {Rsacryptoservi Ceprovider Provider = new RSACryptoServiceProvider (); Provider. FromxmLstring (Xmlprivatekey); byte[] RGB = convert.frombase64string (m_strdecryptstring); byte[] bytes = provider. Decrypt (RGB, false); return new UnicodeEncoding (). GetString (bytes); }//<summary>//RSA encryption//</summary>//<param name= "Xmlpublickey "></param>//<param name=" m_strencryptstring "></param>//<returns></ returns> static string Rsaencrypt (String Xmlpublickey, String m_strencryptstring) {Rsacryp Toserviceprovider Provider = new RSACryptoServiceProvider (); Provider. Fromxmlstring (Xmlpublickey); byte[] bytes = new UnicodeEncoding (). GetBytes (m_strencryptstring); Return convert.tobase64string (provider. Encrypt (bytes, false)); }
C # read CER certificate get secret key to String encryption (RSA)