从PHP的Objective-C中解密AES128 / CBC

时间:2014-05-19 18:05:14

标签: ios objective-c encryption aes

我是xCode和Objective-C的新手。我已经编码了QR扫描仪,现在我需要解密使用AES128 CBC加密的数据。我在php:http://www.androidsnippets.com/encrypt-decrypt-between-android-and-php

中使用此片段加密明文 你可以帮我吗?抱歉我的英语不好,我是德语:D

1 个答案:

答案 0 :(得分:4)

使用CommonCrypto,请参阅CommonCryptoCCCryptor

这是一个让你开始的噱头:

+ (NSData *)doCipher:(NSData *)dataIn
                  iv:(NSData *)iv
                 key:(NSData *)symmetricKey
             context:(CCOperation)encryptOrDecrypt
               error:(NSError **)error
{
    CCCryptorStatus ccStatus   = kCCSuccess;
    size_t          cryptBytes = 0;    // Number of bytes moved to buffer.
    NSMutableData  *dataOut    = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];

    ccStatus = CCCrypt( encryptOrDecrypt,
                       kCCAlgorithmAES128,
                       kCCOptionPKCS7Padding,
                       symmetricKey.bytes, 
                       kCCKeySizeAES128,
                       iv.bytes,
                       dataIn.bytes,
                       dataIn.length,
                       dataOut.mutableBytes,
                       dataOut.length,
                       &cryptBytes);

    if (ccStatus == kCCSuccess) {
        dataOut.length = cryptBytes;
    }
    else {
        if (error) {
            *error = [NSError errorWithDomain:@"kEncryptionError"
                                         code:ccStatus
                                     userInfo:nil];
        }
        dataOut = nil;
    }

    return dataOut;
}

另请参阅RNCryptor了解完整实施情况。