NSMutableData to NSString
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:
Is a 256-bit key for decryption safe enough?
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
|
show 5 more comments
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:
Is a 256-bit key for decryption safe enough?
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
Which Cryptographic algorithm in which mode you are using? The code you show doesn't contain any code about crypto? only SecRandomCopyBytes that isGenerates 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
RegloballyUniqueString
, 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 intoSecKeyGeneratePair
function fromCommonCrypto
native framework, or find a good library for that.
– mag_zbc
Nov 28 '18 at 16:29
|
show 5 more comments
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:
Is a 256-bit key for decryption safe enough?
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
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:
Is a 256-bit key for decryption safe enough?
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
ios objective-c cryptography nsstring nsmutabledata
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 isGenerates 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
RegloballyUniqueString
, 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 intoSecKeyGeneratePair
function fromCommonCrypto
native framework, or find a good library for that.
– mag_zbc
Nov 28 '18 at 16:29
|
show 5 more comments
Which Cryptographic algorithm in which mode you are using? The code you show doesn't contain any code about crypto? only SecRandomCopyBytes that isGenerates 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
RegloballyUniqueString
, 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 intoSecKeyGeneratePair
function fromCommonCrypto
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
|
show 5 more comments
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 28 '18 at 17:44
answered Nov 28 '18 at 15:38
RobRob
305k51576743
305k51576743
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 fromCommonCrypto
native framework, or find a good library for that.– mag_zbc
Nov 28 '18 at 16:29