3

暗号的に安全な情報を渡す必要がある .NET プログラムと Borland Win32 プログラムがあります。現在の計画では、.NET アプリで公開キーと秘密キーのペアを作成し、公開キーをディスクに保存し、.NET プログラムが実行されている限り、秘密キーをメモリに保持します。

次に、Borland アプリはディスクから公開鍵を読み取り、OpenSSL ライブラリを使用して公開鍵でデータを暗号化し、その結果をディスクに書き込みます。

最後に、.NET アプリは暗号化されたデータを読み取り、秘密鍵で復号化します。

キーを .NET からエクスポートし、それを OpenSSL ライブラリにインポートする最良の方法は何ですか?

4

2 に答える 2

5

.NET プログラムで、新しいRSACryptoServiceProvider. 公開鍵をエクスポートし、値と値をディスクにRSAParameters書き込みます。このような:ModulusExponent

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 キーに読み込まれ、平文を暗号化し、暗号文をディスクに書き込むことができます。このような:

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# で問題なく復号化できます。

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
于 2009-02-02T21:08:47.523 に答える
0

OpenSSL.NETラッパーを使用して、C# で OpenSSL を直接使用できます。

于 2009-09-02T12:50:45.500 に答える