【问题标题】:C# RSA implementation with OpenSSL keys & Bouncy castle使用 OpenSSL 密钥和充气城堡的 C# RSA 实现
【发布时间】:2015-06-07 22:04:25
【问题描述】:

我正在尝试使用 OpenSSL 生成的密钥对和 Bouncy Castle 在 C# 中实现字符串加密-解密。

OpenSSL 授予我密钥对,我将其分成 2 个文件。现在我正在使用 Bouncy Castle 的 Pemreader 读取密钥并将其更改为 AsymmetricKeyParameters。

下面的代码运行了,但是解密后的字符串和原来的不一样——我得到了一堆?。

如果我打印出密钥,它们看起来就像在文本文件中一样。

有人能指出我做错了什么吗?阅读程序或引擎使用似乎是原因。没有填充的 2048 位密钥,这种加密有多强?

        string test = "qwerty12345";
        AsymmetricKeyParameter keyparmeter = readPublicKey(public_path); // Read public key into string

        /* Print the test key */
        Console.WriteLine("test key = " + test);

        /* Convert test to byte array */
        byte[] bytes = new byte[test.Length * sizeof(char)];
        System.Buffer.BlockCopy(test.ToCharArray(), 0, bytes, 0, bytes.Length);

        byte[] cipheredbytes = null;

        /* Initiate rsa engine */
        RsaEngine e = new RsaEngine();
        e.Init(true, keyparmeter);          // initialize engine true, encrypting

        /* Crypt! */
        cipheredbytes = e.ProcessBlock(bytes, 0, bytes.Length);

        // ## NOW DECRYPTION ##

        /* Get the private key */
        AsymmetricKeyParameter privkeyparameter = readPrivKey(privkey_path);

        byte[] reversedbytes = null;

        /* Initiate rsa decrypting engine */
        RsaEngine d = new RsaEngine();
        d.Init(false, privkeyparameter);          // initialize engine false, decrypting

        /* Decrypt! */
        reversedbytes = d.ProcessBlock(cipheredbytes, 0, cipheredbytes.Length);

        char[] chars = new char[cipheredbytes.Length / sizeof(char)];
        System.Buffer.BlockCopy(cipheredbytes, 0, chars, 0, cipheredbytes.Length);
        string reversedtest = new string(chars);

    ### PEMREADING ###
    /* Convert PEM into AsymmetricKeyParameter */
    private AsymmetricKeyParameter readPublicKey(string path_to_key)
    {
        RsaKeyParameters asmkeypar;

        using(var reader = File.OpenText(path_to_key))
            asmkeypar = (RsaKeyParameters) new PemReader(reader).ReadObject();

        return asmkeypar;
    }

    /* Convert PEM into AsymmetricKeyParameter */
    private AsymmetricKeyParameter readPrivKey(string path_to_key)
    {
       AsymmetricCipherKeyPair asmkeypar;

        using (var reader = File.OpenText(path_to_key))
            asmkeypar = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject();

        return (RsaKeyParameters) asmkeypar.Private;
    }

【问题讨论】:

    标签: c# encryption openssl rsa bouncycastle


    【解决方案1】:

    您正在使用基本 RSA 算法。这也称为原始或教科书 RSA。基本上它执行模幂运算,但它不进行填充或取消填充。因此,您收到的是明文 + 已放在值前面的零,因为似乎没有发生取消填充。

    最后,您应该执行字符编码而不是System.Buffer.BlockCopy,后者可能会搞砸它,因为它必须在.NET 中对Unicode 编码的字符串进行操作。

    我可以将您推荐给有关加密的this question,它试图列出对原始/教科书 RSA 的所有可能攻击。有很多,你的代码安全的机会几乎为零。

    【讨论】:

      猜你喜欢
      • 2013-05-25
      • 2015-08-09
      • 2014-05-23
      • 2016-08-10
      • 2011-06-11
      • 2017-08-21
      • 2013-04-20
      • 2015-06-29
      • 2014-09-19
      相关资源
      最近更新 更多