Sponsored Content
Full Discussion: Problem returning string
Top Forums Programming Problem returning string Post 302266319 by Treasa on Wednesday 10th of December 2008 01:09:21 AM
Old 12-10-2008
Problem returning string

Hi all,

I have been trying with RSA encryption and decryption. I was able to write a code which worked fine. Then I split it into functions as part of a requirement. However I am not getting the expected result everytime. I know it has something to do with how I am returning but it just does not seem to work out. The original program is here:
PEM_read_RSAPublicKey returns NULL
The modified code is here:
Code:
#include <stdio.h>
#include <algorithm>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <fstream.h>
#include <sys/stat.h>
#include "openssl/crypto.h"
#include "openssl/x509.h"
#include "openssl/pem.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
#include "openssl/rsa.h"

int passphrase_cb(char *buf, int len,int  flag, void *u)
{
char *passphrase="mypassword";
cout<<"Inside callback"<<endl;
int passphrase_len;
passphrase_len = strlen(passphrase);

/* Check sufficient space exists for the passphrase including
* the NULL terminator in the buffer, otherwise truncate.
*/
if (passphrase_len > (len-1))
passphrase_len = (len-1);
memcpy(buf, passphrase, passphrase_len);

/* return the actual passphrase length */
return (passphrase_len);
}

void generateRSAKeys(){
   cout<<"Generating RSA keys"<<endl;
   RSA *rsa=NULL;
   FILE *fp;
   OpenSSL_add_all_algorithms();
   if ((rsa=RSA_generate_key(2048,65537,NULL,NULL)) == NULL){
      cout<<"Creation of keys failed"<<endl;
    }else{
      cout << "success"<<endl;
   }
   fp = fopen("server_public.key","w");
   if (!PEM_write_RSAPublicKey(fp, rsa )){
       cout<<"Error writing public key"<<endl;
    }
    else{
       cout<<"Public key written succesfully"<<endl;
    }
   fclose(fp);
    fp = fopen("server_private.key","w");
     if (!PEM_write_RSAPrivateKey(fp, rsa, EVP_des_ede3_cbc(), (unsigned char *)"mypassword" , strlen("mypassword"),NULL, NULL)){
       cout<<"Error writing private key"<<endl;
    }
    else{
       cout<<"Private key written succesfully"<<endl;
    }
    fclose(fp);
    RSA_free(rsa);
}

/***************************************************************/


std::string newEncryptString(const char *input) {
   FILE *pubFile, *privFile;
   struct stat info;
   int pubRet=stat("/home/mtc/test/server_public.key",&info);
   int privRet=stat("/home/mtc/test/server_private.key",&info);
   cout<<"pubRet="<<pubRet<<endl;
   cout<<"privRet="<<privRet<<endl;
   if ((pubRet!=0) || (privRet!=0)) {
      cout<<"Public and Private key files not present"<<endl;
      generateRSAKeys();
   }
   pubFile=fopen("server_public.key","r");
   RSA *rsa=RSA_new();
   RSA_blinding_off(rsa);
   rsa = PEM_read_RSAPublicKey(pubFile, NULL, NULL, NULL);
   if (rsa==NULL){
      cout<<"Reading of Public key not successfull"<<endl;
   }
   char *encryptedString=(char *)malloc(RSA_size(rsa));
   if (RSA_public_encrypt(strlen(input)+1,(unsigned char*)input,(unsigned char*)encryptedString,rsa,RSA_PKCS1_PADDING)==-1){
      cout<<"encryption failed "<<endl;
   }
   RSA_free(rsa);
   fclose(pubFile);
   std::string cipherText=encryptedString;
   cout<<"Returning from encrypt"<<endl;
   return cipherText;
}


/*****************************************************************/
   std::string newDecryptString(std::string& input) {
   cout<<"Length here "<<sizeof(input)<<endl;
   OpenSSL_add_all_algorithms();
   cout<<"Reached after adding alogo"<<endl;
   FILE *privFile;
   privFile = fopen("/home/mtc/test/server_private.key","r");
   RSA *rsa=RSA_new();
   RSA_blinding_off(rsa);
   int (*cb)(char *buf, int buflen, int flag, void *u);
   cb=passphrase_cb;
   cout<<"Cb value inside decrypt:"<<cb<<endl;
   rsa = PEM_read_RSAPrivateKey(privFile, NULL, cb, NULL);
   if (rsa==NULL){
      cout<<"Reading of private key not successful"<<endl;
   }
   if (RSA_check_key(rsa) != 1){
      cout<<"RSA_check_key(): PrivateKey check failed\n";
   }
   char *plainText=(char *)malloc(RSA_size(rsa));
   if (RSA_private_decrypt(RSA_size(rsa),(unsigned char *)input.c_str(),(unsigned char*)plainText,rsa,RSA_PKCS1_PADDING)==-1){
      cout<<"Decryption failed "<<endl;
   }
   fclose(privFile);
   int len=RSA_size(rsa);
   RSA_free(rsa);
   std::string decryptedString(plainText);
   free(plainText);
   return decryptedString;
}


/*********************************************************************/


int main(int argc, char *argv[])
{
   std::string cipherOut(newEncryptString("trialstring"));
   cout<<"Plain string="<<newDecryptString(cipherOut)<<endl;
   return 0;
}

I am getting expected result only at times. Many times I get the error "Decryption failed". However no other error is happening. Could someone please suggest how to make this working fine?

Thanks
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

2. Programming

string returning function

I have two string returning function in ESQL/C char *segment_name(lbuffer) char *lbuffer; {..... and char *get_bpdvalue(f_name) char *f_name; {...... both declared above main() char *get_bpdvalue(); char *segment_name(); my problem is segment_name works on sprintf and strcpy... (5 Replies)
Discussion started by: jisc
5 Replies

3. UNIX for Dummies Questions & Answers

problem with if -z string

someone please help me out here. i am not a newbie. i just haven't posted on this board in years. i'm talking at least two and a half years. My last user name was TRUEST. i couldn't log into this name because i forgot my password and my aol email address has long been deleted. anyway, i'm... (1 Reply)
Discussion started by: Terrible
1 Replies

4. Shell Programming and Scripting

returning un executed string to shell prompt

Hi all, i'm pritty new to chell scripting I'm trying to find a way to return a value to shell without it executing. is there a special character that will encase a sting including the command to a shell without executing, so waiting for the user to press enter. say i wanted to return a value... (3 Replies)
Discussion started by: jvan
3 Replies

5. Shell Programming and Scripting

String comparison problem

Hi, can someone please help me!!! urgent! I have a strange issue here. I grep for 2 strings from a txt files and compare the string value. Though the string values are the same, they are compared as different values. Please help Case-1 -------- Here I grep for 2 different field values... (3 Replies)
Discussion started by: vani123
3 Replies

6. Programming

Returning local string value from a function in C

Hi, If I have a code like this, what are the potential problems do you see? const char* const retString() { return "hello"; /* string literal */ } My questions are: a) Since the string literal which is already a constant read only data (cannot be... (4 Replies)
Discussion started by: royalibrahim
4 Replies

7. Shell Programming and Scripting

Problem in comparing 2 files string by string

Hi Champs, I am a newbie to unix world, and I am trying to built a script which seems to be far tough to be done alone by me..... " I am having a raw csv file which contains around 50 fields..." From that file I have to grep 2 fields "A" and "B"....field "A" is to be aligned vertically... (11 Replies)
Discussion started by: jitendra.pat04
11 Replies

8. Shell Programming and Scripting

Parsing a long string string problem for procmail

Hi everyone, I am working on fetchmail + procmail to filter mails and I am having problem with parsing a long line in the body of the email. Could anyone help me construct a reg exp for this string below. It needs to match exactly as this string. GetRyt... (4 Replies)
Discussion started by: cwiggler
4 Replies

9. Shell Programming and Scripting

Problem in tokenizing the string

Supposed I have a string in the format: <service_name> = <ldap user FDN> : <password> like DNS = cn=user1,o=company : pwd I want to tokenize like: service name:DNS UserName: cn=user1,o=company Password: pwd. Because of the '=' sign between Service Name and LDAP Name I not... (8 Replies)
Discussion started by: saurabhkoar
8 Replies

10. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies
All times are GMT -4. The time now is 06:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy