Search code examples
iosobjective-ciphoneencryptionccavenue

I am getting Exe_Bad_Access(code=1 address=0X38) when I am integrating the CCAvenue Payment Gate way in IOS


I followed the following steps for integrating the non-shameless CCAvenue GateWay

Steps:-

I have my merchant id and access code's(url,ip).

  1. I am calling my server for getting the RSA key parameters (access_code ,order_id)

// my server already register in CCAvenue Server 2.my server calling the Avenue for RSA key

3.my server forwarding the RSA key

4.once i got i am removing extra lines"\n ",double quotes" "--" ",and "\"

// removing double quates


 NSString * newReplacedString2 = [rsaKey stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 

//removing /n in the key


  NSString * newReplacedString = [newReplacedString2 stringByReplacingOccurrencesOfString:@"\\n" withString:@""];


 //removing / in the key

 NSString * newReplacedString1 = [newReplacedString stringByReplacingOccurrencesOfString:@"\\" withString:@""]; 

5.and i am placing that key between

rsaKey = [NSString stringWithFormat:@"-----BEGIN PUBLIC KEY-----\n%@\n-----END PUBLIC KEY-----\n",newReplacedString1];

//*******//***//****//**//  output   //*******//***//****//**//
   -----BEGIN PUBLIC KEY-----
    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuV7OdmPSutrlOE9lF3YdW4ymGn+qselCOycMk95Tobw1PcqCeAWkrnxUECpAdnHGrUKmFbEDHs3wnwzLTbfa3GvE5dvvmluug78X3RYEFQiMh1QpfS5fBfvs4WQKw7oigko3G0UwZLZFnZ4E4WKTQi4wbCgjwQJFMnMGJfFYNcoSJluVg/q8z3bVxfDOV0ZPWccmvA3bTf9YFHKCC3clscQrGf1NPnBGcBGm+s06t3EljoSmpjtyTgSiGrqBZ8TSCQxoyXxS+RkhNTigg6mqW9hIisxYYqlbzvRnCDhuqgZfmP7t65QG5raELVE7d+Ia+dgh024luZ9+vSk4Qb65DQIDAQAB
    -----END PUBLIC KEY-----
  1. I am encrypting the amount and price using CCTool

    NSString *myRequestString = [NSString stringWithFormat:@"amount=%@&currency=%@",amount,currency];(2,INR)
    CCTool *ccTool = [[CCTool alloc] init];
    NSString *encVal = [ccTool encryptRSA:myRequestString key:rsaKey];
    

in that I am getting the Exe_Bad_Acess in

RSA *rsa = PEM_read_bio_RSA_PUBKEY(bufio, NULL,NULL,NULL);

that rsa getting error.

enter image description here

how to resolve this issue.Please Help me Guys.!!


Solution

  • hi thank you for all support, i finally resolved my issue.

    once u get the rsa key from ur server you need to remove the new lines "\n",slashes "\" and double quotes"\""

    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuV7OdmPSutrlOE9lF3YdW4ymGn+qselCOycMk95Tobw1PcqCeAWkrnxUECpAdnHGrUKmFbEDHs3wnwzLTbfa3GvE5dvvmluug78X3RYEFQiMh1QpfS5fBfvs4WQKw7oigko3G0UwZLZFnZ4E4WKTQi4wbCgjwQJFMnMGJfFYNcoSJluVg/q8z3bVxfDOV0ZPWccmvA3bTf9YFHKCC3clscQrGf1NPnBGcBGm+s06t3EljoSmpjtyTgSiGrqBZ8TSCQxoyXxS+RkhNTigg6mqW9hIisxYYqlbzvRnCDhuqgZfmP7t65QG5raELVE7d+Ia+dgh024luZ9+vSk4Qb65DQIDAQAB
    

    Here we need to convert it into the input for the ba64encryption format.

    https://gist.github.com/superwills/5415344#file-openssl-rsa-encryption-sample-L154

    u need to convert our string exactly same, 1. Each line of "string" must be 64 characters wide. 2.it must be terminated with a newline

    I am updating my Code.For Me its Working Fine.

      NSString *rsaKey = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    

    // NSString *rsaKey = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

            NSLog(@"%@",rsaKey);
    

    // rsaKey = [rsaKey stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; // NSLog(@"%@",rsaKey);

            // removing double quates
            NSString * newReplacedString2 = [rsaKey stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    
            NSLog(@"%@",rsaKey);
    
            //removing \n in the key
            NSString * newReplacedString = [newReplacedString2 stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
    
            NSLog(@"%@",rsaKey);
    
    
            //removing \ in the key
            NSString * newReplacedString1 = [newReplacedString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
            NSLog(@"%@",newReplacedString1);
    
    
            NSString * abc = [NSString stringWithFormat:@"%@", newReplacedString1];
            NSMutableString *sss=[NSMutableString new];
            int j=(int)([abc length]/63);
            for (int i=0; i<=j; i++) {
                int k= i*63;
                NSString * newString;
                if (i != j) {
                    newString = [abc substringWithRange:NSMakeRange(k,63)];
                    NSLog(@"%lu",(unsigned long)newString.length);
                    newString=[NSString stringWithFormat:@"%@",newString];
                }else{
                    newString = [abc substringWithRange:NSMakeRange(k,[abc length]-k)];
                    NSLog(@"%lu",(unsigned long)newString.length);
                    if (newString.length !=0)
                        newString=[NSString stringWithFormat:@"%@",newString];
                }
                if (newString.length !=0)
                    [sss appendString:[NSString stringWithFormat:@"%@\n",newString]];
            }
            NSLog(@"%@",sss);
    
    
            rsaKey = [NSString stringWithFormat:@"-----BEGIN PUBLIC KEY-----\n%@-----END PUBLIC KEY-----\n",sss];
            NSLog(@"%@",rsaKey);
    
            //Encrypting Card Details
                NSString *myRequestString = [NSString stringWithFormat:@"amount=%@&currency=%@",amount,currency];
                CCTool *ccTool = [[CCTool alloc] init];
    
    
            NSLog(@"emcrpted data  skfjsf jakdfhjklfhjk%@",[ccTool encryptRSA:myRequestString key:rsaKey]);
    
    
    
    
    
    
                Happy Coding. :)