2012-07-16 36 views
1

确定加密iPhone上,但绝对不会在PHP解密

我使用这个在iPhone上我的资料加密:

- (NSData *)AES128EncryptWithKey:(NSString *)key{ 

char keyPtr[kCCKeySizeAES128 + 1]; // room for terminator (unused) 
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 

// fetch key data 
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 

NSUInteger dataLength = [self length]; 

//See the doc: For block ciphers, the output size will always be less than or 
//equal to the input size plus the size of one block. 
//That's why we need to add the size of one block here 
size_t bufferSize = dataLength + kCCBlockSizeAES128; 
void *buffer = malloc(bufferSize); 

size_t numBytesEncrypted = 0; 
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode /*| kCCOptionPKCS7Padding*/, 
             keyPtr, kCCKeySizeAES128, 
             NULL /* initialization vector (optional) */, 
             [self bytes], dataLength, /* input */ 
             buffer, bufferSize, /* output */ 
             &numBytesEncrypted); 
if(cryptStatus == kCCSuccess) 
{ 
    //the returned NSData takes ownership of the buffer and will free it on deallocation 
    return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; 
} 

free(buffer); //free the buffer 
return nil;} 
我的服务器上

我的PHP脚本使用:

 $base64encoded_ciphertext = $pass; 

    mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($pass), 'ecb'); 

    $decrypted = $res_non; 
    $dec_s2 = strlen($decrypted); 

    $padding = ord($decrypted[$dec_s2-1]); 
    $decrypted = substr($decrypted, 0, -$padding); 
    return $decrypted; 

但是,无论关键是什么,它都会失败。

键总是10个字符长。我使用系统时钟建立密码来获取值。

在PHP方面我复制了关键的建筑,并根据我的脚本,关键总是匹配什么iPhone用于加密。

此代码在不同的脚本中工作,从一个不同的应用程序...仍然有效。 我已经完成了所有相关代码的死锁和粘贴,但仍然没有任何结果。

我只是不知道我做错了什么......超出了我想要做说不定正在绝对不可能

+1

我希望你没有把这个密码存储在某个数据库中。 – 2012-07-16 05:06:20

回答

0

对于AES 128,你应该使用16字节的加密密钥,AES 192是24, AES 256是32字节。对于你的情况,你应该使用加密密钥,如@“123456789”。