中按预期工作不正常我需要在将请求发送到后端服务器之前对其进行签名。然而,私钥给我。所以我需要导入它,然后用它来签名。我能够导入和登录,但是这些数据与使用openssl进行签名所获得的数据不同。我知道它做错了,因为当我导入公钥时,我无法验证它。如果有一种方法可以避免导入钥匙串,那也太好了。 几天来一直在努力工作,这对我们来说是一件高价值的工作。有人可以帮忙吗? 将私钥导入钥匙串在iphone
- (SecKeyRef) getPrivateKey {
//RSA KEY BELOW IS DUMMY.
key = @"-----BEGIN RSA PRIVATE KEY-----\nORtMei3ImKI2ZKI636I4+uNCwFfZv9pyJzXyfr1ZNo7iaiW7A0NjLxikNxrWpr/M\n6HD8B2j/CSjRPW3bhsgDXAx/AI1aSfJFxazjiTxx2Lk2Ke3jbhE=\n-----END RSA PRIVATE KEY-----\n";
NSString * tag = @"adpPrivateKey";
NSString *s_key = [NSString string];
NSArray *a_key = [key componentsSeparatedByString:@"\n"];
BOOL f_key = FALSE;
for (NSString *a_line in a_key) {
if ([a_line isEqualToString:@"-----BEGIN RSA PRIVATE KEY-----"]) {
f_key = TRUE;
}
else if ([a_line isEqualToString:@"-----END RSA PRIVATE KEY-----"]) {
f_key = FALSE;
}
else if (f_key) {
s_key = [s_key stringByAppendingString:a_line];
}
}
if (s_key.length == 0) return(nil);
// This will be base64 encoded, decode it.
NSData *d_key = [NSData dataFromBase64String:s_key];
if(d_key == nil) return nil;
NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];
// Delete any old lingering key with the same tag
NSMutableDictionary *privateKey = [[NSMutableDictionary alloc] init];
[privateKey setObject:(id) kSecClassKey forKey:(id)kSecClass];
[privateKey setObject:(id) kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
[privateKey setObject:d_tag forKey:(id)kSecAttrApplicationTag];
SecItemDelete((CFDictionaryRef)privateKey);
CFTypeRef persistKey = nil;
// Add persistent version of the key to system keychain
[privateKey setObject:d_key forKey:(id)kSecValueData];
[privateKey setObject:(id) kSecAttrKeyClassPrivate forKey:(id)
kSecAttrKeyClass];
[privateKey setObject:[NSNumber numberWithBool:YES] forKey:(id)
kSecReturnPersistentRef];
OSStatus secStatus = SecItemAdd((CFDictionaryRef)privateKey, &persistKey);
if (persistKey != nil) CFRelease(persistKey);
if ((secStatus != noErr) && (secStatus != errSecDuplicateItem)) {
[privateKey release];
return(nil);
}
// Now fetch the SecKeyRef version of the key
SecKeyRef keyRef = nil;
[privateKey removeObjectForKey:(id)kSecValueData];
[privateKey removeObjectForKey:(id)kSecReturnPersistentRef];
[privateKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnRef
];
[privateKey setObject:(id) kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
secStatus = SecItemCopyMatching((CFDictionaryRef)privateKey,
(CFTypeRef *)&keyRef);
if(secStatus != noErr)
return nil;
[privateKey release];
return keyRef;
}
下面的代码是用于签署。部分代码是从苹果公司的例子(http://developer.apple.com/library/ios/#samplecode/CryptoExercise/Listings/Classes_SecKeyWrapper_m.html#//apple_ref/doc/uid/DTS40008019-Classes_SecKeyWrapper_m-DontLinkElementID_17)
- (NSData *)getSignatureBytes:(NSString *)plainText {
OSStatus sanityCheck = noErr;
NSData * signedHash = nil;
uint8_t * signedHashBytes = NULL;
size_t signedHashBytesSize = 0;
SecKeyRef privateKey = NULL;
privateKey = [self getPrivateKey];
signedHashBytesSize = SecKeyGetBlockSize(privateKey);
//Create a SHA Encoded
NSString * shaEncoded = [self sha256:plainText];
NSLog(@"%@", shaEncoded);
// Malloc a buffer to hold signature.
signedHashBytes = malloc(signedHashBytesSize * sizeof(uint8_t));
memset((void *)signedHashBytes, 0x0, signedHashBytesSize);
NSData *inputData = [self getHashBytes:[plainText dataUsingEncoding:NSUTF8StringEncoding]];
int bytesLengthUINT8 = [inputData length];
sanityCheck = SecKeyRawSign (privateKey, kSecPaddingPKCS1, (const uint8_t *)inputData, CC_SHA256_DIGEST_LENGTH,(uint8_t *)signedHashBytes, &signedHashBytesSize);
if(sanityCheck != noErr)
return nil;
signedHash = [NSData dataWithBytes:(const void *)signedHashBytes length:(NSUInteger)signedHashBytesSize];
NSString *string = [signedHash base64EncodedString];
NSLog(@"%@", string);
if (signedHashBytes) free(signedHashBytes);
return signedHash;
}
我使用的代码示例中http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/导入公用密钥并验证其失败。
'kSecPaddingPKCS1'的价值是什么?你可以尝试比较公钥和私钥的模数吗? –
嗨@owlstead,你可以详细说明该怎么办?你也看到任何问题,我保存和检索私钥的方式。请让我知道是否有其他我可以使用的库。 –
对不起,我不是ios专家。尽管我现在对加密有很多了解,所以我想我会给你一些一般性建议。例如。 'kSecPaddingPKCS1'不指定散列,并且SHA-1很可能是默认值。如果私钥和公钥的模数不匹配,则它们不属于同一个密钥对。在这两种情况下,签名验证都会失败。 –