Sigabrt error coming while encrypt a string

104 views Asked by At

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.

0

There are 0 answers