문제

.NET 프로그램과 Borland Win32 프로그램이있어 암호화 적으로 안전한 정보를 전달해야합니다. 현재 계획은 .NET 앱이 .NET 앱이 공개/개인 키 쌍을 만들고, 공개 키를 디스크에 저장하고 .NET 프로그램이 실행되는 한 개인 키를 메모리에 유지하도록하는 것입니다.

그런 다음 Borland 앱은 디스크에서 공개 키를 읽고 OpenSSL 라이브러리를 사용하여 공개 키로 데이터를 암호화하고 그 결과를 디스크에 씁니다.

마지막으로 .NET 앱은 암호화 된 데이터를 읽고 개인 키로 해독합니다.

.NET에서 키를 내보내고 OpenSSL 라이브러리로 가져 오는 가장 좋은 방법은 무엇입니까?

도움이 되었습니까?

해결책

.NET 프로그램에서 새로운 것을 만듭니다 RSACryptoServiceProvider. 공개 키를 다음과 같이 내보내십시오 RSAParameters 그리고 쓰기 Modulus 그리고 Exponent 디스크 값. 이와 같이:

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

다른 팁

C#에서 OpenSSL을 직접 사용할 수 있습니다. OpenSSL.net 싸개!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top