我使用SecKeyCreateWithData创建公钥。在剥离标题后,从Pem创建密钥。
我尝试使用
验证签名我们使用SHA256和Unicode编码对消息进行签名(C#RSACryptoProvider)。
创建公钥的代码:
NSDictionary* attributes =
@{ (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
(id)kSecAttrKeySizeInBits: @2048,
(id)kSecPublicKeyAttrs:
@{ (id)kSecAttrIsPermanent: @YES,
(id)kSecAttrApplicationTag: tag1
},
(id)kSecAttrCanEncrypt:@YES,
(id)kSecAttrCanVerify:@YES,
(id)kSecAttrKeyClass: (id)kSecAttrKeyClassPublic
};
CFErrorRef error = NULL;
SecKeyRef keyRef = SecKeyCreateWithData((__bridge CFDataRef)publicKeyData,
(__bridge CFDictionaryRef)attributes,
&error);
验证码iOS:
size_t signedHashBytesSize = SecKeyGetBlockSize(keyRef);
const void* signedHashBytes = [signature bytes];
NSData *plainData = [dataToSign dataUsingEncoding:NSUTF16StringEncoding];
size_t hashBytesSize = CC_SHA256_DIGEST_LENGTH;
uint8_t* hashBytes = malloc(hashBytesSize);
if (!CC_SHA256([plainData bytes], (CC_LONG)[plainData length], hashBytes)) {
return nil;
}
OSStatus status1 = SecKeyRawVerify(keyRef,
kSecPaddingPKCS1,
hashBytes,
hashBytesSize,
signedHashBytes,
signedHashBytesSize);
验证码OSx:
verifier = SecVerifyTransformCreate(keyRef, (__bridge CFDataRef)self.digest, &errorCF);
if (errorCF) { CFShow(errorCF);}
SecTransformSetAttribute(verifier,
kSecTransformInputAttributeName,
(__bridge CFTypeRef)plainData,
&errorCF);
if (errorCF) { CFShow(errorCF); exit(-1); }
SecTransformSetAttribute(verifier,
kSecDigestTypeAttribute,
kSecDigestSHA2,
&errorCF);
if (errorCF) { CFShow(errorCF); exit(-1); }
SecTransformSetAttribute(verifier,
kSecDigestLengthAttribute,
(__bridge CFNumberRef)@256,
&errorCF);
if (errorCF) { CFShow(errorCF); exit(-1); }
CFBooleanRef result1 = NULL;
result1 = SecTransformExecute(verifier, &errorCF);
BOOL success = (result1 != NULL);
我被困了任何帮助都会受到赞赏。