Requirements in. NET side to generate a public-private key pair.
And then in. NET side using RSA public key encryption, using RSA private key decryption on Linux side.
The initial attempt was:. NET end use RSACryptoServiceProvider; The Linux side uses OpenSSL
For a full 5 hours, there are wood, there are wood ah! NET RSA and OpenSSL is not on, there are wood, ah, there are wood ah!
People get dizzy or not. The final solution was replaced by,. NET End uses openssl.net.
. NET-side code
- String publickeyfile = context. Server.MapPath ("~/app_data/public.pem");
- using (RSA RSA = RSA. Frompublickey (BIO. File (Publickeyfile, "R" )))
- {
- Buffer = RSA. Publicencrypt (buffer, RSA. PADDING.OAEP);
- EncryptedKey = convert.tobase64string (buffer);
- }
Linux-side code
- BIO *b64, *bmem;
- Base64 decoding
- unsigned char *buffer = (unsigned char *) malloc (length);
- memset (buffer, 0, length);
- b64 = Bio_new (Bio_f_base64 ());
- Bio_set_flags (b64, BIO_FLAGS_BASE64_NO_NL);
- Bmem = Bio_new_mem_buf (szkey, length);
- Bmem = Bio_push (b64, Bmem);
- int len = bio_read (bmem, buffer, length);
- Bio_free_all (BMEM);
- Load private key
- BIO * key = NULL;
- RSA * r = NULL;
- Key = Bio_new (Bio_s_file ());
- Bio_read_filename (Key, "/val/xxx/private.pem");
- R = Pem_read_bio_rsaprivatekey (key, NULL, NULL, NULL);
- Bio_free_all (key);
- unsigned char * plaintext = (unsigned char *) malloc (len);
- memset (plaintext, 0, Len);
- Decrypt
- int ret = Rsa_private_decrypt (Rsa_size (R), buffer, plaintext, r, rsa_pkcs1_oaep_padding);
- Rsa_free (R);
- Free (plaintext);
- Free (buffer);
http://blog.csdn.net/wangjia184/article/details/6941242
OpenSSL RSA Plus decryption (. NET public key encryption/Linux side private key decryption)