c# - How do I import an RSA Public Key from .NET into OpenSSL -


I have a .NET program and a Borland Win32 program that needs to provide some cryptographic secure information. Now in the scheme, the .NET app is to create a public / private key pair, store the public key on the disk and keep the private key in memory as long as the .NET program is running.

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

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

What is the best way to export the key from NAT and in return OpenSSL Library?

Create a new RSACryptoServiceProvider in the .NET program. Export the public key as RSAParameters and type modulus and exponent values ​​on the disk. This way:

RSACRPTO service provider RSA = new RSACRPPS service provider (4096); // 4096 bit key RSAParameters par = rsa.ExportParameters (wrong); // Export the public key file WritAllBytes (@ "C: \ modulus.bin", par.Modulus); // Write modules and disposable disk discs. WritAllBytes ("C: \ exponent.bin", par.Exponent);

On the C ++ side, you will need to read modules and exponent values ​​from the disk, convert them to BIGNUM values ​​These values ​​will be loaded in the RSA key And then you can encrypt plain text and write on cipher text disc. Like this:

  RSA * key; Unsigned char * modulus; Unsigned char * exp; FILE * fp = fopen ("c: \\ modulus.bin", "rb"); // disk modulus = Read the modulus with the new unsigned char [512]; Memset (modulus, 0, 512); Fred (modulus, 512, 1, FP); Fclose (fp); Fp = fopen ("c: \\ exponent.bin", "rb"); // exponent disk exp = new unsigned char [3]; Memset (XP, 0, 3); Fred (XP, 3, 1, FP); Fclose (fp); BIGNUM * bn_mod = NULL; BIGNUM * bn_exp = NULL; Bn_mod = BN_bin2bn (modulus, 512, faucet); // Convert both values ​​to BIGNUM bn_exp = BN_bin2bn (XP, 3, Faucet); Key = RSA_new (); // Create a new RSA key- & gt; N = bn_mod; // value 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 four [valid]; Memeset (cipher, 0, valid); RSA_public_encrypt (strlen (plain), plain, cipher, key, RSA_PKCS1_PADDING); // Encrypt plain text fp = fopen ("C: \\ cipher.bin", "wb"); // to write secretext disc frit (cipher, 512, 1, fp); Fclose (fp); Finally you can take a ciphertext and decrypt it in C # without any difficulty.  
  byte [] cipher = file. Redlabats (@ "c: cipher.bin"); // file byte [] plain = rsa.Decrypt (cipher, false) read ciphertext; // decrypt ciphertext console WrightLine (ASCIIINCoding.ASCIIGetString (plain)); // Display decode and plain text  

Comments