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

  • Home
  • SEARCH
  • 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 8210213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:59:56+00:00 2026-06-07T09:59:56+00:00

i am trying to use CCCrypt to encrypt my data , but something wrong

  • 0

i am trying to use CCCrypt to encrypt my data , but something wrong with it, the data length changed after i encrypted, but didn’t happen on the decrypt, here is the following code.

- (NSData *)AES256EncryptWithKeyGenerateIV:(NSString *)key {
    char keyPtr[kCCKeySizeAES128+1];
    bzero(keyPtr, sizeof(keyPtr));

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    char ivPtr[kCCKeySizeAES128+1];

    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5([key UTF8String], strlen([key UTF8String]), result);

    NSString* resultString = [NSString stringWithFormat:
                              @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                          result[0], result[1], result[2], result[3],
                          result[4], result[5], result[6], result[7],
                          result[8], result[9], result[10], result[11],
                          result[12], result[13], result[14], result[15]
                          ];
    resultString = [resultString substringToIndex:16];

    bzero(ivPtr, sizeof(ivPtr));
    [resultString getCString:ivPtr maxLength:sizeof(ivPtr)     encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [self length];

    NSLog(@"Before encrypt dataLength is %d",dataLength);

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

    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,
                                      0,
                                      keyPtr, kCCBlockSizeAES128,
                                      ivPtr,
                                      [self bytes], dataLength,
                                      buffer, bufferSize,
                                      &numBytesDecrypted);

    NSLog(@"After file encrypted dataLength is %d",numBytesDecrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }

    free(buffer);
    return nil;
}

and here is the log i print the data length out:

2012-07-09 18:29:20.787 Before encrypt dataLength is 248
2012-07-09 18:29:20.788 After file encrypted dataLength is 240
2012-07-09 18:29:20.789 Before encrypt dataLength is 20
2012-07-09 18:29:20.790 After file encrypted dataLength is 16
2012-07-09 18:29:20.792 Before encrypt dataLength is 583
2012-07-09 18:29:20.792 After file encrypted dataLength is 576
2012-07-09 18:29:20.795 Before encrypt dataLength is 31120
2012-07-09 18:29:20.796 After file encrypted dataLength is 31120
2012-07-09 18:29:20.797 Before encrypt dataLength is 27551
2012-07-09 18:29:20.798 After file encrypted dataLength is 27536
2012-07-09 18:29:20.800 Before encrypt dataLength is 1266
2012-07-09 18:29:20.800 After file encrypted dataLength is 1264
2012-07-09 18:29:20.802 Before encrypt dataLength is 431315
2012-07-09 18:29:20.809 After file encrypted dataLength is 431312
2012-07-09 18:29:20.815 Before encrypt dataLength is 204
2012-07-09 18:29:20.815 After file encrypted dataLength is 192
2012-07-09 18:29:20.817 Before encrypt dataLength is 1083
2012-07-09 18:29:20.817 After file encrypted dataLength is 1072
2012-07-09 18:29:20.818 Before encrypt dataLength is 440
2012-07-09 18:29:20.819 After file encrypted dataLength is 432

can someone tell me why please?

  • 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-06-07T09:59:58+00:00Added an answer on June 7, 2026 at 9:59 am

    Padding has to be applied when you are using a block cipher, to make the plain text fit into the cipher blocks. AES is a block cipher, and uses a block size of 128 bits, 16 bytes. Try it with explicit padding and CBC mode in the CCOptions, see the Apple documentation of CCCrypt how to do that (although, again, I’ve seen better descriptions of cryptographic methods in my life).

    [edit]
    Note: try kCCOptionPKCS7Padding, CBC is probably the default.

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

Sidebar

Related Questions

trying to use the preferred on method call but the code is not working
I am trying use filehelpers class builder but I am kinda confused on what
I was trying use svn to find a checkin using svn log, but it
I am trying use std::copy to copy from two different iterator. But during course
I'm trying use the Sum method in a lambda expression for a comparison, but
I am trying use Thread but i have some problem (I am beginner at
I'm trying use double type in openCL, but doesn't work anyway, i want use
Trying to use Java's regexp I want to match /app, /app/.* , but not
Trying to use a guid as a resource id in a rest url but
I'm trying use epoch time dates in my series data. The array looks like

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.