In C #, use the OpenSSL Public/Private Key for encryption and decryption,
RSACryptoServiceProvider is required for RSA decryption in C #, but public or private key in OpenSSL format is not supported.
X509 Public Key
-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCRh7vKq7r6EhSg9y6iD38Vy08zumEJXhNTIGe8vRjdpE118Nu1cPRZ0AU9FbMIx8cnQtgCqGgW3DPDLj4VmlmdnJ8PcFHBBWgh85P1hicv8BQ0nLLjqezaBcS9EmO1C7zhdONVrvMFyHc1j76e+Fv+XfC6CRvwKfqX+j5OMmWtowIDAQAB-----END PUBLIC KEY-----
Unencrypted PKCS #8 Private Key
-----BEGIN PRIVATE KEY-----MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJGHu8qruvoSFKD3LqIPfxXLTzO6YQleE1MgZ7y9GN2kTXXw27Vw9FnQBT0VswjHxydC2AKoaBbcM8MuPhWaWZ2cnw9wUcEFaCHzk/WGJy/wFDScsuOp7NoFxL0SY7ULvOF041Wu8wXIdzWPvp74W/5d8LoJG/Ap+pf6Pk4yZa2jAgMBAAECgYBxgvu5M4Xd9Che/S5Efe3UZAZdBK0Xj2dnWUlQ7/XrO3Kn4bydo4MUYqsTYE+LM7hlKUAvooe9/Pfq77DBTOlETcSJfvZdL+mX5b3v8hnknWdpiyhfB2yaYbNTOUWMHboekwtqjP5C5yA5IObGcDFFPVZA/tRa50XNq3PATS1jIQJBAPi7U+zhFG/QKI4IlUYymwKlsa7ZMHJjXX8508ZxD0azYDfFsZEWquAdPPXj9lVlXD7uMBzewAx/gn2wTBLtr1sCQQCVyGRyTarXFaqdImEe+7/o6hCIdVvCBUuyIkd0WUPTkbizpUpUrzu9oy1UbvWlI2r4QqjoMh8eq05zlU+sJ9VZAkEA6mDCzZSuiEoy3NvzpYksDguKagNYpoFBahBYoUaKKYn/Ya6VCu28KKEEf4PG7GMt0FLr3Vh8yYohsUQ9+xwjvwJAGDB8yFjytjmxjB75QF+35o2mjeMmJndkeig/EfM4mPp3scuH7ZU/OKkZsQEaesj1hZJ+ZMFzdSiFSvVaIJtuQQJAH3sXQ3DVEoeSW40TyYviq2K1pEpz9fGcS1TrjI+Yt1Mf0mvO6eupNZCfuhiAqJ5ZGRzTv1PuG7pgz+ey5sWBcA==-----END PRIVATE KEY-----
PKCS #1 Private Key
-----BEGIN RSA PRIVATE KEY-----MIICXQIBAAKBgQC7PyjMEuniN6BPn8oqzIZ6AO1NjSTO9R3adCCIwKfKIEoWXXM+tHDpktdPKSaAsWJPTNAGvEvtxOfzXib/EMXKqD0eUy5MatfpRjRdf1hJVimmfrb09Qx2j7CsKLy7nD23m4xubdYBwvkjMwt/L3JxB5D6qryW1wei/j1c+/OCxQIDAQABAoGAT7vGYJgRNf4f6qgNS4pKHTu10RcwPFyOOM7IZ9M5380+HyXuBB6MEjowKwpH1fcy+LepwaR+5KG7b5uBGY4H2ticMtdysBd9gLwnY4Eh4j7LCWE54HvELpeWXkWpFQdb/NQhcqMAGwYsTnRPdBqkrUmJBTYqEGkIlqCQ5vUJOCECQQDhe0KGmbq1RWp6TDvgpA2dUmlt2fdP8oNW8O7MvbDaQRduoZnVRTPYCDKfzFqpNXL1hAYgth1N0vzDnv3VoLcpAkEA1JcY+rLv5js1g5Luv8LaI5/3uOg0CW7fmh/LfGuz8k/OxASN+cAOUjPHrxtc5xn1zat4/bnV5GEdlOp/DhquPQJBAIV2Fsdi4M+AueiPjPWHRQO0jvDVjfwFOFZSn5YSRUa6NmtmPY6tumUJXSWWqKb1GwlVTuc3xBqXYsNLLUWwLhkCQQDJUJCiD0LohhdGEqUuSKnj5H9kxddJO4pZXFSI7UEJbJQDwcBkyn+FTm2BH+tZGZdQfVnlA89OJr0poOpSg+eNAkAKY85SR9KASaTiDBoPpJ8N805XEhd0Kq+ghzSThxL3fVtKUQLiCh7Yd8oMd/G5S3xWJHUXSioATT8uPRH2bOb/-----END RSA PRIVATE KEY-----
REFERENCE The following Nuget packages
Https://www.nuget.org/packages/System.Extended/
You can directly use RSACryptoServiceProvider to find several more extension methods.
The parameter is a Base64 encoded byte array.
var private_key = @"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJGHu8qruvoSFKD3 LqIPfxXLTzO6YQleE1MgZ7y9GN2kTXXw27Vw9FnQBT0VswjHxydC2AKoaBbcM8Mu PhWaWZ2cnw9wUcEFaCHzk/WGJy/wFDScsuOp7NoFxL0SY7ULvOF041Wu8wXIdzWP vp74W/5d8LoJG/Ap+pf6Pk4yZa2jAgMBAAECgYBxgvu5M4Xd9Che/S5Efe3UZAZd BK0Xj2dnWUlQ7/XrO3Kn4bydo4MUYqsTYE+LM7hlKUAvooe9/Pfq77DBTOlETcSJ fvZdL+mX5b3v8hnknWdpiyhfB2yaYbNTOUWMHboekwtqjP5C5yA5IObGcDFFPVZA /tRa50XNq3PATS1jIQJBAPi7U+zhFG/QKI4IlUYymwKlsa7ZMHJjXX8508ZxD0az YDfFsZEWquAdPPXj9lVlXD7uMBzewAx/gn2wTBLtr1sCQQCVyGRyTarXFaqdImEe +7/o6hCIdVvCBUuyIkd0WUPTkbizpUpUrzu9oy1UbvWlI2r4QqjoMh8eq05zlU+s J9VZAkEA6mDCzZSuiEoy3NvzpYksDguKagNYpoFBahBYoUaKKYn/Ya6VCu28KKEE f4PG7GMt0FLr3Vh8yYohsUQ9+xwjvwJAGDB8yFjytjmxjB75QF+35o2mjeMmJndk eig/EfM4mPp3scuH7ZU/OKkZsQEaesj1hZJ+ZMFzdSiFSvVaIJtuQQJAH3sXQ3DV EoeSW40TyYviq2K1pEpz9fGcS1TrjI+Yt1Mf0mvO6eupNZCfuhiAqJ5ZGRzTv1Pu G7pgz+ey5sWBcA==";var public_key = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCRh7vKq7r6EhSg9y6iD38Vy08z umEJXhNTIGe8vRjdpE118Nu1cPRZ0AU9FbMIx8cnQtgCqGgW3DPDLj4VmlmdnJ8P cFHBBWgh85P1hicv8BQ0nLLjqezaBcS9EmO1C7zhdONVrvMFyHc1j76e+Fv+XfC6 CRvwKfqX+j5OMmWtowIDAQAB";var rsa = new RSACryptoServiceProvider();rsa.FromX509PublicKey(Convert.FromBase64String(public_key));var bytes = rsa.Encrypt(Encoding.UTF8.GetBytes("mangohappy"), false);rsa.FromPKCS8PrivateKey(Convert.FromBase64String(private_key));Console.WriteLine(Encoding.UTF8.GetString(rsa.Decrypt(bytes, false)));
Note :. NET RSACryptoServiceProvider only supports public key encryption, Private Key decryption, and the above are also encrypted PKCS #8 format private key, I will not deal with it, the public keys of PKCS #1 and PKCS #8 are in X509 format.