将 NSData 转换为字符串?

发布于 2024-11-16 09:26:46 字数 655 浏览 1 评论 0原文

我将 openssl 私钥 EVP_PKEY 存储为 nsdata。为此,我使用下面的代码序列化为字节流

unsigned char *buf, *p;
int len;
len = i2d_PrivateKey(pkey, NULL);
buf = OPENSSL_malloc(len); 
p = buf;
i2d_PrivateKey(pkey, &p);

,其中 pkey 的类型为 EVP_PKEY。 然后我使用下面给出的行将缓冲区“p”中的字节存储为 NSData

NSData *keydata = [NSData dataWithBytes:P length:len];

现在我使用下面给出的代码将其转换为 NSString 但当我将其打印到控制台时它给出了一些其他字符。

NSString *content =[ NSString stringWithCString:[keydata bytes] encoding:NSUTF8StringEncoding];

有人可以帮忙吗?

基本上我想存储 EVP_PKEY 进入sqlite数据库

我走在正确的道路上吗? 谢谢。

I am storing a openssl private Key EVP_PKEY as nsdata. For this I am serializing into a byte stream using the code below

unsigned char *buf, *p;
int len;
len = i2d_PrivateKey(pkey, NULL);
buf = OPENSSL_malloc(len); 
p = buf;
i2d_PrivateKey(pkey, &p);

where pkey is of type EVP_PKEY.
Then I am storing the bytes from buffer 'p' as an NSData using the line given below

NSData *keydata = [NSData dataWithBytes:P length:len];

Now I am converting it to a NSString using the code given below but when i print it into console its giving some other characters.

NSString *content =[ NSString stringWithCString:[keydata bytes] encoding:NSUTF8StringEncoding];

Could someone help?

Basically I want to store the EVP_PKEY
into a sqlite database

am I on the right track?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风筝在阴天搁浅。 2024-11-23 09:26:47

我相信你的“P”作为 dataWithBytes 参数

NSData *keydata = [NSData dataWithBytes:P length:len];

应该是“buf”,

NSData *keydata = [NSData dataWithBytes:buf length:len];

因为 i2d_PrivateKey 将指向输出缓冲区 p 的指针放在缓冲区的末尾并等待进一步的输入,并且 buf 仍然指向缓冲区的开头。

以下代码适用于我,其中 pkey 是指向 EVP_PKEY 的指针:

unsigned char *buf, *pp;
int len = i2d_PrivateKey(pkey, NULL);
buf = OPENSSL_malloc(len); 
pp = buf;
i2d_PrivateKey(pkey, &pp);

NSData* pkeyData = [NSData dataWithBytes:(const void *)buf length:len];
DLog(@"Private key in hex (%d): %@", len, pkeyData);

您可以使用在线转换器将二进制数据转换为 Base 64 (http://tomeko.net/online_tools/hex_to_base64.php?lang=en) 并将其与私钥进行比较使用以下命令并检查 mypkey.pem 的输出后在您的证书文件中:

openssl pkcs12 -in myCert.p12 -nocerts -nodes -out mypkey.pem

我引用了您的问题和 这个 EVP 功能网站作为我的答案。

I believe your "P" as the dataWithBytes param

NSData *keydata = [NSData dataWithBytes:P length:len];

should be "buf"

NSData *keydata = [NSData dataWithBytes:buf length:len];

since i2d_PrivateKey puts the pointer to the output buffer p at the end of the buffer and waiting for further input, and buf is still pointing to the beginning of your buffer.

The following code works for me where pkey is a pointer to an EVP_PKEY:

unsigned char *buf, *pp;
int len = i2d_PrivateKey(pkey, NULL);
buf = OPENSSL_malloc(len); 
pp = buf;
i2d_PrivateKey(pkey, &pp);

NSData* pkeyData = [NSData dataWithBytes:(const void *)buf length:len];
DLog(@"Private key in hex (%d): %@", len, pkeyData);

You can use an online converter to convert your binary data into base 64 (http://tomeko.net/online_tools/hex_to_base64.php?lang=en) and compare it to the private key in your cert file after using the following command and checking the output of mypkey.pem:

openssl pkcs12 -in myCert.p12 -nocerts -nodes -out mypkey.pem

I referenced your question and this EVP function site for my answer.

杀お生予夺 2024-11-23 09:26:47

斯威夫特3:

String(data: data, encoding: .utf8)

Swift 3:

String(data: data, encoding: .utf8)
依 靠 2024-11-23 09:26:47

将任意 NSData 转换为 NSString 的一个简单方法是对其进行 base64 编码。

NSString *base64EncodedKey = [keydata base64EncodedStringWithOptions: NSDataBase64Encoding64CharacterLineLength];

然后,您可以将其存储到数据库中以供以后重用。只需将其解码回 NSData 即可。

A simple way to convert arbitrary NSData to NSString is to base64 encode it.

NSString *base64EncodedKey = [keydata base64EncodedStringWithOptions: NSDataBase64Encoding64CharacterLineLength];

You can then store it into your database for reuse later. Just decode it back to NSData.

内心荒芜 2024-11-23 09:26:46

Objective-C

您可以使用(请参阅NSString 类参考< /a>)

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding

示例:

NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];

备注:请注意 NSData 值对于指定的编码(上例中的 UTF-8)必须有效,否则 nil< /code> 将返回:

如果由于某种原因初始化失败(例如,如果数据不存在),则返回 nil表示用于编码的有效数据)。

之前的 Swift 3.0

String(data: yourData, encoding: NSUTF8StringEncoding)

Swift 3.0 以后的版本

String(data: yourData, encoding: .utf8)

请参阅 String#init(data:encoding:) 参考

Objective-C

You can use (see NSString Class Reference)

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding

Example:

NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];

Remark: Please notice the NSData value must be valid for the encoding specified (UTF-8 in the example above), otherwise nil will be returned:

Returns nil if the initialization fails for some reason (for example if data does not represent valid data for encoding).

Prior Swift 3.0

String(data: yourData, encoding: NSUTF8StringEncoding)

Swift 3.0 Onwards

String(data: yourData, encoding: .utf8)

See String#init(data:encoding:) Reference

久夏青 2024-11-23 09:26:46

之前的 Swift 3.0:

String(data: yourData, encoding: NSUTF8StringEncoding)

对于 Swift 4.0:

String(data: yourData, encoding: .utf8)

Prior Swift 3.0 :

String(data: yourData, encoding: NSUTF8StringEncoding)

For Swift 4.0:

String(data: yourData, encoding: .utf8)
云胡 2024-11-23 09:26:46

斯威夫特 5

String(data: data!, encoding: String.Encoding.utf8)

Swift 5:

String(data: data!, encoding: String.Encoding.utf8)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文