如何导入一个RSA公钥从.net到OpenSSL的 [英] How do I import an RSA Public Key from .NET into OpenSSL

查看:273
本文介绍了如何导入一个RSA公钥从.net到OpenSSL的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET程序和需要通过一些加密安全信息的Borland的Win32程序。该计划目前是有.NET应用程序创建一个公钥/私钥对,存储在磁盘上的公钥和保持私钥在内存中,只要.NET程序正在运行。

I have a .NET program and a Borland Win32 program that need to pass some cryptographically secure information. The plan right now is to have the .NET app create a public/private key pair, store the public key on disk and keep the private key in memory for as long as the .NET program is running.

Borland的应用程序,然后会从磁盘读取公钥并使用OpenSSL库的数据用公钥加密和该结果写入到磁盘。

The Borland app will then read the public key from disk and use the OpenSSL library to encrypt the data with the public key and write that result to disk.

最后.net应用程序将读取加密的数据,并用私钥解密。

Finally the .NET app will read the encrypted data and decrypt it with the private key.

什么是最好的办法,从.NET并反过来将其导入OpenSSL库导出密钥?

What is the best way to export the key from .NET and in turn import it into the OpenSSL library?

推荐答案

在.NET程序创建一个新的的RSACryptoServiceProvider 。导出公钥 RSAParameters 和写入指数值到磁盘。像这样的:

In the .NET program create a new RSACryptoServiceProvider. Export the public key as RSAParameters and write the Modulus and Exponent values to disk. Like this:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(4096); //4096 bit key
RSAParameters par = rsa.ExportParameters(false); // export the public key

File.WriteAllBytes(@"C:\modulus.bin", par.Modulus); // write the modulus and the exponent to disk
File.WriteAllBytes(@"C:\exponent.bin", par.Exponent);

在C ++的一面,你需要从磁盘中读取系数和指数值将它们转换成 BIGNUM 值。这些值将被装入一个RSA密钥,然后可以加密的纯文本和写密文到磁盘。像这样的:

On the C++ side you'll need to read the modulus and exponent values from disk convert them into BIGNUM values. These values will be loaded into an RSA key and then you can encrypt the plain text and write the cipher text to disk. Like this:

RSA * key;

unsigned char *modulus; 
unsigned char *exp; 

FILE * fp = fopen("c:\\modulus.bin", "rb"); // Read the modulus from disk
modulus = new unsigned char[512];
memset(modulus, 0, 512);
fread(modulus, 512, 1, fp);
fclose(fp);

fp = fopen("c:\\exponent.bin", "rb"); // Read the exponent from disk
exp = new unsigned char[3];
memset(exp, 0, 3);
fread(exp, 3, 1, fp);
fclose(fp);

BIGNUM * bn_mod = NULL;
BIGNUM * bn_exp = NULL;

bn_mod = BN_bin2bn(modulus, 512, NULL); // Convert both values to BIGNUM
bn_exp = BN_bin2bn(exp, 3, NULL);

key = RSA_new(); // Create a new RSA key
key->n = bn_mod; // Assign in the values
key->e = bn_exp;
key->d = NULL;
key->p = NULL;
key->q = NULL;

int maxSize = RSA_size(key); // Find the length of the cipher text

cipher = new char[valid];
memset(cipher, 0, valid);
RSA_public_encrypt(strlen(plain), plain, cipher, key, RSA_PKCS1_PADDING); // Encrypt plaintext

fp = fopen("C:\\cipher.bin", "wb"); // write ciphertext to disk
fwrite(cipher, 512, 1, fp);
fclose(fp);



最后,你可以把密文进行解密在C#中没有任何困难。

Finally you can take the ciphertext and decrypt it in C# without any difficulty.

byte[] cipher = File.ReadAllBytes(@"c:\cipher.bin"); // Read ciphertext from file
byte[] plain = rsa.Decrypt(cipher, false); // Decrypt ciphertext

Console.WriteLine(ASCIIEncoding.ASCII.GetString(plain)); // Decode and display plain text

这篇关于如何导入一个RSA公钥从.net到OpenSSL的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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