从char数组转换NSString包含中文字符

时间:2014-10-23 07:04:17

标签: ios objective-c iphone jailbreak

我正在使用来自p12文件的openssl解压缩代码符号,并得到它。但它不是一个可读的字符串对象。 例如,我的代码签名是:“”iPhone Developer:振王“,该函数返回”iPhone Developer:\ xE6 \ x8C \ xAF \ xE7 \ x8E \ x8B“

这是代码:

- (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;
}

我怎样才能获得正确的中文字符值?

2 个答案:

答案 0 :(得分:0)

您很可能会将NSLog的输出与字符串的内容混淆。

NSLog不会打印中文字符。 iOS功能如设置按钮标题,或内容到文本将正常工作。

答案 1 :(得分:0)

现在我通过这个解决方案获得了代码签名,但是如果使用openssl,我不知道是什么原因造成的。这是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