1

I have googling a lot, but any answer help me with this problem:

Code:

MAIN DECRYPT in XRSA.m

- (NSData *) decryptWithString:(NSString *)content {

return [self RSADecryptData:[content dataUsingEncoding:NSUTF8StringEncoding]];



}

LOAD PRIVATE KEY .p12 in XRSA.m

#pragma mark - Private Key (.p12)
-(BOOL)setPrivateKey:(NSString *)privateKeyPath withPassphrase:(NSString *)password{

NSData *pkcs12key = [NSData dataWithContentsOfFile:privateKeyPath];
NSDictionary* options = NULL;
CFArrayRef importedItems = NULL;

if (password) {
    options = [NSDictionary dictionaryWithObjectsAndKeys: password, kSecImportExportPassphrase, nil];
}

OSStatus returnCode = SecPKCS12Import((__bridge CFDataRef) pkcs12key,
                                      (__bridge CFDictionaryRef) options,
                                      &importedItems);

if (returnCode != 0) {
    NSLog(@"SecPKCS12Import fail");
    return FALSE;
}

NSDictionary* item = (NSDictionary*) CFArrayGetValueAtIndex(importedItems, 0);
SecIdentityRef  identity = (__bridge SecIdentityRef) [item objectForKey:(__bridge NSString *) kSecImportItemIdentity];
SecIdentityCopyPrivateKey(identity, &privateKey);
if (privateKey == nil) {
    NSLog(@"SecIdentityCopyPrivateKey fail");
    return FALSE;
}

return TRUE;

}

Decrypt message in XRSA.m

#pragma mark - RSA Decryption
-(NSData *)RSADecryptData:(NSData *)content{

NSAssert(privateKey != nil,@"Private key can not be nil");

size_t cipherLen = content.length;
void *cipher = malloc(cipherLen);
[content getBytes:cipher length:cipherLen];
size_t plainLen = SecKeyGetBlockSize(privateKey) - 12;
void *plain = malloc(plainLen);

//SecKeyDecrypt(<#SecKeyRef key#>, <#SecPadding padding#>, <#const uint8_t *cipherText#>, <#size_t cipherTextLen#>, <#uint8_t *plainText#>, <#size_t *plainTextLen#>)
OSStatus returnCode = SecKeyDecrypt(privateKey, kSecPaddingPKCS1, cipher,cipherLen, plain, &plainLen);

NSData *result = nil;
if (returnCode != 0) {
    NSLog(@"SecKeyDecrypt fail. Error Code: %d", (int)returnCode);
}
else {
    result = [NSData dataWithBytes:plain
                            length:plainLen];
}

free(plain);
free(cipher);

return result;
}

in ViewControler.m:

NSString *privatekeyPath = [[NSBundle mainBundle] pathForResource:@"private_key" ofType:@"p12"];
XRSA *rsa2 = [XRSA alloc];

if([rsa2 setPrivateKey:privatekeyPath withPassphrase:@"Xs23tg"]){

        NSString *data = @"UKFpmRmyu1TUZLqcgHmCEGnHaT7+0j5fAaf57xzVR2/j/Qe0j+b5Lez7wya3jlARfzRuHSSZctsGs4gK2JX2LEqHmQLX2zRhLSSzyMlLnYPF8X4pjbDY5agjPlWf4FpFJnmwGr2XjdqRJzPZ9NvEJAns5dNKAh0lQ3nc3kDppfg=";
    [rsa2 decryptWithString:data];
}
else{

}

In RSADecryptData fuction, OSStaus is always return error code -9809.

Any ideas? Thanks for your time.

Kotik_o
  • 295
  • 6
  • 19

1 Answers1

0

There are a couple of possibilities:

In the line [content getBytes:cipher length:cipherLen]; you are not assigning that result to anything. Perhaps assign it to a const uint8_t * and pass into the SecKeyDecrypt function instead of content.

You should check to ensure that the cipherLen is less than the plainLen value. You didn't mention your key length, but that could be the cause of the failure. If you need to support larger message, you will need to decrypt in smaller chunk and iterate over your cipher.

picciano
  • 22,341
  • 9
  • 69
  • 82