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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:07:24+00:00 2026-05-28T01:07:24+00:00

I am trying to create a 16 byte and later 32 byte initialization vector

  • 0

I am trying to create a 16 byte and later 32 byte initialization vector in objective-c (Mac OS). I took some code on how to create random bytes and modified it to 16 bytes, but I have some difficulty with this. The NSData dumps the hex, but an NSString dump gives nil, and a cstring NSLog gives the wrong number of characters (not reproduced the same in the dump here).

Here is my terminal output:

2012-01-07 14:29:07.705 Test3Test[4633:80f] iv hex <48ea262d efd8f5f5 f8021126 fd74c9fd>
2012-01-07 14:29:07.710 Test3Test[4633:80f] IV string: (null)
2012-01-07 14:29:07.711 Test3Test[4633:80f] IV char string t^Q¶�^��^A

Here is the main program:

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    //NSString *iv_string = [NSString stringWithCString:iv encoding:NSUTF8StringEncoding];
    testclass *obj = [testclass alloc];
    NSData *iv_data = [obj createRandomNSData];
    //[iv_string dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"iv hex %@",iv_data);

    //NSString *iv_string = [[NSString alloc] initWithBytes:[iv_data bytes] length:16 encoding:NSUTF8StringE$
    NSString *iv_string = [[NSString alloc] initWithData:iv_data encoding:NSUTF8StringEncoding];
    NSLog(@"IV string: %@",iv_string);
    NSLog(@"IV char string %.*s",[iv_data bytes]);

    return 0;
]

(I left in the above some commented code that I tried and did not work also).

Below is my random number generater, taken from a stack overflow example:

@implementation testclass
-(NSData*)createRandomNSData
{
    int twentyMb           = 16;
    NSMutableData* theData = [NSMutableData dataWithCapacity:twentyMb];
    for( unsigned int i = 0 ; i < twentyMb/4 ; ++i )
    {
            u_int32_t randomBits = arc4random();
            [theData appendBytes:(void*)&randomBits length:4];
    }
    NSData *data = [NSData dataWithData:theData];
    [theData dealloc];
    return data;
}
@end

I am really quite clueless as to what could be the problem here. If I have data as bytes, it should convert to a string or not necessarily? I have looked over the relevant examples here on stackoverflow, but none of them have worked in this situation.

Thanks,
Elijah

  • 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-28T01:07:25+00:00Added an answer on May 28, 2026 at 1:07 am

    An arbitrary byte sequence may not be legal UTF8 encoding. As @Joachim Isaksson notes, there is seldom reason to convert to strings this way. If you need to store random data as a string, you should use an encoding scheme like Base64, serialize the NSData to a plist, or similar approach. You cannot simply use a cstring either, since NULL is legal inside of a random byte sequence, but is not legal inside of a cstring.

    You do not need to build your own random byte creator on Mac or iOS. There’s one built-in called SecRandomCopyBytes(). For example (from Properly encrypting with AES with CommonCrypto):

    + (NSData *)randomDataOfLength:(size_t)length {
      NSMutableData *data = [NSMutableData dataWithLength:length];
    
      int result = SecRandomCopyBytes(kSecRandomDefault, 
                                      length,
                                      data.mutableBytes);
      NSAssert(result == 0, @"Unable to generate random bytes: %d",
               errno);
    
      return data;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a byte[] given some unknown amount of bytes. Here's an
I am trying to create a UIImage from a byte array that is actually
I am trying to dynamically create a Bitmap Image using byte array using following
I'm trying to figure out how to create personalized urls for double-byte languages. For
Trying to create a user account in a test. But getting a Object reference
When I am trying to create image like Image<Gray, Byte> testImage = new Image<Gray,
I'm currently trying to create a C source code which properly handles I/O whatever
Im trying to create a spinning square inside of xcode using opengl but instead
I am trying to create an image from a byte array. The byte array
I am trying to create a code coverage tool using Delphi 2007. My general

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.