0

I got sigabrt error when i encrypt a string using following code. NSString *name=@"Mobinius" where key=@"CollaborationKey";

Desired output = vKu5r%2fKEcFpcA62QjBG22w%3d%3d;

code is:

- (NSString *)RC2EncryptWithKey:(NSString *)keyval {
NSData *data = [self dataUsingEncoding:NSASCIIStringEncoding];
const char *keyPtr = (const char *)[keyval cStringUsingEncoding:NSASCIIStringEncoding];
const char *scrambleiv = (const char *)[keyval cStringUsingEncoding:NSASCIIStringEncoding];
NSUInteger dataLength = [data length];
size_t movedBytes=0;
size_t totalBytesWritten=0;
CCCryptorRef cryptor=NULL;
CCCryptorStatus cryptStatus = CCCryptorCreate(kCCEncrypt, kCCAlgorithmRC2, kCCOptionPKCS7Padding, keyPtr, [keyval length], scrambleiv, &cryptor);
size_t bufferPtrSize = CCCryptorGetOutputLength(cryptor, [data length]+4, true);
uint8_t *bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t) );
memset((void *)bufferPtr, 0x0, bufferPtrSize);
uint8_t *ptr = bufferPtr;
// Set up initial size.
size_t remainingBytes = bufferPtrSize;

StringPtr * length = malloc(4);
length[0] = (unsigned char *)(self.length & 0xFF);
length[1] = (unsigned char *)((self.length >> 8) & 0xFF);
length[2] = (unsigned char *)((self.length >> 16) & 0xFF);
length[3] = (unsigned char *)((self.length >> 24) & 0xFF);

cryptStatus = CCCryptorUpdate(cryptor, length, 4, ptr,remainingBytes,&movedBytes);

cryptStatus = CCCryptorUpdate(cryptor, [data bytes], dataLength, ptr,remainingBytes,&movedBytes);

ptr += movedBytes;
remainingBytes -= movedBytes;
totalBytesWritten += movedBytes;

cryptStatus = CCCryptorFinal(cryptor, ptr,remainingBytes,&movedBytes);
totalBytesWritten += movedBytes;

NSData *dataStr=[NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)totalBytesWritten];
NSString *result = [dataStr base64EncodingWithLineLength:0];//[dataStr base64Encoding];
NSString *replacedStr =[result stringByReplacingOccurrencesOfString:@"+" withString:@"@"];
NSString *urlEncode=[replacedStr URLEncodedString_ch];
return urlEncode;}

- (NSString *) URLEncodedString_ch {
NSMutableString * output = [NSMutableString string];
const unsigned char * source = (const unsigned char *)[self UTF8String];
int sourceLen = (int)strlen((const char *)source);
for (int i = 0; i < sourceLen; ++i) {
    const unsigned char thisChar = source[i];
    if (thisChar == ' '){
        [output appendString:@"+"];
    } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
               (thisChar >= 'a' && thisChar <= 'z') ||
               (thisChar >= 'A' && thisChar <= 'Z') ||
               (thisChar >= '0' && thisChar <= '9')) {
        [output appendFormat:@"%c", thisChar];
    } else {
        [output appendFormat:@"%%%02x", thisChar];
    }
}
return output;}

Please help me to resolve this problem. Thanks for all to reply.

  • please provide the full code and error message. – katzenhut May 26 '14 at 09:49
  • It looks like you are making this a Category on `NSString`, that is not a good idea and just complicates things. Make things as simple as possible, crypto is hard enough. – zaph May 26 '14 at 15:37
  • Get a simple crypto method that is not a Category. If you need Base64 encoding or URL encoding do that before and/or after the encryption/decryption. Keep these things separate, that makes things much simpler and obeys the principle of Single Responsibility. There are Apple routines to URL encode and Base64 encode use them, search Apple dove and/or SO for information on them. – zaph May 26 '14 at 15:49

0 Answers0