1

同じシードを適用するたびに、同じ非対称鍵ペアを生成したい。

iOS RSA暗号演習を使用して、RSA非対称キーペアを生成しました。また、毎回同じシードを適用します。(公開タグと非公開タグ) ただし、生成するたびに異なるキーを受け取ります。

- (void)generateKeyPair:(NSUInteger)keySize {
    OSStatus sanityCheck = noErr;
    publicKeyRef = NULL;
    privateKeyRef = NULL;

    LOGGING_FACILITY1( keySize == 512 || keySize == 1024 || keySize == 2048, @"%d is an invalid and unsupported key size.", keySize );

    // First delete current keys.
    [self deleteAsymmetricKeys];

    // Container dictionaries.
    NSMutableDictionary * privateKeyAttr = [[NSMutableDictionary alloc] init];
    NSMutableDictionary * publicKeyAttr = [[NSMutableDictionary alloc] init];
    NSMutableDictionary * keyPairAttr = [[NSMutableDictionary alloc] init];

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

    // Set the private key dictionary.
    [privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecAttrIsPermanent];
    [privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecAttrCanEncrypt];
    [privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecAttrCanDecrypt];

     [privateKeyAttr setObject:privateTag forKey:(id)kSecAttrApplicationTag];
    // See SecKey.h to set other flag values.

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

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

    // SecKeyGeneratePair returns the SecKeyRefs just for educational purposes.
    sanityCheck = SecKeyGeneratePair((CFDictionaryRef)keyPairAttr, &publicKeyRef, &privateKeyRef);
    LOGGING_FACILITY( sanityCheck == noErr && publicKeyRef != NULL && privateKeyRef != NULL, @"Something really bad went wrong with generating the key pair." );


    NSLog(@"getPublicKeyBits: %@", [self getPublicKeyBits]);

    NSLog(@"getPublicKeyExp: %@", [self getPublicKeyExp]);
    NSLog(@"getPublicKeyMod: %@", [self getPublicKeyMod]);


   // NSLog(@"keyPairAttr: %@" , keyPairAttr);
    [privateKeyAttr release];
    [publicKeyAttr release];
    [keyPairAttr release];
}
4

1 に答える 1

2

SecItemCopyMatching設定している「パブリック タグとプライベート タグ」は、キー ペアをキー チェーンに保存する場合に後で使用して検索できる単なる識別子です。

SecKeyGeneratePair残念ながら、またはを使用して非対称鍵ペアの「シード」値を設定することはできませんSecKeyGeneratePairAsync。常に「ランダムに生成された」鍵ペアを取得します。

これを行う必要がある場合は、その機能を提供する他のライブラリを調べる必要があります。

于 2013-06-26T15:49:35.323 に答える