首页 > 解决方案 > 如何在目标 C 中使用 SHA512 创建 RSA 密钥对

问题描述

我想用 SHA512 创建 RSA keyPair(2048 大小) 我该如何实现它。我收到无效的密钥对。以下是我得到的关键:

Public: MIIBCgKCAQEA2DhFsPubv+aDKb09RTblPPFo6I6ZWVGbgVnsf0HcYU7HJCF6+0V3\r\nUjWZqNJ1zjUFbIOL81UOaDwtVWZdQuWSSyas+43T4ZoGoevML8ctgi/zG8yTWCJM\r\nxz1nHUfvaglebw/qSwTWSaw0VvH89qGtvKiexv63Jzqv+Laq1iB8oBr6oHV0TnVG\r\ncVqeABt9VefeAis5Qo/vETYKpvL6/wRFsfvJM+JxcjANpqyvGeR20PVoYpigrBRq\r\n9wtnxmuAT2NrdRJ/X6truwMCS3ulJJYY1wAy6+KRkbmXOZ54FHfzMKg8DHCv/fGV\r\nnuv0E/NOPN+ pVJ//a7R/8/oKJ5Ct9BXsJwIDAQAB

RSA.m 文件

- (id)init {
    if (self = [super init]) {
        cryptoQueue = [[NSOperationQueue alloc] init];
        kSecAttrKeySizeInBitsLength = k2048;
    }
    return self;
}

- (void)generateRSAKeyPair:(RSACompletionBlock)completion {
    NSInvocationOperation * genOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(generateKeyPairOperation) object:nil];
    [cryptoQueue addOperation:genOp];

    _completion = completion;
}

- (void)generateKeyPairOperation{
    @autoreleasepool {
        // Generate the asymmetric key (public and private)
        [self generateKeyPairRSA];
        [self performSelectorOnMainThread:@selector(generateKeyPairCompleted) withObject:nil waitUntilDone:NO];
    }
}

- (void)generateKeyPairCompleted{
    if (_completion) {
        _completion();
    }
}

- (void)generateKeyPairRSA {
    OSStatus sanityCheck = noErr;
    publicKeyRef = NULL;
    privateKeyRef = NULL;

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

    // 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:kSecAttrKeySizeInBitsLength] forKey:(__bridge id)kSecAttrKeySizeInBits];

    // Set the private key dictionary.
    [privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
    [privateKeyAttr setObject: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: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, &publicKeyRef, &privateKeyRef);
    LOGGING_FACILITY( sanityCheck == noErr && publicKeyRef != NULL && privateKeyRef != NULL, @"Something went wrong with generating the key pair." );
}


- (NSString *)getPublicKeyAsBase64 {
    return [[self publicKeyBits] base64EncodedStringWithOptions:0];


}

- (NSString *)getPrivateKeyAsBase64 {
    return [[self privateKeyBits] base64EncodedStringWithOptions:0];
}

标签: objective-cencryption-asymmetric

解决方案


推荐阅读