SSL-simple of RSA

Source: Internet
Author: User
Tags modulus asymmetric encryption
RSA is an asymmetric encryption algorithm.

Features:
Encryption and decryption use a completely different and completely matched pair of public and private keys

-- RSA key generation
RSA * rsa_generate_key (INT bits, unsigned long e, void (* callback) (INT, Int, void *), void * cb_arg );

-- RSA encryption and decryption
Int rsa_public_encrypt (int flen, const unsigned char * From, unsigned char * To, RSA * RSA, int padding );
Int rsa_private_decrypt (int flen, const unsigned char * From, unsigned char * To, RSA * RSA, int padding );

-- RSA signature and Verification
The RSA signature operation uses the private key to encrypt the hash value of the signed message.
Int rsa_sign (INT type, const unsigned char * m, unsigned int m_length, unsigned char * sigret, unsigned int * siglen, RSA * RSA );
Int rsa_verify (INT type, const unsigned char * m, unsigned int m_length, unsigned char * sigbuf, unsigned int siglen, RSA * RSA );

/** <Br/> * Open SSL guide <br/> * http://www.openssl.org/docs/crypto//rsa.html <br/> */<br/> # include <strings. h> <br/> # include <OpenSSL/RSA. h> <br/> # include <OpenSSL/err. h> <br/> // # define rsa_f4 0x10001l/65537 <br/> # define modulus "c8fbcf21" <br/> # define public_exponent rsa_f4 <br/> # define private_exponent" 97b55d7d "<br/> RSA * my_generate_key () <br/>{< br/> bignum * BNN, * BNE, * BND; <br/> BNN = Bn_new (); <br/> BNE = bn_new (); <br/> BND = bn_new (); <br/> bn_hex2bn (& BNN, modulus ); <br/> bn_set_word (BNE, public_exponent); <br/> bn_hex2bn (& BND, private_exponent); <br/> RSA * r = rsa_new (); <br/> r-> N = BNN; <br/> r-> E = BNE; <br/> r-> d = BND; <br/> return R; <br/>}< br/> int main () <br/>{< br/> int ret, FLEN; <br/> unsigned char * cleartext = (unsigned char *) "text"; <br/> unsigned char * Cipher Text; <br/> // RSA * r = my_generate_key (); <br/> // RSA * r = rsa_generate_key (512, rsa_f4, null, null ); <br/> RSA * r = rsa_generate_key (1024, rsa_f4, null, null); <br/> // RSA * r_public = rsapublickey_dup (R ); <br/> // RSA * r_private = rsw.vatekey_dup (r); <br/> FLEN = rsa_size (r); <br/> rsa_print_fp (stdout, R, FLEN ); <br/> ciphertext = (unsigned char *) malloc (FLEN); <br/> bzero (ciphertext, FLEN); <br/> P Rintf ("begin encrypt... /n "); <br/> ret = rsa_public_encrypt (FLEN, cleartext, ciphertext, R, rsa_no_padding); <br/> If (Ret <0) <br/>{< br/> printf ("encrypt failed! "); <Br/> return 1; <br/>}< br/> printf (" Size: % d/N ", RET ); <br/> printf ("cleartext: % s/n", cleartext); <br/> printf ("ciphertext :"); <br/> for (INT I = 0; I <ret; I ++) <br/> {<br/> printf ("% 02x ", ciphertext [I]); <br/>}< br/> printf ("/N"); </P> <p> printf ("/nbegin decrypt... /n "); <br/> ret = rsa_private_decrypt (FLEN, ciphertext, cleartext, R, rsa_no_padding); <br/> If (Ret <0) <br/> {<br/> printf ("d Ecrypt failed! "); <Br/> return 1; <br/>}< br/> printf (" Size: % d/N ", RET ); <br/> printf ("cleartext: % s/n", cleartext); <br/> printf ("ciphertext :"); <br/> for (INT I = 0; I <ret; I ++) <br/> {<br/> printf ("% 02x ", ciphertext [I]); <br/>}< br/> printf ("/N"); <br/> free (ciphertext ); <br/> rsa_free (r); <br/> return 0; <br/>}< br/>


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.