RE: RSA encryption and Decryption code in C language
Hello Enrico, Thanks for the code! It really helped. Regards, yamini. -- View this message in context: http://openssl.6102.n7.nabble.com/RSA-encryption-and-Decryption-code-in-C-language-tp45588p45759.html Sent from the OpenSSL - User mailing list archive at Nabble.com. __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager majord...@openssl.org
Re: RSA encryption and Decryption code in C language
Hi Yamini, I would suggest looking at the 'EVP Envelope' API : https://www.openssl.org/docs/crypto/EVP_SealInit.html Le 17/06/2013 19:26, yamini a écrit : Hello, I am implementing the DES algorithm between my client and server systems for encryption. The DES key is transmitted in encrypted form between Client and Server using RSA encryption and decryption. My idea of implementing the above task is creating RSA key (RSA_generate_key) and using the public key for encryption and private key for decryption. I have looked for sample codes to do this in C language but found nothing. So if anyone has any code snippets for this task please post them here. It would be very helpful. The code for RSA encryption and Decryption between client and server(client and server are on different machines). Thanks and Regards, Yamini. __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager majord...@openssl.org
Re: RSA encryption and Decryption code in C language
On 18 June 2013 09:43, Michel msa...@paybox.com wrote: Hi Yamini, I would suggest looking at the 'EVP Envelope' API : https://www.openssl.org/docs/crypto/EVP_SealInit.html Also see: http://wiki.openssl.org/index.php/EVP_Asymmetric_Encryption_and_Decryption_of_an_Envelope Matt __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager majord...@openssl.org
RE: RSA encryption and Decryption code in C language
Hi, I'm implementing a software very similar to yours. This is a small function that I used to generate private and public key: #include openssl/pem.h int main() { char * file_pem = key_priv; char * file_pem_pub = key_pub; FILE * fp; int bits = 1024; unsigned long exp = RSA_F4; RSA * rsa; rsa = RSA_generate_key(bits,exp,NULL,NULL); fp = fopen(file_pem,w); unsigned char* kstr =; //Password, change it ,it's just an example PEM_write_RSAPrivateKey(fp,rsa,EVP_des_ede3_cbc(),kstr,strlen(kstr),NULL,NULL); close(fp); fp = fopen(file_pem_pub,w); PEM_write_RSAPublicKey(fp,rsa); close(fp); RSA_free(rsa); } This function is called by client, it is self explicative: RSA * create_and_set_context() { RSA * rsa = RSA_new(); FILE * fp = fopen(key_pub,r); if( fp == NULL) return NULL; RSA * rs = PEM_read_RSAPublicKey(fp, rsa, NULL,NULL); return rs; } This is function called by client to encrypt symmetric key to send to Server. /* Key is the simmetryc key, to is a buffer */ int encrypt_simmetric_key(unsigned char *key, unsigned char *to, int size, RSA * rsa) { return RSA_public_encrypt(size, key, to, rsa, RSA_PKCS1_PADDING ); } *** Now, server side: RSA * create_and_set_context() { OpenSSL_add_all_algorithms(); RSA * rsa = RSA_new(); FILE * fp = fopen(key_priv,r); unsigned char* kstr =XX; if( fp == NULL) return NULL; RSA * rs = PEM_read_RSAPrivateKey(fp, rsa, NULL,kstr); return rs; } Then, supposed that buf is the buffer where is stored the symmetric key just received with a socket by Server: unsigned char* getSimKey(char * buf, RSA* rsa) { unsigned char* to = malloc(RSA_size(rsa)); // RSA_size(rsa) is the modulus if( to == NULL) return NULL; int k = RSA_private_decrypt(RSA_size(rsa), (unsigned char*)buf, to, rsa, RSA_PKCS1_PADDING); if( k == -1) return NULL; printf(K: %d\n,k); int i = 0; for(; i k; i++) printbyte(to[i]); return to; } That's all. Sorry for my bad english, I hope my code will help. Bye Enrico
Re: RSA encryption and Decryption code in C language
Hello Michel, Thanks for the link. I have the following code. int main() { RSA *key; unsigned char input_ptext[] = 58FD6F1C310FC9D0194FB8B0E99070A6CBA3473BFE69F953E60E99070A6CBA3473BFE69F953E0E99070A6CBA3473BFE69F953E0E99070A6CBAE; unsigned char ctext[256]; unsigned char ptext[256]; int n,i; ERR_clear_error(); key = RSA_generate_key(1024,65537,NULL,NULL); printf(the size of input_text is %ld\n, sizeof(input_ptext)); if (!key) return 0; n = RSA_size(key); n = RSA_public_encrypt(sizeof(input_ptext) - 1,input_ptext,ctext,key,RSA_PKCS1_PADDING); if (n 0) return 0; n = RSA_private_decrypt(n,ctext,ptext,key,RSA_PKCS1_PADDING); if (n 0) return 0; RSA_free(key); printf(the decrypted text is %s\n,ptext); if (memcmp(input_ptext,ptext,sizeof(input_ptext) - 1)) return 0; printf(Finished\n); printf(the decrypted text is %s\n,ptext); return 1; } But the problem is, I have to encrypt it at clients side and decrypt it at servers side. In the above program I generated the key at clients side. But How do I transport the public key to the other party for it to generate the private key? If I send the key using TCP/IP channel, that makes the system vulnerable, which is not desirable. So how do I transport the keys between client and the server. Thanks and Regards, Yamini. On Tue, Jun 18, 2013 at 1:43 AM, Michel msa...@paybox.com wrote: Hi Yamini, I would suggest looking at the 'EVP Envelope' API : https://www.openssl.org/docs/**crypto/EVP_SealInit.htmlhttps://www.openssl.org/docs/crypto/EVP_SealInit.html Le 17/06/2013 19:26, yamini a écrit : Hello, I am implementing the DES algorithm between my client and server systems for encryption. The DES key is transmitted in encrypted form between Client and Server using RSA encryption and decryption. My idea of implementing the above task is creating RSA key (RSA_generate_key) and using the public key for encryption and private key for decryption. I have looked for sample codes to do this in C language but found nothing. So if anyone has any code snippets for this task please post them here. It would be very helpful. The code for RSA encryption and Decryption between client and server(client and server are on different machines). Thanks and Regards, Yamini.
Re: RSA encryption and Decryption code in C language
You cannot generate a private key from a public key. Typically, the receiver generates the key pair and sends the public key to the sender. The sender encrypts with the public key. The receiver decrypts with the private key. A typical format for sending a public key across a channel is an X.509 certificate. On 6/18/2013 1:36 PM, Hemayamini Kurra wrote: But the problem is, I have to encrypt it at clients side and decrypt it at servers side. In the above program I generated the key at clients side. But How do I transport the public key to the other party for it to generate the private key? If I send the key using TCP/IP channel, that makes the system vulnerable, which is not desirable. So how do I transport the keys between client and the server. __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager majord...@openssl.org