我有一个由java使用以下代码生成的base64公钥:
RSAPublicKeySpec rsaKS = new RSAPublicKeySpec(modulus, pubExponent);
RSAPublicKey rsaPubKey = (RSAPublicKey) kf.generatePublic(rsaKS);
byte[] encoded = rsaPubKey.getEncoded();
String base64 = Base64.encodeToString(encoded, Base64.DEFAULT);
Log.e(null, "base64: " + base64);这将导致一个Base64字符串。
在OSX中,我可以使用以下代码获得一个SecKeyRef:
// Create the SecKeyRef using the key data
CFErrorRef error = NULL;
CFMutableDictionaryRef parameters = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
CFDictionarySetValue(parameters, kSecAttrKeyType, kSecAttrKeyTypeRSA);
CFDictionarySetValue(parameters, kSecAttrKeyClass, kSecAttrKeyClassPublic);
SecKeyRef keyRef = SecKeyCreateFromData(parameters, (__bridge CFDataRef)[pubKey base64DecodedData], &error);然而,在iOS中没有SecKeyCreateFromData方法。
我可以在iOS中使用iOS中的这段代码字符串,后者将它添加到密钥链中,然后以SecKeyRef的形式再次检索它,但是,为了能够检索它一次,我宁愿不必将证书添加到密钥链中。
通过一些研究,我似乎应该能够使用SecCertificateCreateWithData从iOS字符串中创建一个在iOS中使用的证书,但是在使用以下代码时总是会得到一个空证书:
NSString* pespublicKey = @"MIGfMA0GCSqGSIb3....DCUdz/y4B2sf+q5n+QIDAQAB";
NSData* certData = [pespublicKey dataUsingEncoding:NSUTF8StringEncoding];
SecCertificateRef cert;
if ([certData length]) {
cert = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)certData);
if (cert != NULL) {
CFStringRef certSummary = SecCertificateCopySubjectSummary(cert);
NSString* summaryString = [[NSString alloc] initWithString:(__bridge NSString*)certSummary];
NSLog(@"CERT SUMMARY: %@", summaryString);
CFRelease(certSummary);
} else {
NSLog(@" *** ERROR *** trying to create the SSL certificate from data located at %@, but failed", pespublicKey);
}
}发布于 2015-04-19 04:40:31
您不是base64 64-首先解码您的关键数据。您正在将base64 64编码的数据传递给SecCertificateCreateWithData(),该函数需要原始的解码数据。试着做这样的事情:
NSData *certData = [[NSData alloc] initWithBase64EncodedString:pespublicKey options:0];
cert = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)certData);更新:
您要发送到iOS代码的是base64 DER编码的密钥,而不是DER或PEM编码的证书。因此,您看到的结果是预期的--您给它一个编码的数据blob,它不包含证书,它给您返回一个空证书引用,表示不存在的证书数据。
你有两个选择:
要使第二个选项工作,Java代码不仅必须拥有公钥,还需要关联的私钥来生成一个签名的证书。
根据您对SecKeyRef的计划,您可能会遇到问题。SecKeyRef值可以直接转换为SecKeychainItemRef值,以便在Keychain函数中使用。如果SecKeyRef值不是来自密钥链,那么您的代码将得到错误。阅读文档以获得更多信息。
https://stackoverflow.com/questions/29542986
复制相似问题