gpt4 book ai didi

iOS 生成 RSA 非随 secret 钥对?

转载 作者:行者123 更新时间:2023-11-29 10:58:04 30 4
gpt4 key购买 nike

我想在每次应用相同的种子时生成相同的非对称 key 对。

我已经使用 iOS RSA 加密练习来生成 RSA 非对称 key 对。我也每次都使用相同的种子。 (公共(public)和私有(private)标签)但是,我每次生成时都会收到不同的 key 。

- (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];
}

最佳答案

您设置的“公共(public)和私有(private)标签”只是标识符,如果您将 key 对存储在 key 链中,您稍后可以使用 SecItemCopyMatching 搜索这些标识符。

遗憾的是,您无法使用 SecKeyGeneratePairSecKeyGeneratePairAsync 为非对称 key 对设置“种子”值。您将始终获得“随机生成”的 key 对。

如果您必须这样做,您将不得不查看提供该功能的其他库。

关于iOS 生成 RSA 非随 secret 钥对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17322033/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com