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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:29:04+00:00 2026-05-17T00:29:04+00:00

Well im trying to encrypt an string in objective c extending NSData with this

  • 0

Well im trying to encrypt an string in objective c extending NSData with this method:


 @implementation NSData (AES128)

  • (NSData *)AES128Encrypt { char keyPtr[kCCKeySizeAES128] = {'\xe1','\xaa','\x9c','\x61','\x46','\x74','\x44','\x56','\xf0','\xe5','\x47','\x46','\x86','\xdc','\x95','\x77'};

    NSUInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyPtr,kCCKeySizeAES128,NULL /* initialization vector (optional) /,[self bytes], dataLength, / input /buffer, bufferSize, / output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; }

    free(buffer); //free the buffer; return nil; }

  • (NSData *)AES128Decrypt { char keyPtr[kCCKeySizeAES128] = {'\xe1','\xaa','\x9c','\x61','\x46','\x74','\x44','\x56','\xf0','\xe5','\x47','\x46','\x86','\xdc','\x95','\x77'};

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or //equal to the input size plus the size of one block. //That's why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize);

    size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus=CCCrypt(kCCDecrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyPtr, kCCKeySizeAES128,NULL /* initialization vector (optional) /,[self bytes], dataLength, / input /buffer, bufferSize, / output */&numBytesDecrypted);

    if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; }

    free(buffer); //free the buffer; return nil; }

@end

then i call it here:


NSString *strData = @"My string";

NSData *objNSData = [NSData dataWithData:[strData dataUsingEncoding: NSUTF8StringEncoding]];

NSLog(@"encrypted: %@",[objNSData description]);

If I just use it in objective c, it works fine.
But when i try to send it to a java server it doesn't work.

My cipher data seems like this:

86fcf0fa9e3dff93dc8918ffd02ee203 12de0bf8c8ba300456293c4240296c0d

and if I try to cipher it in java using also AES with the same key, i get this:

86fcf0fa9e3dff93dc8918ffd02ee203 8388f173da143c6aeeb90e554259c83c

its weird because the first half its the same.

Someone knows why this can be happening?
thanks.

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

    I’ve never done any Objective-C programming, but I’m almost positive that you’re using AES in different modes in your code. You need to make sure these are consistent. The default is probably Cipher Block Chaining (CBC) mode. Make sure you set this option in your Java code.

    By the way, CBC mode should have a randomized Initialization Vector (IV) rather than NULL (which I assume uses all zeros). This too would need to be consistent across both.

    I’m obliged to give standard disclaimer with cryptography that it’s usually much safer to use a higher level protocol that handles this stuff for you like SSL/TLS for data in transit and something like Keyczar for data at rest. Getting crypto right is really hard and a tiny error (like picking a bad mode) can totally destroy the security of the system.

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

Sidebar

Related Questions

Hi guys thanks for pay attention to this. Well im trying to replace every
Well, I was trying to fix this program, and I keep getting the errors
I am trying to encrypt/decrypt a string using eith Rijndael or Aes and the
Well im trying to write my own custom control extender but i can't get
well iam trying to get version of IE used by user by using $.browser.version
Well,I am trying to do something similar what I did in Adobe Flex.Consuming a
Well I'm trying to set a variable to use in a thread, it works
Well I am trying to learn the java MVC pattern, but I can't understand
Alright well I am trying to create a dictionary from a text file so
hey well I'm trying to change the url structure using the htaccess file and

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.