ios - 将 uint8_t 转换为 NSData

标签 ios rsa nsdata bytebuffer

我已经根据 Apple Developer 网站上提供的示例在 iOS 应用程序中实现了 publickey privatekey RSA 加密。

如果我加密并返回 uint8_t cipherBuffer,然后从 uint8_t cipherBuffer 解密,它会完美运行。但是,我需要将加密数据作为 NSData 存储到 .xcdata 模型中。

我遇到的问题是将 uint8_t cipherBuffer 可靠地转换为 NSData 和/或将 NSData 转换回 uint8_t 什么时候解密。解密的数据似乎被截断了。

这就是我将 uint8_t 加密缓冲区转换为 NSData 的方式:

return [NSData dataWithBytesNoCopy:cipherBuffer length:BUFFER_SIZE];

这就是我在解密时将加密的 NSData 转换回 uint8_t 缓冲区的方式:

uint8_t *cipherBuffer = (uint8_t*)[cipherText bytes];

最佳答案

感谢 jgh 和 Jody;

我将加密方法更改为“malloc”缓冲区,并尝试了几种方法将字节写入 NSData,结果是:

return [NSData dataWithBytes:(const void *)cipherBuffer length:CIPHER_BUFFER_SIZE];

最终解决问题的是改变我在解密方法中创建 uint8_t 的方式:

const uint8_t *cipherBuffer = (const uint8_t*)[data bytes];

关于ios - 将 uint8_t 转换为 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11820217/

相关文章:

ios - Xcode 7.2 : Impossible to run unit tests on iPhone 6s

ios - @IBAction 在递归调用时崩溃

ios - Swift 4 中的反向地理编码

encryption - 使用 RSA-1024 签名 SHA-256 摘要 : what is the size?

file - NSData(文件内容 : "someexistingfile") returns nil in Playground

ios - 我可以同时运行 Xcode 6.3.2 和 Xcode 6.2 吗?

encryption - RSA如何保持认证和不可否认性

c++ - RSA 签名大小不匹配

iphone - 将来自服务器的图像显示到 UIImageView

ios - 直接使用 SSZipArchive 压缩 NSData 不工作