Hi
I am trying to decrypt using RSA private key but i get OSStatus -50 error.
Any help is appriciated.
here is my code :
const size_t BUFFER_SIZE = 128;
const size_t CIPHER_BUFFER_SIZE = 1024;
const uint32_t PADDING = kSecPaddingPKCS1;
- (NSString *)privateKeyDecryptionOnString: (NSString*)message {
uint8_t *cipherBuffer;
uint8_t *decryptedBuffer;
const char* utf8String = [message cStringUsingEncoding:NSUTF8StringEncoding];
cipherBuffer = (uint8_t *)calloc(1024, sizeof(uint8_t));
decryptedBuffer = (uint8_t *)calloc(BUFFER_SIZE, sizeof(uint8_t));
cipherBuffer = (uint8_t *)utf8String;
NSLog(@"encrypted data: %s", cipherBuffer);
[self decryptWithPrivateKey:cipherBuffer plainBuffer:decryptedBuffer];
NSLog(@"decrypted data: %s", decryptedBuffer);
NSString *string = [NSString stringWithUTF8String:(char *)decryptedBuffer];
free(cipherBuffer);
free(decryptedBuffer);
return string;
}
- (void)decryptWithPrivateKey:(uint8_t *)cipherBuffer plainBuffer:(uint8_t *)plainBuffer
{
OSStatus status = noErr;
size_t cipherBufferSize = strlen((char *)cipherBuffer);
NSLog(@"decryptWithPrivateKey: length of buffer: %lu", BUFFER_SIZE);
NSLog(@"decryptWithPrivateKey: length of input: %lu", cipherBufferSize);
// DECRYPTION
size_t plainBufferSize = BUFFER_SIZE;
// Error handling
NSLog(@"%zu",SecKeyGetBlockSize([self getPrivateKeyRef]));
status = SecKeyDecrypt([self getPrivateKeyRef],
PADDING,
&cipherBuffer[0],
cipherBufferSize,
&plainBuffer[0],
&plainBufferSize
);
NSLog(@"decryption result code: %ld (size: %lu)", status, plainBufferSize);
NSLog(@"FINAL decrypted text: %s", plainBuffer);
}