Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6244915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:24:47+00:00 2026-05-24T12:24:47+00:00

I am trying to use the iPhone’s PKI libraries to encrypt a short string

  • 0

I am trying to use the iPhone’s PKI libraries to encrypt a short string (12345678), but I keep getting the error -9809 (i.e. errSSLCrypto) whenever I try to use SecKeyEncrypt. The SecureTransport.h header file describes this error simply as “underlying cryptographic error”, which wasn’t very meaningful.

My code is as follows:

- (NSData *)encryptDataWithPublicKey:(NSString *)plainText {

    OSStatus result = -1;

    NSData *plainTextData = [plainText dataUsingEncoding:NSASCIIStringEncoding];
    size_t plainTextLength = [plainTextData length];

    SecTrustRef trustRef;
    SecTrustResultType trustResult;

    SecPolicyRef policy = SecPolicyCreateBasicX509();

    result = SecTrustCreateWithCertificates(m_oCert, policy, &trustRef);

    if (result != errSecSuccess) {
        NSLog(@"Trust create failed with code: %d",result);
        return nil;
    }

    result = SecTrustEvaluate(trustRef, &trustResult);

    if (result != errSecSuccess) {
        NSLog(@"Trust eval failed with code: %d",result);

        CFRelease(trustRef);
        return nil;
    }

    SecKeyRef publicKey = SecTrustCopyPublicKey(trustRef);

    uint8_t *cipherTextBuf = NULL;
    size_t cipherTextLen = 0;

    size_t keyBlockSize = SecKeyGetBlockSize(publicKey);
    int maxInputSize = keyBlockSize - 11; //If using PKCS1 Padding, else keyBlockSize

    if (plainTextLength > maxInputSize) {
        //Fail
        NSLog(@"Data size is larger than max permitted!");

        CFRelease(trustRef);
        CFRelease(publicKey);
        CFRelease(policy);

        return nil;
    }

    cipherTextBuf = malloc(sizeof(uint8_t)*keyBlockSize);
    memset(cipherTextBuf,0,keyBlockSize);

    //result = SecKeyEncrypt(publicKey, kSecPaddingPKCS1, plainTextBuf, plainTextLength, cipherTextBuf, &cipherTextLen);
    result = SecKeyEncrypt(publicKey, kSecPaddingNone, (const uint8_t *)[plainTextData bytes], plainTextLength, cipherTextBuf, &cipherTextLen);

    NSData *cipherText = nil;
    if (result == errSecSuccess) {

        cipherText = [NSData dataWithBytes:cipherTextBuf length:cipherTextLen];

    } else {
        NSLog(@"Error detected: %d",result);
    }

    free(cipherTextBuf);
    cipherTextBuf = NULL;

    CFRelease(trustRef);
    CFRelease(publicKey);
    CFRelease(policy);

    return cipherText;
}

It does not matter what padding I use, they both give the same error. The public key is derived from a certificate supplied by my client, and I’ve checked to make sure that the key is valid. What am I doing wrong and how do I use the function properly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:24:48+00:00Added an answer on May 24, 2026 at 12:24 pm

    When using SecKeyEncrypt, the input cipherTextLength should be the the size of the output buffer. Setting

    size_t cipherTextLen = keyBlockSize;
    

    solved the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use Objective-C blocks in an iPhone 3.1.3 app but am
I'm trying to use gluLookAt to move the camera in my iPhone game, but
Im trying to use ALAssetsLibrary to load images fromthe photo album of iphone/ipad.Im getting
I'm trying to use the MFMailComposeViewController in an iPhone app I'm building, but I
I'm trying to use libRocket in an iPhone application (with cocos2d), but I can't
I’m trying to use the proprietary iPhone Safari properties -webkit-transition and -webkit-transform to make
I am trying to use Aptana to build an IPhone web application. I've never
I am trying to use the PLDatabase framework in my iPhone app. I've added
I'm trying to use a C++ library (CLucene) from my Cocoa Touch iPhone application
I am trying to use some external Framework in my iPhone app. I did

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.