简体   繁体   English

在iPhone上加密,但绝对不会在php中解密

[英]Encrypted on iPhone, but absolutely will not decrypt in php

ok

I'm using this to encrypt my data on iPhone: 我正在使用它来加密我在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;}

on my server my php script uses: 在我的服务器上,我的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;

However, no matter what the key is, it fails. 但是,无论键是什么,它都会失败。

The keys are always 10 characters long. 键始终为10个字符长。 I build a password using the system clock to get values. 我使用系统时钟建立密码以获取值。

On the php side I duplicate the key building and according to my script, the key ALWAYS matches what the iPhone used to encrypt. 在php端,我复制了密钥构建,并根据我的脚本,密钥始终与iPhone用来加密的密钥匹配。

This code worked in a different script, from a different app… and still works. 该代码在不同的脚本中,在不同的应用程序中运行……仍然有效。 I've done a dead cut and paste of all related code and still nothing. 我已经完成了所有相关代码的固定剪切和粘贴,仍然一无所获。

I just don't know what I'm doing wrong… beyond what I'm trying to do maybe being absolutely impossible 我只是不知道自己在做什么错...超出我正在尝试做的事情,可能是绝对不可能的

For AES 128 you should use 16 byte Encryption key, AES 192 it is 24, AES 256 it is 32 byte. 对于AES 128,您应该使用16字节的加密密钥,AES 192是24,AES 256是32字节。 For your case you should use encryption key like @"1234567890123456". 对于您的情况,您应该使用@“ 1234567890123456”这样的加密密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM