Exemplo n.º 1
0
    public static byte[] EncryptRSA(string publicKeyAsPem, string payload, string passphrase = null)
    {
        var encoder = new UTF8Encoding();

        byte[]    byte_payload = encoder.GetBytes(payload);
        CryptoKey d            = CryptoKey.FromPublicKey(publicKeyAsPem, passphrase);

        OpenSSL.Crypto.RSA rsa    = d.GetRSA();
        byte[]             result = rsa.PublicEncrypt(byte_payload, OpenSSL.Crypto.RSA.Padding.PKCS1);
        rsa.Dispose();
        return(result);
    }
Exemplo n.º 2
0
    public static string DecryptRSA(string privateKeyAsPem, byte[] payload, string passphrase = null)
    {
        var encoder = new UTF8Encoding();

        byte[]    byte_payload = payload;
        CryptoKey d            = CryptoKey.FromPrivateKey(privateKeyAsPem, passphrase);

        OpenSSL.Crypto.RSA rsa    = d.GetRSA();
        byte[]             result = rsa.PrivateDecrypt(byte_payload, OpenSSL.Crypto.RSA.Padding.PKCS1);
        rsa.Dispose();
        return(encoder.GetString(result));
    }
Exemplo n.º 3
0
 /// <summary>
 /// 公钥加密
 /// </summary>
 public static string PublicEncrypt(string publicKey, string text, Encoding encoding, int padding)
 {
     byte[] textBytes = encoding.GetBytes(text);
     using (BIO bio = new BIO(publicKey))
     {
         using (OpenSSL.Crypto.RSA rsa = OpenSSL.Crypto.RSA.FromPublicKey(bio))
         {
             textBytes = rsa.PublicEncrypt(textBytes, (OpenSSL.Crypto.RSA.Padding)padding);
             rsa.Dispose();
         }
         bio.Dispose();
     }
     return(Convert.ToBase64String(textBytes));
 }