NSMutableData to NSString












0















Hi everyone I'm working with symmetric encryption in my app to encrypt messages that users send to each other. Now I'm trying to create a fairly secure key to decrypt this data (decryption key) using an NSMutableData. Currently I have two questions:




  1. Is a 256-bit key for decryption safe enough?


  2. I have some problems with NSString. When I want to retrieve the string value of the NSMutableData my NSLog always returns me a null value



Where am I doing wrong?



NSMutableData *masterKey = [NSMutableData dataWithLength:32];
int result = SecRandomCopyBytes(kSecRandomDefault, 32, masterKey.mutableBytes);
if (result != noErr) {
NSLog(FAILED_MASTERKEY);
return;
}
NSLog(@"MASTER %@",[[NSString alloc] initWithData:masterKey encoding:NSUTF32StringEncoding]);




2018-11-28 16:06:31.803868+0100 [41860:9341804] MASTER (null)









share|improve this question

























  • Which Cryptographic algorithm in which mode you are using? The code you show doesn't contain any code about crypto? only SecRandomCopyBytes that is Generates an array of cryptographically secure random bytes. Also, you have two question that are not really realted.

    – kelalaka
    Nov 28 '18 at 15:27








  • 2





    I'd suggest you split this into two separate questions. I've answered the "how do I create a string representation of an arbitrary binary value", as suggested by this question's title, below. But the encryption questions seem like a complete different issue.

    – Rob
    Nov 28 '18 at 15:44








  • 1





    Re globallyUniqueString, that would appear to ensure uniqueness, but I see nothing to suggest cryptographically robust randomness. I'd stick with cryptographic functions.

    – Rob
    Nov 28 '18 at 15:46






  • 1





    When you post your separate crypto question, I'd suggest you step back and give a broader context. This whole notion of generating a single encryption key seems suspect, introducing a problem of how do you share this between the various devices. I would have expected that you'd want to generate key pairs on each device and exchange public keys or something like that.

    – Rob
    Nov 28 '18 at 15:54






  • 1





    You cannot just take some random data and say that's a EC encryption key, that's not how it works. You need to look into SecKeyGeneratePair function from CommonCrypto native framework, or find a good library for that.

    – mag_zbc
    Nov 28 '18 at 16:29


















0















Hi everyone I'm working with symmetric encryption in my app to encrypt messages that users send to each other. Now I'm trying to create a fairly secure key to decrypt this data (decryption key) using an NSMutableData. Currently I have two questions:




  1. Is a 256-bit key for decryption safe enough?


  2. I have some problems with NSString. When I want to retrieve the string value of the NSMutableData my NSLog always returns me a null value



Where am I doing wrong?



NSMutableData *masterKey = [NSMutableData dataWithLength:32];
int result = SecRandomCopyBytes(kSecRandomDefault, 32, masterKey.mutableBytes);
if (result != noErr) {
NSLog(FAILED_MASTERKEY);
return;
}
NSLog(@"MASTER %@",[[NSString alloc] initWithData:masterKey encoding:NSUTF32StringEncoding]);




2018-11-28 16:06:31.803868+0100 [41860:9341804] MASTER (null)









share|improve this question

























  • Which Cryptographic algorithm in which mode you are using? The code you show doesn't contain any code about crypto? only SecRandomCopyBytes that is Generates an array of cryptographically secure random bytes. Also, you have two question that are not really realted.

    – kelalaka
    Nov 28 '18 at 15:27








  • 2





    I'd suggest you split this into two separate questions. I've answered the "how do I create a string representation of an arbitrary binary value", as suggested by this question's title, below. But the encryption questions seem like a complete different issue.

    – Rob
    Nov 28 '18 at 15:44








  • 1





    Re globallyUniqueString, that would appear to ensure uniqueness, but I see nothing to suggest cryptographically robust randomness. I'd stick with cryptographic functions.

    – Rob
    Nov 28 '18 at 15:46






  • 1





    When you post your separate crypto question, I'd suggest you step back and give a broader context. This whole notion of generating a single encryption key seems suspect, introducing a problem of how do you share this between the various devices. I would have expected that you'd want to generate key pairs on each device and exchange public keys or something like that.

    – Rob
    Nov 28 '18 at 15:54






  • 1





    You cannot just take some random data and say that's a EC encryption key, that's not how it works. You need to look into SecKeyGeneratePair function from CommonCrypto native framework, or find a good library for that.

    – mag_zbc
    Nov 28 '18 at 16:29
















0












0








0








Hi everyone I'm working with symmetric encryption in my app to encrypt messages that users send to each other. Now I'm trying to create a fairly secure key to decrypt this data (decryption key) using an NSMutableData. Currently I have two questions:




  1. Is a 256-bit key for decryption safe enough?


  2. I have some problems with NSString. When I want to retrieve the string value of the NSMutableData my NSLog always returns me a null value



Where am I doing wrong?



NSMutableData *masterKey = [NSMutableData dataWithLength:32];
int result = SecRandomCopyBytes(kSecRandomDefault, 32, masterKey.mutableBytes);
if (result != noErr) {
NSLog(FAILED_MASTERKEY);
return;
}
NSLog(@"MASTER %@",[[NSString alloc] initWithData:masterKey encoding:NSUTF32StringEncoding]);




2018-11-28 16:06:31.803868+0100 [41860:9341804] MASTER (null)









share|improve this question
















Hi everyone I'm working with symmetric encryption in my app to encrypt messages that users send to each other. Now I'm trying to create a fairly secure key to decrypt this data (decryption key) using an NSMutableData. Currently I have two questions:




  1. Is a 256-bit key for decryption safe enough?


  2. I have some problems with NSString. When I want to retrieve the string value of the NSMutableData my NSLog always returns me a null value



Where am I doing wrong?



NSMutableData *masterKey = [NSMutableData dataWithLength:32];
int result = SecRandomCopyBytes(kSecRandomDefault, 32, masterKey.mutableBytes);
if (result != noErr) {
NSLog(FAILED_MASTERKEY);
return;
}
NSLog(@"MASTER %@",[[NSString alloc] initWithData:masterKey encoding:NSUTF32StringEncoding]);




2018-11-28 16:06:31.803868+0100 [41860:9341804] MASTER (null)






ios objective-c cryptography nsstring nsmutabledata






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 15:27







kAiN

















asked Nov 28 '18 at 15:18









kAiNkAiN

9151539




9151539













  • Which Cryptographic algorithm in which mode you are using? The code you show doesn't contain any code about crypto? only SecRandomCopyBytes that is Generates an array of cryptographically secure random bytes. Also, you have two question that are not really realted.

    – kelalaka
    Nov 28 '18 at 15:27








  • 2





    I'd suggest you split this into two separate questions. I've answered the "how do I create a string representation of an arbitrary binary value", as suggested by this question's title, below. But the encryption questions seem like a complete different issue.

    – Rob
    Nov 28 '18 at 15:44








  • 1





    Re globallyUniqueString, that would appear to ensure uniqueness, but I see nothing to suggest cryptographically robust randomness. I'd stick with cryptographic functions.

    – Rob
    Nov 28 '18 at 15:46






  • 1





    When you post your separate crypto question, I'd suggest you step back and give a broader context. This whole notion of generating a single encryption key seems suspect, introducing a problem of how do you share this between the various devices. I would have expected that you'd want to generate key pairs on each device and exchange public keys or something like that.

    – Rob
    Nov 28 '18 at 15:54






  • 1





    You cannot just take some random data and say that's a EC encryption key, that's not how it works. You need to look into SecKeyGeneratePair function from CommonCrypto native framework, or find a good library for that.

    – mag_zbc
    Nov 28 '18 at 16:29





















  • Which Cryptographic algorithm in which mode you are using? The code you show doesn't contain any code about crypto? only SecRandomCopyBytes that is Generates an array of cryptographically secure random bytes. Also, you have two question that are not really realted.

    – kelalaka
    Nov 28 '18 at 15:27








  • 2





    I'd suggest you split this into two separate questions. I've answered the "how do I create a string representation of an arbitrary binary value", as suggested by this question's title, below. But the encryption questions seem like a complete different issue.

    – Rob
    Nov 28 '18 at 15:44








  • 1





    Re globallyUniqueString, that would appear to ensure uniqueness, but I see nothing to suggest cryptographically robust randomness. I'd stick with cryptographic functions.

    – Rob
    Nov 28 '18 at 15:46






  • 1





    When you post your separate crypto question, I'd suggest you step back and give a broader context. This whole notion of generating a single encryption key seems suspect, introducing a problem of how do you share this between the various devices. I would have expected that you'd want to generate key pairs on each device and exchange public keys or something like that.

    – Rob
    Nov 28 '18 at 15:54






  • 1





    You cannot just take some random data and say that's a EC encryption key, that's not how it works. You need to look into SecKeyGeneratePair function from CommonCrypto native framework, or find a good library for that.

    – mag_zbc
    Nov 28 '18 at 16:29



















Which Cryptographic algorithm in which mode you are using? The code you show doesn't contain any code about crypto? only SecRandomCopyBytes that is Generates an array of cryptographically secure random bytes. Also, you have two question that are not really realted.

– kelalaka
Nov 28 '18 at 15:27







Which Cryptographic algorithm in which mode you are using? The code you show doesn't contain any code about crypto? only SecRandomCopyBytes that is Generates an array of cryptographically secure random bytes. Also, you have two question that are not really realted.

– kelalaka
Nov 28 '18 at 15:27






2




2





I'd suggest you split this into two separate questions. I've answered the "how do I create a string representation of an arbitrary binary value", as suggested by this question's title, below. But the encryption questions seem like a complete different issue.

– Rob
Nov 28 '18 at 15:44







I'd suggest you split this into two separate questions. I've answered the "how do I create a string representation of an arbitrary binary value", as suggested by this question's title, below. But the encryption questions seem like a complete different issue.

– Rob
Nov 28 '18 at 15:44






1




1





Re globallyUniqueString, that would appear to ensure uniqueness, but I see nothing to suggest cryptographically robust randomness. I'd stick with cryptographic functions.

– Rob
Nov 28 '18 at 15:46





Re globallyUniqueString, that would appear to ensure uniqueness, but I see nothing to suggest cryptographically robust randomness. I'd stick with cryptographic functions.

– Rob
Nov 28 '18 at 15:46




1




1





When you post your separate crypto question, I'd suggest you step back and give a broader context. This whole notion of generating a single encryption key seems suspect, introducing a problem of how do you share this between the various devices. I would have expected that you'd want to generate key pairs on each device and exchange public keys or something like that.

– Rob
Nov 28 '18 at 15:54





When you post your separate crypto question, I'd suggest you step back and give a broader context. This whole notion of generating a single encryption key seems suspect, introducing a problem of how do you share this between the various devices. I would have expected that you'd want to generate key pairs on each device and exchange public keys or something like that.

– Rob
Nov 28 '18 at 15:54




1




1





You cannot just take some random data and say that's a EC encryption key, that's not how it works. You need to look into SecKeyGeneratePair function from CommonCrypto native framework, or find a good library for that.

– mag_zbc
Nov 28 '18 at 16:29







You cannot just take some random data and say that's a EC encryption key, that's not how it works. You need to look into SecKeyGeneratePair function from CommonCrypto native framework, or find a good library for that.

– mag_zbc
Nov 28 '18 at 16:29














1 Answer
1






active

oldest

votes


















3














You cannot create NSString directly from arbitrary binary data. Alternatives range from displaying hexadecimal representation (e.g. from the description method) or using some other text representation of binary data (e.g. base-64). But you can't just pass random binary data to -[NSString initWithData:encoding:].



NSLog(@"Hex:     %@", [masterKey description]);
NSLog(@"Hex: %@", masterKey); // directly logging the `NSData` will also use its `description`
NSLog(@"Base 64: %@", [masterKey base64EncodedStringWithOptions:0]);


The common technique for exchanging binary data via web service is base-64. But if the intent was merely to log the value so that you could see that, indeed, a value was generated, then just logging its description is simplest.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53522691%2fnsmutabledata-to-nsstring%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You cannot create NSString directly from arbitrary binary data. Alternatives range from displaying hexadecimal representation (e.g. from the description method) or using some other text representation of binary data (e.g. base-64). But you can't just pass random binary data to -[NSString initWithData:encoding:].



    NSLog(@"Hex:     %@", [masterKey description]);
    NSLog(@"Hex: %@", masterKey); // directly logging the `NSData` will also use its `description`
    NSLog(@"Base 64: %@", [masterKey base64EncodedStringWithOptions:0]);


    The common technique for exchanging binary data via web service is base-64. But if the intent was merely to log the value so that you could see that, indeed, a value was generated, then just logging its description is simplest.






    share|improve this answer






























      3














      You cannot create NSString directly from arbitrary binary data. Alternatives range from displaying hexadecimal representation (e.g. from the description method) or using some other text representation of binary data (e.g. base-64). But you can't just pass random binary data to -[NSString initWithData:encoding:].



      NSLog(@"Hex:     %@", [masterKey description]);
      NSLog(@"Hex: %@", masterKey); // directly logging the `NSData` will also use its `description`
      NSLog(@"Base 64: %@", [masterKey base64EncodedStringWithOptions:0]);


      The common technique for exchanging binary data via web service is base-64. But if the intent was merely to log the value so that you could see that, indeed, a value was generated, then just logging its description is simplest.






      share|improve this answer




























        3












        3








        3







        You cannot create NSString directly from arbitrary binary data. Alternatives range from displaying hexadecimal representation (e.g. from the description method) or using some other text representation of binary data (e.g. base-64). But you can't just pass random binary data to -[NSString initWithData:encoding:].



        NSLog(@"Hex:     %@", [masterKey description]);
        NSLog(@"Hex: %@", masterKey); // directly logging the `NSData` will also use its `description`
        NSLog(@"Base 64: %@", [masterKey base64EncodedStringWithOptions:0]);


        The common technique for exchanging binary data via web service is base-64. But if the intent was merely to log the value so that you could see that, indeed, a value was generated, then just logging its description is simplest.






        share|improve this answer















        You cannot create NSString directly from arbitrary binary data. Alternatives range from displaying hexadecimal representation (e.g. from the description method) or using some other text representation of binary data (e.g. base-64). But you can't just pass random binary data to -[NSString initWithData:encoding:].



        NSLog(@"Hex:     %@", [masterKey description]);
        NSLog(@"Hex: %@", masterKey); // directly logging the `NSData` will also use its `description`
        NSLog(@"Base 64: %@", [masterKey base64EncodedStringWithOptions:0]);


        The common technique for exchanging binary data via web service is base-64. But if the intent was merely to log the value so that you could see that, indeed, a value was generated, then just logging its description is simplest.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 28 '18 at 17:44

























        answered Nov 28 '18 at 15:38









        RobRob

        305k51576743




        305k51576743
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53522691%2fnsmutabledata-to-nsstring%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks