2013-02-11 40 views
0

以下代碼對於設備不起作用它不會進入設備的while循環,但它會在模擬器上運行。代碼在iphone上不起作用

int status; 
char value[1024] = "abcd"; 
FILE *fp = popen("openssl enc -aes-128-cbc -k secret -P -md sha1 2>&1", "r"); 
if (fp == NULL) 
    exit(1); // handle error 
int i=0; 
NSString *strAESKey; 
while (fgets(value, 1024, fp) != NULL) 
{ 
    i++; 
    if(i==2) 
    { 
     strAESKey=[NSString stringWithFormat:@"%s",value]; 
     break; 
    } 
} 

status = pclose(fp); 
if (status == -1) 
{ 
    /* Error reported by pclose() */ 
} 
else 
{ 
    /* Use macros described under wait() to inspect `status' in order 
                        to determine success/failure of command executed by popen() */ 
} 

我哪裏錯了?

回答

4

iOS應用程序沙盒禁止使用fork函數,該函數使用popen。模擬器不使用沙箱,但設備。

您將需要直接使用openssl庫而不是使用命令行程序。 iOS公共API不包含openssl庫,因此您需要自己構建一個靜態庫。你可以通過搜索找到一些幫助。我會從this blog post開始。

+0

我們在我們的項目中構建了openssl庫,我們無法生成AES密鑰?請你提供一些參考資料來生成AES密鑰。 – user2050884 2013-02-11 09:52:56

+0

[這是'openssl enc']的源代碼(https://github.com/openssl/openssl/blob/master/apps/enc.c),這是您嘗試運行的命令。 – 2013-02-11 17:09:53