为什么 iPad 上的 AES 加密和 PHP 解密会失败?

2023-12-07

我有一个 iPad 应用程序,可以将加密信息传输到基于 PHP 的网站,但我在正确解密此信息时遇到困难。我使用以下代码进行PHP端解密:

//Decryption function

function mc_decrypt($decrypt, $key, $iv)  
{  
    $decoded = base64_decode($decrypt);  
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');  
    mcrypt_generic_init($td, $key, $iv);  
    $decrypted = mdecrypt_generic($td, $decoded);  
    mcrypt_generic_deinit($td);  
    mcrypt_module_close($td);  
    return trim($decrypted);  
}  

我的 iPad 应用程序中的 Objective-C 代码:

#import <CommonCrypto/CommonCryptor.h>

@implementation NSData (AES256)

- (NSData *)AES256EncryptWithKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+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, kCCOptionPKCS7Padding,
                                     keyPtr, kCCKeySizeAES256,
                                     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;
}

- (NSData *)AES256DecryptWithKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+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 numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                     keyPtr, kCCKeySizeAES256,
                                     NULL /* initialization vector (optional) */,
                                     [self bytes], dataLength, /* input */
                                     buffer, bufferSize, /* output */
                                     &numBytesDecrypted);

    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}

@end

当我尝试解密在 iPad 上编码并在 PHP 端解密的数据时,为什么会看到数据损坏?


检查您正在使用的密钥。在 PHP 中,MCRYPT_RIJNDAEL_128 _256 等常量并不代表密钥强度,而是代表所使用的块大小。

要使用 PHP 获得 128 位加密,您需要使用 16 字节长的密钥。对于 256,您需要 32 个字节,依此类推。

你的 PHP 和 C 代码对我来说都是正确的。确保在这两种情况下都正确使用密钥。

作为另一个想法。看起来您正在 C 中使用 PKCS#7 填充。我不相信 PHP 被设计为默认使用该填充。如果我没记错的话,我相信 Mcrypt 函数使用空填充。

最后检查 PHP 中的初始化向量。我注意到你没有在你的 C 代码中使用它,这意味着你不应该在 PHP 中接受 $iv 变量。应该将其作为 NULL 传递到 mcrypt 函数中(但强烈建议不要这样做)。

相反,您应该做的是在 C 代码中随机化 IV(其中数据被加密),然后将 IV 与加密数据一起发送。您可以检测正在使用的算法的 IV 的大小,并将其从加密数据的前面分离出来,然后用于填充 PHP 方面的内容。这进一步确保您的加密安全。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 iPad 上的 AES 加密和 PHP 解密会失败? 的相关文章