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 99123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:19:38+00:00 2026-05-11T00:19:38+00:00

The situation I’m trying to solve: in my Cocoa app, I need to encrypt

  • 0

The situation I’m trying to solve: in my Cocoa app, I need to encrypt a string with a symmetric cipher, POST it to PHP, and have that script decode the data. The process needs to work in reverse for returning an answer (PHP encodes, Cocoa decodes).

I’m missing something because even though I can get both the key and initialization vector (iv) to be the same in both PHP and Cocoa, the decoding never works when one app sends its encoded data to the other. Both work just fine encoding/decoding their own data (verified to make sure there wasn’t some PEBKAC issue at hand). I have a suspicion that there’s a padding issue someplace, I just don’t see it.

My cocoa app encodes using SSCrypto (which is just a handy-dandy wrapper around OpenSSL functions). The cipher is Blowfish, mode is CBC. (forgive the memory leaks, code has been stripped to the bare essentials)

NSData *secretText = [@'secretTextToEncode' dataUsingEncoding:NSUTF8StringEncoding]; NSData *symmetricKey = [@'ThisIsMyKey' dataUsingEncoding:NSUTF8StringEncoding];  unsigned char *input = (unsigned char *)[secretText bytes]; unsigned char *outbuf; int outlen, templen, inlen; inlen = [secretText length];  unsigned char evp_key[EVP_MAX_KEY_LENGTH] = {'\0'}; int cipherMaxIVLength = EVP_MAX_IV_LENGTH; EVP_CIPHER_CTX cCtx; const EVP_CIPHER *cipher = EVP_bf_cbc();  cipherMaxIVLength = EVP_CIPHER_iv_length( cipher ); unsigned char iv[cipherMaxIVLength];  EVP_BytesToKey(cipher, EVP_md5(), NULL, [symmetricKey bytes], [symmetricKey length], 1, evp_key, iv);  NSData *initVector = [NSData dataWithBytes:iv length:cipherMaxIVLength];  EVP_CIPHER_CTX_init(&cCtx);  if (!EVP_EncryptInit_ex(&cCtx, cipher, NULL, evp_key, iv)) {     EVP_CIPHER_CTX_cleanup(&cCtx);     return nil; } int ctx_CipherKeyLength = EVP_CIPHER_CTX_key_length( &cCtx ); EVP_CIPHER_CTX_set_key_length(&cCtx, ctx_CipherKeyLength);  outbuf = (unsigned char *)calloc(inlen + EVP_CIPHER_CTX_block_size(&cCtx), sizeof(unsigned char));  if (!EVP_EncryptUpdate(&cCtx, outbuf, &outlen, input, inlen)){     EVP_CIPHER_CTX_cleanup(&cCtx);     return nil; } if (!EVP_EncryptFinal(&cCtx, outbuf + outlen, &templen)){     EVP_CIPHER_CTX_cleanup(&cCtx);     return nil; } outlen += templen; EVP_CIPHER_CTX_cleanup(&cCtx);  NSData *cipherText = [NSData dataWithBytes:outbuf length:outlen];  NSString *base64String = [cipherText encodeBase64WithNewlines:NO]; NSString *iv = [initVector encodeBase64WithNewlines:NO]; 

base64String and iv are then POSTed to PHP that attempts to decode it:

<?php  import_request_variables( 'p', 'p_' );  if( $p_data != '' && $p_iv != '' ) {     $encodedData = base64_decode( $p_data, true );     $iv = base64_decode( $p_iv, true );      $td = mcrypt_module_open( MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '' );     $keySize = mcrypt_enc_get_key_size( $td );     $key = substr( md5( 'ThisIsMyKey' ), 0, $keySize );      $decodedData = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $encodedData, MCRYPT_MODE_CBC, $iv );     mcrypt_module_close( $td );      echo 'decoded: ' . $decodedData; } ?> 

decodedData is always gibberish.

I’ve tried reversing the process, sending the encoded output from PHP to Cocoa but EVP_DecryptFinal() fails, which is what leads me to believe there’s a NULL padding issue somewhere. I’ve read and re-read the PHP and OpenSSL docs but it’s all blurring together now and I’m out of ideas to try.

  • 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. 2026-05-11T00:19:39+00:00Added an answer on May 11, 2026 at 12:19 am

    I think your problem is that the method of deriving the raw encryption key from the key string is different on the two sides. The php md5() function returns a hexadecimal string, i.e ‘a476c3…’ which you are chopping down to the key size, while EVP_BytesToKey() is a fairly complicated hash routine that return a raw byte string. It might, with the parameters supplied simplify down to a raw MD5 hash, but I can’t really tell. Either way, it’s going to be different from the php hash.

    If you change the php to md5( ‘ThisIsMyKey’, TRUE ), that will give you a raw md5 hash. On the cocoa side of things, SSCrypto’s +getMD5ForData: method should generate the same one for the same string (text encoding issues aside).

    Edit 1: If the php string and Cocoa data print out identically, they’re still different at the byte level. The php string is hex-encoded (i.e consists only of characters 0-9 and a-f) while the cocoa data is the raw bytes (although NSData helpfully prints out a hex-encoded string of its contents when NSLogged). You still need to add the second TRUE parameter to php’s md5() function to get the raw byte string.

    Edit 2: OpenSSL 1.1.0c changed the digest algorithm used in some internal components. Formerly, MD5 was used, and 1.1.0 switched to SHA256. Be careful the change is not affecting you in both EVP_BytesToKey and commands like openssl enc.

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

Sidebar

Ask A Question

Stats

  • Questions 52k
  • Answers 52k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I just had to deal with the same and I'll… May 11, 2026 at 6:41 am
  • added an answer numpy.matrix is an ordinary class defined in numpy/core/defmatrix.py. You can… May 11, 2026 at 6:41 am
  • added an answer The vncserver that's included in Solaris 10 4/08 (Update 5)… May 11, 2026 at 6:41 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.