简体   繁体   English

具有OpenSSL密钥和Bouncy Castle的C#RSA实现

[英]C# RSA implementation with OpenSSL keys & Bouncy castle

I'm trying to implement string encryption-decryption in C# using OpenSSL-generated keypair and Bouncy Castle. 我正在尝试使用OpenSSL生成的密钥对和Bouncy Castle在C#中实现字符串加密-解密。

OpenSSL granted me keypair, which I have separated in 2 files. OpenSSL授予我密钥对,我将其分成2个文件。 Now I am using Pemreader from Bouncy Castle to read the keys and change them to AsymmetricKeyParameters. 现在,我使用Bouncy Castle的Pemreader读取密钥并将其更改为AsymmetricKeyParameters。

The code below runs, but the decrypted string isn't the same as the original - I am getting a bunch of ?'s. 下面的代码可以运行,但是解密后的字符串与原始字符串不同-我得到了一堆?。

If I print out the keys, they seem just like in the text file. 如果我打印出密钥,它们看起来就像在文本文件中一样。

Could someone point out what I am doing wrong? 有人可以指出我做错了吗? The pemreading procedure or engine-using seem to be the cause. 原因是翻书程序或使用引擎。 How strong this encryption will be with 2048-bit key without padding? 无需填充的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;
    }

You are using the base RSA algorithm. 您正在使用基本RSA算法。 This is also known as raw or textbook RSA. 这也称为原始RSA或教科书RSA。 Basically it performs modular exponentiation, but it doesn't do the padding or unpadding. 基本上,它执行模幂运算,但不执行填充或取消填充操作。 So what you are receiving is the plaintext + the zero's that have been put in front of the value, as the unpadding does not seem to take place. 因此,您收到的是值前面的明文+零,因为似乎没有发生填充。

Finally, you should perform character encoding instead of System.Buffer.BlockCopy , the latter will probably make a mess out of it because it has to operate on a Unicode encoded string in .NET. 最后,您应该执行字符编码,而不是System.Buffer.BlockCopy ,后者可能会使情况变得一团糟,因为它必须对.NET中的Unicode编码的字符串进行操作。

I can refer you to this question on crypto that tries to list all the possible attacks on raw/textbook RSA. 我可以向您介绍有关加密的问题该问题试图列出对原始/教科书RSA的所有可能的攻击。 There are a lot, the chance that your code is secure is about zero. 很多情况下,您的代码安全的几率约为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM