How to store public/private key securely

安稳与你 提交于 2019-12-10 14:48:08

问题


Looking to store public/private key information securely on an iOS device. I know I want to store this in the KeyChain however I am not 100% sure what sort of attributes I need to populate in the SecRecord. I was going to do something like:

// private key
SecKeyChain.Add(new SecRecord(SecKind.Key)
{
    Accessible = SecAccessible.AlwaysThisDeviceOnly,
    KeySizeInBits = 512,
    KeyClass = SecKeyClass.Private,
    CanSign = true,
    ValueData = privateKeyValue,
    Account = publicKeyValue
});

Which would store the private key, then follow a similar approach for the public key replacing the Account attribute with a value unique to the user e.g. username. However, not sure if this is the right way to use this.

Does anyone have a good examples on how you would do this specifically for keys?


回答1:


Decided to go with the following approach:

// store public key
SecKeyChain.Add(new SecRecord(SecKind.Key)
{
    ApplicationLabel = userName,
    Accessible = SecAccessible.AlwaysThisDeviceOnly,
    KeySizeInBits = 512,
    KeyClass = SecKeyClass.Public,
    ValueData = NSData.FromString(publicKey)
});

// store private key
SecKeyChain.Add(new SecRecord(SecKind.Key)
{
    ApplicationLabel = publicKey,
    Accessible = SecAccessible.AlwaysThisDeviceOnly,
    KeySizeInBits = 512,
    KeyClass = SecKeyClass.Private,
    CanSign = true,
    ValueData = NSData.FromString(secretKey)
});

This means each public key is mapped to an individual user and each private key is mapped to a public key which allows me to store multiple user keys (rather than only storing current logged in users).

Seems to work ok, however, still not 100% sure it is the correct way to do this kind of thing so some clarification would be nice.



来源:https://stackoverflow.com/questions/12644304/how-to-store-public-private-key-securely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!