使用 OpenSSL.NET 和现有密钥解密 RSA

发布于 2024-12-02 03:09:35 字数 821 浏览 2 评论 0原文

我有以下代码使用 OpenSSL.Net 生成 OpenSSL RSA 公钥和私钥。但是,我似乎找不到使用给定私钥解密数据的方法。我知道如果我调用生成密钥,然后调用相应的方法来加密和解密数据,它就可以正常工作。但是,如果我尝试从给定公钥的外部源解密某些内容,我该如何使用该密钥进行解密。

注意:请不要给出不使用 OpenSSL.NET 的示例。 Microsoft 加密提供程序比 OpenSSL 慢得多,无法满足我的速度要求。

谢谢!

public class AsymmetricKeyResult
{
    public string PublicKey { get; set; }
    public string PrivateKey { get; set; }

    public AsymmetricKeyResult(string publicKey, string privateKey)
    {
        this.PublicKey = publicKey;
        this.PrivateKey = privateKey;
    }
}

public static AsymmetricKeyResult GenerateAsymmetricKeys(int keyLength)
{
    RSA rsa = new RSA();
    rsa.GenerateKeys(keyLength, 0x10021, null, null);
    AsymmetricKeyResult kResult = new AsymmetricKeyResult(rsa.PublicKeyAsPEM, rsa.PrivateKeyAsPEM);

    return kResult;
}

I have the following code below to generate an OpenSSL RSA public and private key using OpenSSL.Net. However, I can't seem to find a way to decrypt data with a given private key. I know if I call generate keys and then the corresponding methods to encrypt and decrypt the data it works fine. However, if I am trying to decrypt something from an external source given a public key, how can I decrypt using that key.

Note: Please do not give examples that don't use OpenSSL.NET. The Microsoft Cryptographic providers are far slower than OpenSSL and do not meet my speed requirements.

Thanks!

public class AsymmetricKeyResult
{
    public string PublicKey { get; set; }
    public string PrivateKey { get; set; }

    public AsymmetricKeyResult(string publicKey, string privateKey)
    {
        this.PublicKey = publicKey;
        this.PrivateKey = privateKey;
    }
}

public static AsymmetricKeyResult GenerateAsymmetricKeys(int keyLength)
{
    RSA rsa = new RSA();
    rsa.GenerateKeys(keyLength, 0x10021, null, null);
    AsymmetricKeyResult kResult = new AsymmetricKeyResult(rsa.PublicKeyAsPEM, rsa.PrivateKeyAsPEM);

    return kResult;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔女人霸气范 2024-12-09 03:09:35

我最终通过 OpenSSL.NET 的托管包装器上的对象浏览器弄清楚了这一点。这有效:

    public static byte[] AsymmetricEncrypt(string publicKeyAsPem, byte[] payload)
    {
        CryptoKey d = CryptoKey.FromPublicKey(publicKeyAsPem, null);
        RSA rsa = d.GetRSA();
        byte[] result = rsa.PublicEncrypt(payload, RSA.Padding.PKCS1);
        rsa.Dispose();
        return result;
    }

    public static byte[] AsymmetricDecrypt(string privateKeyAsPem, byte[] payload)
    {
        CryptoKey d = CryptoKey.FromPrivateKey(privateKeyAsPem, null);
        RSA rsa = d.GetRSA();
        byte[] result = rsa.PrivateDecrypt(payload, RSA.Padding.PKCS1);
        rsa.Dispose();
        return result;
    }

I ended up figuring it out through the object browser on the Managed Wrapper for OpenSSL.NET. This works:

    public static byte[] AsymmetricEncrypt(string publicKeyAsPem, byte[] payload)
    {
        CryptoKey d = CryptoKey.FromPublicKey(publicKeyAsPem, null);
        RSA rsa = d.GetRSA();
        byte[] result = rsa.PublicEncrypt(payload, RSA.Padding.PKCS1);
        rsa.Dispose();
        return result;
    }

    public static byte[] AsymmetricDecrypt(string privateKeyAsPem, byte[] payload)
    {
        CryptoKey d = CryptoKey.FromPrivateKey(privateKeyAsPem, null);
        RSA rsa = d.GetRSA();
        byte[] result = rsa.PrivateDecrypt(payload, RSA.Padding.PKCS1);
        rsa.Dispose();
        return result;
    }
过去的过去 2024-12-09 03:09:35

我发现了这个,并认为这是我祈祷的答案。但是,使用 openssl (1.0.0e Mac os x) 生成 pub/priv 密钥,我无法从加密文本中再次获取明文 - 是否缺少 GetBytes/Baseencode 类型步骤?你遇到过这个吗?

编辑:我一发布,就遇到了一个使用 UTF8 编码而不是 ASCII 的不同示例,它有效!

Ben

namespace testopenssl2
{
class Program
{

    public static byte[] AsymmetricEncrypt(string publicKeyAsPem, byte[] payload)
    {
        CryptoKey d = CryptoKey.FromPublicKey(publicKeyAsPem, null);
        RSA rsa = d.GetRSA();
        byte[] result = rsa.PublicEncrypt(payload, RSA.Padding.PKCS1);
        rsa.Dispose();
        return result;
    }

    public static byte[] AsymmetricDecrypt(string privateKeyAsPem, byte[] payload)
    {
        //CryptoKey d = CryptoKey.FromPrivateKey(privateKeyAsPem, null);
        CryptoKey d = CryptoKey.FromPrivateKey(privateKeyAsPem, "pass");
        RSA rsa = d.GetRSA();
        byte[] result = rsa.PrivateDecrypt(payload, RSA.Padding.PKCS1);
        rsa.Dispose();
        return result;
    }

    static void Main(string[] args)
    {
        String t = @"-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbhcU+fhYmYLESZQAj1hKBXsNY
si0kYHNkxpP7ftxZiTFowWUVXHzQgkcYiCNnp3pt1eG6Vt0WDzyFYXqUUqugvX41
gkaIrKQw/sRiWEx49krcz7Vxr3dufL6Mg3eK7NyWDGsqwFrx/qVNqdhsHg12PGNx
IMY4UBtxin2A8pd4OwIDAQAB
-----END PUBLIC KEY-----";
        String p = @"-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,0A128C2617BD2EB1

pTDtXB+mockO7fvVqn4fwGnSb1Zv3HaMAALtpiB7Rn64eAHL7psKQIIM3qoshDWF
XgXDdTnMOGO7wtYkd9R7iJYxgt19EuEdtu2SLLXQuN4ll+JSR2R/34dF19iMXI30
d3pe7obTIwKdyRGuu8GgEm6bGai4pkqptP0HRA6qdMI2+Qfl9+VqUuvIm7tfpIRd
/ZLENe756IrGDvI7lGx39Md/H2sgAJsWkSYubhmtxVJ0IEvbPuKDC5V5oLyTOoy+
6sc6ly57C4XHaTLhAKnYEvZAddnXg/e/VtfmTpqKx3n7D6FAKo1RjAjeZqEvefZd
XAhh19YhZq3mdZNYUt7ojUarf/q3zrtTMLUxHdR0Be/VaQC5AE0d6quKyUQgxiti
XNRS8xk9IJJqJLFSHO3ET+oTfcs+kLPuUDHqq0hY/OgW/THcDgPY1cDwtOX9yuI3
YDoFTb3SXzRTmk2ui33f96wNPwzIAp9+TJzITxJYbF233Pz4YWuabrFuoNWZnwtT
E/o6wcGfvAXTQkAKzwfLbTDmg5SSiGokoEcgm7qpfmQxKdmV1LmbW88DuAgdWggm
Qf3ydZ2IrrtD1o+XP7JraeVOql0OK77pJh/bcr3bLiAT8YtsQUZLnOjkbDc3F1zW
BGr6eeqUHxY6cqKieokhl9cBBjWuxJQL2h997svBufWdNOjTA4+32lXzDzi7bUxC
xzIqZ7nm3YC2zUjla/l3Smz5KitqU5Y3Q9URpXOW+qMiPxmTHYOEcRDy9yh2U4iA
CoTD6q0ZNJLEo3EVcDB+26O663/mQLuR69xstUgqHpSzGvXbqrmezA==
-----END RSA PRIVATE KEY-----";

        System.Text.Encoding enc = System.Text.Encoding.ASCII;
        String s = "hello";
        byte[] payload = enc.GetBytes(s);
        Console.WriteLine("s: {0}", s);

        byte[] byte_encData = AsymmetricEncrypt(t,payload);

        String res;
        res = Convert.ToBase64String(byte_encData);
        Console.WriteLine("encypted: {0}", res);

        byte[] byte_decrypted = AsymmetricDecrypt(p, byte_encData);
        String res_unenc;
        res_unenc = Convert.ToBase64String(byte_decrypted);

        // works!
        Console.WriteLine("decrypted: {0}", res_unenc);

    }
    }
}

I found this and thought it was the answer to my prayers. However using openssl (1.0.0e Mac os x) to generate pub/priv keys, i cannot get the clear text back again from the encrypted text - is there a GetBytes/Baseencode type step I'm missing? did you come across this?

EDIT: no sooner had i posted i then came across a different example using UTF8 encoding not ASCII, which works!

Ben

namespace testopenssl2
{
class Program
{

    public static byte[] AsymmetricEncrypt(string publicKeyAsPem, byte[] payload)
    {
        CryptoKey d = CryptoKey.FromPublicKey(publicKeyAsPem, null);
        RSA rsa = d.GetRSA();
        byte[] result = rsa.PublicEncrypt(payload, RSA.Padding.PKCS1);
        rsa.Dispose();
        return result;
    }

    public static byte[] AsymmetricDecrypt(string privateKeyAsPem, byte[] payload)
    {
        //CryptoKey d = CryptoKey.FromPrivateKey(privateKeyAsPem, null);
        CryptoKey d = CryptoKey.FromPrivateKey(privateKeyAsPem, "pass");
        RSA rsa = d.GetRSA();
        byte[] result = rsa.PrivateDecrypt(payload, RSA.Padding.PKCS1);
        rsa.Dispose();
        return result;
    }

    static void Main(string[] args)
    {
        String t = @"-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbhcU+fhYmYLESZQAj1hKBXsNY
si0kYHNkxpP7ftxZiTFowWUVXHzQgkcYiCNnp3pt1eG6Vt0WDzyFYXqUUqugvX41
gkaIrKQw/sRiWEx49krcz7Vxr3dufL6Mg3eK7NyWDGsqwFrx/qVNqdhsHg12PGNx
IMY4UBtxin2A8pd4OwIDAQAB
-----END PUBLIC KEY-----";
        String p = @"-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,0A128C2617BD2EB1

pTDtXB+mockO7fvVqn4fwGnSb1Zv3HaMAALtpiB7Rn64eAHL7psKQIIM3qoshDWF
XgXDdTnMOGO7wtYkd9R7iJYxgt19EuEdtu2SLLXQuN4ll+JSR2R/34dF19iMXI30
d3pe7obTIwKdyRGuu8GgEm6bGai4pkqptP0HRA6qdMI2+Qfl9+VqUuvIm7tfpIRd
/ZLENe756IrGDvI7lGx39Md/H2sgAJsWkSYubhmtxVJ0IEvbPuKDC5V5oLyTOoy+
6sc6ly57C4XHaTLhAKnYEvZAddnXg/e/VtfmTpqKx3n7D6FAKo1RjAjeZqEvefZd
XAhh19YhZq3mdZNYUt7ojUarf/q3zrtTMLUxHdR0Be/VaQC5AE0d6quKyUQgxiti
XNRS8xk9IJJqJLFSHO3ET+oTfcs+kLPuUDHqq0hY/OgW/THcDgPY1cDwtOX9yuI3
YDoFTb3SXzRTmk2ui33f96wNPwzIAp9+TJzITxJYbF233Pz4YWuabrFuoNWZnwtT
E/o6wcGfvAXTQkAKzwfLbTDmg5SSiGokoEcgm7qpfmQxKdmV1LmbW88DuAgdWggm
Qf3ydZ2IrrtD1o+XP7JraeVOql0OK77pJh/bcr3bLiAT8YtsQUZLnOjkbDc3F1zW
BGr6eeqUHxY6cqKieokhl9cBBjWuxJQL2h997svBufWdNOjTA4+32lXzDzi7bUxC
xzIqZ7nm3YC2zUjla/l3Smz5KitqU5Y3Q9URpXOW+qMiPxmTHYOEcRDy9yh2U4iA
CoTD6q0ZNJLEo3EVcDB+26O663/mQLuR69xstUgqHpSzGvXbqrmezA==
-----END RSA PRIVATE KEY-----";

        System.Text.Encoding enc = System.Text.Encoding.ASCII;
        String s = "hello";
        byte[] payload = enc.GetBytes(s);
        Console.WriteLine("s: {0}", s);

        byte[] byte_encData = AsymmetricEncrypt(t,payload);

        String res;
        res = Convert.ToBase64String(byte_encData);
        Console.WriteLine("encypted: {0}", res);

        byte[] byte_decrypted = AsymmetricDecrypt(p, byte_encData);
        String res_unenc;
        res_unenc = Convert.ToBase64String(byte_decrypted);

        // works!
        Console.WriteLine("decrypted: {0}", res_unenc);

    }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文