如何设置上RSAParameters的公钥和私钥用于的RSACryptoServiceProvider? [英] How do I set the public and private key on RSAParameters to use for RSACryptoServiceProvider?

查看:507
本文介绍了如何设置上RSAParameters的公钥和私钥用于的RSACryptoServiceProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了一些网上的教程,并产生了公钥和私钥,但如何将它们设置在C#中使用?以下是我有:

 字符串公钥=...; 
串privateKey =...;
UnicodeEncoding ByteConverter =新UnicodeEncoding();
字节[] = dataToEncrypt ByteConverter.GetBytes(数据进行加密);
字节[]的EncryptedData;
字节[] decryptedData;
串decrptedStr =;
使用(RSA的RSACryptoServiceProvider =新的RSACryptoServiceProvider()){
RSAParameters myRSAParameters = RSA.ExportParameters(真);
myRSAParameters.Modulus = ByteConverter.GetBytes(公钥);
myRSAParameters.Exponent = ByteConverter.GetBytes(privateKey);
的EncryptedData = RSAEncrypt(dataToEncrypt,myRSAParameters,FALSE);
decryptedData = RSADecrypt(的EncryptedData,myRSAParameters,FALSE);
decrptedStr = ByteConverter.GetString(decryptedData);
}

下面是加密/解密,我复制的海峡M $的网站功能:

 静态公共字节[] RSAEncrypt(字节[] DataToEncrypt,RSAParameters RSAKeyInfo,布尔DoOAEPPadding){
尝试{
字节[]的EncryptedData;
使用(RSA的RSACryptoServiceProvider =新的RSACryptoServiceProvider()){
RSA.ImportParameters(RSAKeyInfo);
的EncryptedData = RSA.Encrypt(DataToEncrypt,DoOAEPPadding);
}
返回的EncryptedData;
}赶上(CryptographicException五){
Console.WriteLine(e.Message);

返回NULL;
}

}

静态公共字节[] RSADecrypt(字节[] DataToDecrypt,RSAParameters RSAKeyInfo,布尔DoOAEPPadding){
尝试{
字节[] decryptedData;
使用(RSA的RSACryptoServiceProvider =新的RSACryptoServiceProvider()){
RSA.ImportParameters(RSAKeyInfo);
decryptedData = RSA.Decrypt(DataToDecrypt,DoOAEPPadding);
}
返回decryptedData;
}赶上(CryptographicException五){
Console.WriteLine(e.ToString());

返回NULL;
}

}


解决方案

有许多问题在这里。你不指明你的公/私钥的编码,但我可以肯定,不管是编码,只需通过 ByteConverter.GetBytes得到字节相当于Unicode编码的字符串(...) 是行不通的。这些应该是字节数组相当于数字RSA参数。



第二个问题是你的公和私键解释。公钥不(仅仅)模量,和私钥不是指数。公钥由模数的的的(公共)指数(的指数属性 RSAParameters 类型和私钥的模量和的私人的指数(即 D RSAParameters财产键入)。然而,为了要用于解密的 RSAParameters 导入的RSACryptoServiceProvider 还必须具有 DP DQ InverseQ 属性中设置( P 问:也可能需要,虽然我没有测试过)。



再次不知道在你的公钥/私钥使用的编码,我不能给任何进一步的细节,如何提取适当的值来填充<$ C $的必要的属性C> RSAParameters 。


I read some tutorials online, and generated a public and private key, but how do I set them to be used in C#? Here is what I have:

        string publicKey = "...";
        string privateKey = "....";
        UnicodeEncoding ByteConverter = new UnicodeEncoding();
        byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
        byte[] encryptedData;
        byte[] decryptedData;
        string decrptedStr = "";
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
            RSAParameters myRSAParameters = RSA.ExportParameters(true);
            myRSAParameters.Modulus = ByteConverter.GetBytes(publicKey);
            myRSAParameters.Exponent = ByteConverter.GetBytes(privateKey);
            encryptedData = RSAEncrypt(dataToEncrypt, myRSAParameters, false);
            decryptedData = RSADecrypt(encryptedData, myRSAParameters, false);
            decrptedStr = ByteConverter.GetString(decryptedData);
        }

Here are the encrypt / decrypt function that I copied strait from M$ website:

    static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) {
        try {
            byte[] encryptedData;
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
                RSA.ImportParameters(RSAKeyInfo);
                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
            }
            return encryptedData;
        } catch (CryptographicException e) {
            Console.WriteLine(e.Message);

            return null;
        }

    }

    static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) {
        try {
            byte[] decryptedData;
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
                RSA.ImportParameters(RSAKeyInfo);
                decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
            }
            return decryptedData;
        } catch (CryptographicException e) {
            Console.WriteLine(e.ToString());

            return null;
        }

    }

解决方案

There are a number of problems here. You don't indicate the encoding of your public/private keys, but I can be sure that regardless of the encoding, simply getting the byte equivalent of the Unicode encoded string via ByteConverter.GetBytes(...) is not going to work. These should be the byte-array equivalent of the numeric RSA parameters.

The second problem is your interpretation of the "public" and "private" keys. The public key is not (just) the modulus, and the private key is not the exponent. The public key is comprised of the modulus and the (public) exponent (the Exponent property of the RSAParameters type, and the private key is the modulus and the private exponent (the D property of the RSAParameters type). However, in order to be used for decryption, the RSAParameters imported into the RSACryptoServiceProvider must also have the DP, DQ and InverseQ properties set (P and Q may also be required though I've not tested this).

Again, without knowing the encoding used in your public/private keys, I can't give any further details as to how to extract the appropriate values for populating the necessary properties of RSAParameters.

这篇关于如何设置上RSAParameters的公钥和私钥用于的RSACryptoServiceProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆