0

I read cert info by use

NSString *summary = CFBridgingRelease(SecCertificateCopySubjectSummary(cert));

which get the cert summary, when the summary is chinese, such as ,the summary return as ä¸

how can i encode ä¸ to correct?

The code how i get cert summary

CFMutableDictionaryRef query = CFDictionaryCreateMutable(nil, 4, nil, nil);
CFDictionaryAddValue(query, kSecClass, kSecClassCertificate);

CFDictionaryAddValue(query, kSecMatchLimit, (const void *)@(100));
CFDictionaryAddValue(query, kSecReturnRef, kCFBooleanTrue);

CFArrayRef items;
OSStatus status = SecItemCopyMatching(query, (CFTypeRef *)&items);
NSArray *certificates = CFBridgingRelease(items);
for (int i = 0; i < certificates.count; i++)
{
    SecCertificateRef certificate = (__bridge SecCertificateRef)certificates[i];
    NSString *summary = (NSString *)CFBridgingRelease(SecCertificateCopySubjectSummary(certificate);
    NSLog(@"%@", summary);
}

when summary is a chinese string, the result is not right.

5
  • How did you examine the summary string? Show the code or debugger commands you used to display/print/log it and the exact output. Commented Sep 1, 2017 at 4:54
  • add the code in the question, thx. Commented Sep 1, 2017 at 6:35
  • You're missing a closing parenthesis on the SecCertificateCopySubjectSummary() line, which makes me wonder how this differs from your real code. Anyway, it may be that this is just a bug in SecCertificateCopySubjectSummary(). An NSString or CFString is intended to be encoding-neutral. Or, another way to think about it, it should be already decoded into an internal representation. The API is in terms of "characters", by which it actually means UTF-16 code units, but the internal representation could be anything. It should not be possible to get an encoding mistake like this. Commented Sep 1, 2017 at 8:19
  • I suppose it's also possible the certificate itself is malformed. For example, it may be that it should use some specific encoding like UTF-8 for its summary, but the data in the certificate is actually using some other encoding. Does it look correct in the Keychain Access utility? Commented Sep 1, 2017 at 8:20
  • thx for help. finally solve the problem by decode utf8. i post an answer for help others Commented Sep 1, 2017 at 8:54

1 Answer 1

0

Solve the problem. First i print every char in the nsstring, looks like

228 184 173

And chinese character use 3 char in utf8, so i switch it to bin

0b1110 0100
0b10 111000
0b10 101101
//for utf8 to unicode, it's
0100111000101101
//to int
20013
//which is 中 at unicode

And for UTF8 Decoding with NSString help, finally switch the string form utf8 to unicode.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.