OpenSSL RSA Plus decryption (. NET public key encryption/Linux side private key decryption)

Source: Internet
Author: User
Tags openssl rsa

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

    1. String publickeyfile = context.  Server.MapPath ("~/app_data/public.pem");
    2. using (RSA RSA = RSA. Frompublickey (BIO. File (Publickeyfile, "R" )))
    3. {
    4. Buffer = RSA. Publicencrypt (buffer, RSA. PADDING.OAEP);
    5. EncryptedKey = convert.tobase64string (buffer);
    6. }



Linux-side code

    1. BIO *b64, *bmem;
    2. Base64 decoding
    3. unsigned char *buffer = (unsigned char *) malloc (length);
    4. memset (buffer, 0, length);
    5. b64 = Bio_new (Bio_f_base64 ());
    6. Bio_set_flags (b64, BIO_FLAGS_BASE64_NO_NL);
    7. Bmem = Bio_new_mem_buf (szkey, length);
    8. Bmem = Bio_push (b64, Bmem);
    9. int len = bio_read (bmem, buffer, length);
    10. Bio_free_all (BMEM);
    11. Load private key
    12. BIO * key = NULL;
    13. RSA * r = NULL;
    14. Key = Bio_new (Bio_s_file ());
    15. Bio_read_filename (Key, "/val/xxx/private.pem");
    16. R = Pem_read_bio_rsaprivatekey (key, NULL, NULL, NULL);
    17. Bio_free_all (key);
    18. unsigned char * plaintext = (unsigned char *) malloc (len);
    19. memset (plaintext, 0, Len);
    20. Decrypt
    21. int ret = Rsa_private_decrypt (Rsa_size (R), buffer, plaintext, r, rsa_pkcs1_oaep_padding);
    22. Rsa_free (R);
    23. Free (plaintext);
    24. Free (buffer);

http://blog.csdn.net/wangjia184/article/details/6941242

OpenSSL RSA Plus decryption (. NET public key encryption/Linux side private key decryption)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.