OpenSSL EVP RSA encryption decryption
The interface provided by RSA.h can be used directly
The following test uses the RSA interface provided by EVP
1. RSA encryption and decryption provided by EVP
Main interface:
intEvp_pkey_encrypt_init (EVP_PKEY_CTX *ctx);intEvp_pkey_encrypt (EVP_PKEY_CTX *CTX, unsignedChar* out, size_t *Outlen,ConstUnsignedChar*inch, size_t Inlen);intEvp_pkey_decrypt_init (EVP_PKEY_CTX *ctx);intEvp_pkey_decrypt (EVP_PKEY_CTX *CTX, unsignedChar* out, size_t *Outlen,ConstUnsignedChar*inch, size_t Inlen);
Test code
Mr. Cheng Evp_key
// generate key Pair RSA *r = rsa_new (); int + ; *e = bn_new (); 65537 ); RSA_GENERATE_KEY_EX (R, Bits, E, NULL); *key; = evp_pkey_new (); Evp_pkey_set1_rsa (key, R);
Test encryption and decryption
//By default, Rsa_pkcs1_padding is used, where the maximum encryption block is 64-11=53, and a large number of arrays need to be grouped Char*srcstr ="01234567890123456789012345678901234567890123456789123"; //char *srcstr = "Hello World"; intEnclen =0; Charencdata[1024x768] = {0}; Chardecdata[1024x768] = {0}; intDeclen =0; printf ("src=%s\n", SRCSTR); //EncryptEVP_PKEY_CTX *Ectx; Ectx=evp_pkey_ctx_new (key, NULL); Evp_pkey_encrypt_init (ECTX); Evp_pkey_encrypt (Ectx, Encdata,&Enclen, Srcstr, strlen (SRCSTR)); //decryptionEVP_PKEY_CTX *Dctx; Dctx=evp_pkey_ctx_new (key, NULL); Evp_pkey_decrypt_init (DCTX); Evp_pkey_decrypt (Dctx, Decdata,&Declen, Encdata, Enclen); printf ("dec=%s\n", Decdata); Evp_pkey_ctx_free (ECTX); Evp_pkey_ctx_free (DCTX); Evp_pkey_free (key); Bn_free (e); Rsa_free (r);
Execution Result:
src=01234567890123456789012345678901234567890123456789123Dec= 012345678901234567890123456789012345678901234567891230
View Code
The above test is also the RSA packet encryption, if the encrypted data is longer, need to be called multiple times;
Complement mode: rsa_pkcs1_padding, the maximum grouping is rsa_size (r)-11
2. Additional signature and verification interfaces provided by EVP
intEvp_pkey_sign_init (EVP_PKEY_CTX *ctx);intEvp_pkey_sign (EVP_PKEY_CTX *CTX, unsignedChar*sig, size_t *Siglen,ConstUnsignedChar*TBS, size_t tbslen);intEvp_pkey_verify_init (EVP_PKEY_CTX *ctx);intEvp_pkey_verify (EVP_PKEY_CTX *CTX,ConstUnsignedChar*Sig, size_t Siglen,ConstUnsignedChar*TBS, size_t tbslen);intEvp_pkey_verify_recover_init (EVP_PKEY_CTX *ctx);intEvp_pkey_verify_recover (EVP_PKEY_CTX *CTX, unsignedChar*rout, size_t *Routlen,ConstUnsignedChar*sig, size_t Siglen);
3. About digital envelopes, signing envelopes, opening envelope interfaces
__owurintEvp_openinit (Evp_cipher_ctx *ctx,ConstEvp_cipher *type,ConstUnsignedChar*ek,intEkl,ConstUnsignedChar*iv, Evp_pkey *priv); __owurintEvp_openfinal (Evp_cipher_ctx *ctx, unsignedChar* out,int*outl); __owurintEvp_sealinit (Evp_cipher_ctx *ctx,ConstEvp_cipher *type, unsignedChar**ek,int*EKL, unsignedChar*IV, Evp_pkey**PUBK,intNPUBK); __owurintEvp_sealfinal (Evp_cipher_ctx *ctx, unsignedChar* out,int*OUTL);
Reference: https://www.openssl.org/docs/man1.1.0/crypto/EVP_PKEY_encrypt.html
Https://www.openssl.org/docs/man1.1.0/crypto/EVP_PKEY_verify.html
OpenSSL EVP RSA encryption decryption