1 Reply
      Latest reply on Feb 15, 2017 2:00 AM by eskimo
      anush.shenoy Level 1 Level 1 (0 points)



        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];




            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],







            NSLog(@"decryption result code: %ld (size: %lu)", status, plainBufferSize);

            NSLog(@"FINAL decrypted text: %s", plainBuffer);



        • Re: decrypt using RSA private key error OSStatus -50
          eskimo Apple Staff Apple Staff (13,345 points)

          The error you’re getting is errSecParam, indicating that SecKeyDecrypt is not happy with the parameters you passed it.  As to the exactly what’s going wrong here, it’s hard to say without more info.  My biggest concern is that you seem to be treating cyphertext as a string.  That’s a really bad idea.  As an obvious example of the problem, this line:

          size_t cipherBufferSize = strlen((char *)cipherBuffer);

          assumes that the cyphertext contains no zero bytes, which is not a reasonable assumption.  However, there are more subtle things that might go wrong.

          The correct way to store binary data, like cypher text, in Cocoa is with the NSData type.  For an example of how to use this for cryptographic work, you should check out the CryptoCompatibility sample code.  Specifically, the QCCRSASmallCryptorCompat class shows how to encrypt and decrypt small chunks of data with SecKeyEncrypt and SecKeyDecrypt, respectively.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"