Using OpenSSL for RSA encryption and decryption instances

Source: Internet
Author: User
Tags decrypt openssl openssl api openssl rsa asymmetric encryption

Reprinted from: http://www.cnblogs.com/aLittleBitCool/archive/2011/09/22/2185418.html

OpenSSL is a powerful toolkit that integrates a wide range of cryptographic algorithms and utilities. We can use the command desk tools it provides to generate keys, certificates to encrypt and decrypt files, or to encrypt the transmitted information in code using the API interfaces it provides.

RSA is an asymmetric encryption algorithm. In short, the asymmetric encryption algorithm means that encrypting and decrypting a file requires two keys, one for encryption, the public key, one for decryption, and the private key. Certificates can be used to authorize the use of public keys.

Today a small study of the next OpenSSL RSA encryption, which mainly involves the use of public keys and keys to decrypt files, does not involve the operation of the certificate. What you want to know collectively is:

http://www.openssl.org/

http://blog.csdn.net/jiangsq12345/article/details/6066275

--------------------------------------------------------------------------------------------------------------- ------

Let's start with the simple use of the OpenSSL tool under the command desk:

Generate a key:

OpenSSL genrsa-out Test.key 1024

Here-out specifies the generation file. It is important to note that this file contains both the public key and the key, which means that the file can be used for encryption or decryption. The following 1024 is the length of the generated key.

OpenSSL can extract the public key from this file:

OpenSSL rsa-in test.key-pubout-out Test_pub.key

-in Specifies the input file,-out specifies the file name to extract the generated public key. At this point, we have a public key in hand, a private key (including the public key). You can now encrypt the file with the public key.

I created a hello text file in the directory and then encrypted the file with the public key generated earlier:

-in specifies the file to encrypt,-inkey specifies the key,-pubin indicates that it is encrypted with a pure public key file, and-out as the encrypted file.

Decrypt file:

OpenSSL rsautl-decrypt-in Hello.en-inkey test.key-out hello.de

-in Specifies the encrypted file,-inkey specifies the private key file,-out the decrypted file.

At this point, a cryptographic decryption process has ended. In the actual use may also include the certificate, this later has the opportunity to say again ~

--------------------------------------------------------------------------------------------------------------- ----
This article describes how the program uses the Test.key and Test_pub.key generated earlier to encrypt and decrypt information (or, of course, to generate a key file directly from the OpenSSL API).

Here is an example that uses an existing key to encrypt and decrypt the source string:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/rsa.h> #include <openssl/pem.h> #include <openssl/err.h> #define OPENSSLKEY "Test.key" #define PublicKey "Test_pub.key" # Define Buffsize char* my_encrypt (char *str,char *path_key);//Encrypt char* my_decrypt (char *str,char *path_key);//Decrypt int M
    Ain (void) {char *source= "I like dancing!";
    Char *ptr_en,*ptr_de;
    printf ("Source is:%s\n", source);
    Ptr_en=my_encrypt (Source,publickey);
    printf ("After encrypt:%s\n", ptr_en);
    Ptr_de=my_decrypt (Ptr_en,opensslkey);
    printf ("After decrypt:%s\n", Ptr_de);
    if (ptr_en!=null) {free (ptr_en);
    } if (Ptr_de!=null) {free (PTR_DE);
} return 0;
    } Char *my_encrypt (char *str,char *path_key) {char *p_en;
    RSA *p_rsa;
    FILE *file;
    int Flen,rsa_len;
        if ((File=fopen (Path_key, "R")) ==null) {perror ("Open key file Error");    
    return NULL; } if ((P_rsa=pem_rEad_rsa_pubkey (file,null,null,null)) ==null) {//if (P_rsa=pem_read_rsapublickey (file,null,null,null)) ==NULL) {
        I'm going to change it. However, whether the public key is detached from the source file err_print_errors_fp (stdout);
    return NULL;
    } flen=strlen (str);
    Rsa_len=rsa_size (P_RSA);
    p_en= (unsigned char *) malloc (rsa_len+1);
    memset (p_en,0,rsa_len+1);
    if (Rsa_public_encrypt (Rsa_len, (unsigned char *) str, (unsigned char*) p_en,p_rsa,rsa_no_padding) {return NULL;
    } rsa_free (P_RSA);
    fclose (file);
return p_en;
    } Char *my_decrypt (char *str,char *path_key) {char *p_de;
    RSA *p_rsa;
    FILE *file;
    int Rsa_len;
        if ((File=fopen (Path_key, "R")) ==null) {perror ("Open key file Error");
    return NULL;
        } if ((P_rsa=pem_read_rsaprivatekey (file,null,null,null)) ==null) {ERR_PRINT_ERRORS_FP (stdout);
    return NULL;
    } rsa_len=rsa_size (P_RSA);
    p_de= (unsigned char *) malloc (rsa_len+1);
    memset (p_de,0,rsa_len+1); if (Rsa_private_decrypt (Rsa_len, (unsigned char *) str, (unsigned char*) p_de,p_rsa,rsa_no_padding) {return NULL;
    } rsa_free (P_RSA);
    fclose (file);
return p_de; }


A rather odd question:

37, 38 in line to get the key from the file, found that if using OpenSSL provided by the Pem_read_rsapublickey method will continue to fail.

Estimated to be a problem with file format ~

Related Article

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.