Search code examples
iosobjective-ciphonejailbreak

convert NSString from char array contains chinese character


I'm use openssl extract code sign from p12 file, and got it. but it not a readable string object. for example, my code sign is :""iPhone Developer: 振 王", and the function returns "iPhone Developer: \xE6\x8C\xAF \xE7\x8E\x8B"

here's the code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSString *p1 = @"/Users/william/Desktop/root.p12";
    NSString *s = [self getCodesignFromP12Path:p1 andPassword:@"1"];

    // It's output 'iPhone Developer: \xE6\x8C\xAF \xE7\x8E\x8B (7V3KMVKNR4)'
    NSLog(@"%@", s);
}

- (NSString *)getCodesignFromP12Path:(NSString *)p12Path andPassword:(NSString *)pwd
{
    NSString *string = nil;
    NSString *command = [NSString stringWithFormat:@"openssl pkcs12 -in %@ -nodes -passin pass:%@ | openssl x509 -noout -subject  | awk -F'[=/]' '{print $6}'", p12Path, pwd];

    FILE*pipein_fp;
    char readbuf[80] = {0};

    if((pipein_fp=popen([command cStringUsingEncoding:NSUTF8StringEncoding],"r"))==NULL)
    {
        perror("popen");
        exit(1);
    }

    while(fgets(readbuf,80,pipein_fp))
    {
        string = [NSString stringWithCString:readbuf encoding:NSUTF8StringEncoding];
    }

    pclose(pipein_fp);
    return string;
}

how can i get the correct chinese character value ?


Solution

  • Now I got the code sign by this solution, but I don't know what cause it if use openssl. here's the github demo.

    const NSString * kCertificationPassword = @"1";
    
    @implementation AppDelegate
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        // Insert code here to initialize your application
        NSString *ret = nil;
        NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"root" ofType:@"p12"];
    
        // Dump identifier with openssl
        NSString *identifierWithSSL = [NSString stringWithFormat:@"openssl pkcs12 -in %@ -nodes -passin pass:%@ | openssl x509 -noout -subject  | awk -F'[=/]' '{print $6}'", cerPath, kCertificationPassword];
        [self shellCMD:identifierWithSSL result:&ret];
        // Output: iPhone Developer: \xE6\x8C\xAF \xE7\x8E\x8B (7V3KMVKNR4)
        NSLog(@"%@", ret);
    
        // Import certification to keychain.
        NSString *importComand = [NSString stringWithFormat:@"security import %@ -k ~/Library/Keychains/login.keychain -P %@ -T /usr/bin/codesign",cerPath, kCertificationPassword];
        [self shellCMD:importComand result:nil];
    
        // Dump identifier with security.
        NSString *identifierWithSecurity = @"security find-identity -p codesigning | grep 7V3KMVKNR4 | awk -F'[\"\"]' '{print $2}'";
        [self shellCMD:identifierWithSecurity result:&ret];
        // Output: iPhone Developer: 振 王 (7V3KMVKNR4)
        NSLog(@"%@", ret);
    }
    
    - (void)shellCMD:(NSString*)cmd result:(NSString**)aResult
    {
        FILE*pipein_fp;
        char readbuf[80] = {0};
    
        if((pipein_fp=popen([cmd cStringUsingEncoding:NSUTF8StringEncoding],"r"))==NULL)
        {
            perror("popen");
            exit(1);
        }
    
        while(fgets(readbuf,80,pipein_fp))
        {
            if ( aResult )
            {
                *aResult = [NSString stringWithCString:readbuf encoding:NSUTF8StringEncoding];
            }
        }
    
        pclose(pipein_fp);
    }
    @end