获取生成的RSA私钥和公钥的字节数

时间:2014-05-25 17:04:10

标签: ios objective-c encryption cryptography rsa

我在iphone上生成RSA密钥对,但是如何将生成的私钥和公钥存储在文件中?我需要使用AES加密私钥并将其传输到我的服务器,但我找不到任何方法来获取密钥的字节。

我生成密钥对的代码:

-(void)generateKeyPair {
    OSStatus sanityCheck = noErr;
    SecKeyRef localPublicKey = NULL;
    SecKeyRef localPrivateKey = NULL;

    self.publicTag = [@"com.crap.pub" dataUsingEncoding:NSUTF8StringEncoding];
    self.privateTag = [@"com.crap.pri" dataUsingEncoding:NSUTF8StringEncoding];

    // Container dictionaries.
    NSMutableDictionary * privateKeyAttr = [NSMutableDictionary dictionaryWithCapacity:0];
    NSMutableDictionary * publicKeyAttr = [NSMutableDictionary dictionaryWithCapacity:0];
    NSMutableDictionary * keyPairAttr = [NSMutableDictionary dictionaryWithCapacity:0];

    // Set top level dictionary for the keypair.
    [keyPairAttr setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
    [keyPairAttr setObject:[NSNumber numberWithUnsignedInteger:2048] forKey:(__bridge id)kSecAttrKeySizeInBits];

    // Set the private key dictionary.
    [privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
    [privateKeyAttr setObject:self.privateTag forKey:(__bridge id)kSecAttrApplicationTag];
    // See SecKey.h to set other flag values.

    // Set the public key dictionary.
    [publicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
    [publicKeyAttr setObject:self.publicTag forKey:(__bridge id)kSecAttrApplicationTag];
    // See SecKey.h to set other flag values.

    // Set attributes to top level dictionary.
    [keyPairAttr setObject:privateKeyAttr forKey:(__bridge id)kSecPrivateKeyAttrs];
    [keyPairAttr setObject:publicKeyAttr forKey:(__bridge id)kSecPublicKeyAttrs];

    // SecKeyGeneratePair returns the SecKeyRefs just for educational purposes.
    sanityCheck = SecKeyGeneratePair((__bridge CFDictionaryRef)keyPairAttr, &localPublicKey, &localPrivateKey);
}

0 个答案:

没有答案