如何安全地存储公钥/私钥

时间:2012-09-28 16:59:34

标签: security xamarin.ios storage

希望在iOS设备上安全地存储公钥/私钥信息。我知道我想将它存储在KeyChain中但是我并不是100%确定我需要在SecRecord填充什么类型的属性。我打算做点什么:

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

哪个会存储私钥,然后按照公钥的类似方法将Account属性替换为用户唯一的值,例如用户名。但是,不确定这是否是使用它的正确方法。

有没有人有一个很好的例子来说明如何专门为密钥做这个?

1 个答案:

答案 0 :(得分:4)

决定采用以下方法:

// 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)
});

这意味着每个公钥都映射到单个用户,每个私钥都映射到一个公钥,这允许我存储多个用户密钥(而不是仅存储当前登录的用户)。

似乎工作正常,但是,仍然不是100%确定这是做这种事情的正确方法,所以一些澄清会很好。