objective-c - 如何在目标 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];
}
解决方案
推荐阅读
- python - 如何将列表共享为列表而不是不同函数之间的元组?
- java - 爪哇。lambda 中预期的数组类型
- clojure - 有没有人使用 Datomic 分别获取结构和实体?
- c - c : gdb : 覆盖和释放太多内存
- concurrency - 并发 - 如何从单独的任务中分配变量?
- vba - VBA 随机数
- html - nsdata 到 nsstring 转换中的 NSUTF8StringEncoding 问题
- debugging - React Native Debugger 应用程序将无法打开 - debugger-ui 改为在 Chrome 中打开
- r - 包含 data.table 名称的变量已就地更改?
- python-3.x - 为 python gui 的 Radiobutton 创建一个类