简体   繁体   English

在我的代码中写入RSA私钥是否安全?

[英]Is it safe to write RSA private key in my code

I have an important file saving in my app's document directory. 我的应用程序文档目录中保存了重要的文件。 I don't allow other to view its content so I encrypt the file with auto-generated AES key and encrypt this AES key with RSA public key, then save the encrypted AES key to NSUserDefaults. 我不允许其他人查看其内容,因此我使用自动生成的AES密钥对文件进行加密,并使用RSA公共密钥对此AES密钥进行加密,然后将加密的AES密钥保存到NSUserDefaults。

When using the important file, I will fetch the encrypted AES key and decrypt the AES key with RSA public key, then decrypt the file. 使用重要文件时,我将获取加密的AES密钥,并使用RSA公共密钥解密AES密钥,然后解密该文件。

But I don't know where to store the RSA private key, is it safe to write it in my code like NSString *rsaPrivateKey = @"%^^&*(())"; 但是我不知道将RSA私钥存储在哪里,是否可以安全地将其写入我的代码中,例如NSString *rsaPrivateKey = @"%^^&*(())"; , if not, is there a safer solution ? 如果没有,是否有更安全的解决方案?

I know how to generate RSA public and private key using openssl ( This link helps me ) 我知道如何使用openssl生成RSA公钥和私钥( 此链接对我有帮助

Edit: 编辑:

The important file is only open to the user himself, you can consider it as a photo with some sensitive content and the photo is saved by the user. 重要文件仅向用户本人开放,您可以将其视为包含一些敏感内容的照片,并且该照片由用户保存。

In principle you are trying to solve the DRM problem. 原则上,您正在尝试解决DRM问题。 As long as you cannot trust the runtime system, there is no complete solution. 只要您不信任运行时系统,就没有完整的解决方案。 Your best bet is to store keys in system provided containers and hope those are well protected (ie the iOS key chain as borrrden already mentioned). 最好的选择是将密钥存储在系统提供的容器中,并希望它们受到良好的保护(即borrrden已经提到的iOS密钥链)。

If that fails you can encrypt your RSA key with an AES key generated from some static information within the device. 如果失败,则可以使用从设备内的某些静态信息生成的AES密钥来加密RSA密钥。 This should be thought of as obfuscation rather than encryption as the key is not really secret. 应该将其视为混淆而不是加密,因为密钥并不是真正的秘密。 You should probably try and use a standard container format such as PKCS#8 to encrypt the keys. 您可能应该尝试并使用诸如PKCS#8之类的标准容器格式来加密密钥。

It is possible to generate RSA keys from a static set of data (using a PRNG to feed this data in the RSA key pair generator) but those kind of schemes are not standardized, very brittle and take an unspecified amount of CPU time, so I would strongly recommend not to take that route. 可以从一组静态数据中生成RSA密钥(使用PRNG在RSA密钥对生成器中提供此数据),但是这类方案不是标准化的,非常脆弱,并且占用了未指定的CPU时间,因此我强烈建议您不要走那条路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM