iPhone AES Rijndael 128解密

时间:2013-01-13 01:08:02

标签: iphone aes decode encryption

我有一个问题:

我加密数据,并在base64中编码。之后,我将它发送到我的页面php并解码和解密数据。 OK!

现在,如果我在php中加密和编码数据并将其发送到我的应用程序,我的应用程序解码数据并解密数据,但结果是字符串长度的一半。看这个:

PLAINTEXT: xLxE9sY8vkBTGDpvz0DkLkejLSjuVhBq

来自服务器的CHIPER: jp6gtdy / tf8mnQoeZCQjPuauq089eJvmns0DP4kvDzw =

在我的APP中解密CHIPER服务器: xLxE9sY8vkBTGDpv < --- IT'S HALF !!!!

但如果我在我的应用中加密明文,结果会有所不同:

来自APP的CHIPER: jp6gtdy / tf8mnQoeZCQjPuauq089eJvmns0DP4kvDzzjd4QC2afnXreH / VpUo / Mw

来自服务器的CHIPER: jp6gtdy / tf8mnQoeZCQjPuauq089eJvmns0DP4kvDzw =

和解密就可以了:

DECRYPT CHIPER APP: xLxE9sY8vkBTGDpvz0DkLkejLSjuVhBq

现在,我使用了带有2个chyper文本的在线工具......结果是一样的!!明文!!!尝试!!! (http://www.tools4noobs.com/online_tools/decrypt/)!?!?!?!

我的应用程序中的功能是:

+ (NSData*)decryptData:(NSData*)data key:(NSData*)key iv:(NSData*)iv;
{
    NSData* result = nil;

    // setup key
    unsigned char cKey[FBENCRYPT_KEY_SIZE];
    bzero(cKey, sizeof(cKey));
    [key getBytes:cKey length:FBENCRYPT_KEY_SIZE];

    // setup iv
    char cIv[FBENCRYPT_BLOCK_SIZE];
    bzero(cIv, FBENCRYPT_BLOCK_SIZE);
    if (iv) {
        [iv getBytes:cIv length:FBENCRYPT_BLOCK_SIZE];
    }

    // setup output buffer
    size_t bufferSize = [data length] + FBENCRYPT_BLOCK_SIZE;
    void *buffer = malloc(bufferSize);

    // do decrypt
    size_t decryptedSize = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                          FBENCRYPT_ALGORITHM,
                                          kCCOptionPKCS7Padding,
                                          cKey,
                                          FBENCRYPT_KEY_SIZE,
                                          cIv,
                                          [data bytes],
                                          [data length],
                                          buffer,
                                          bufferSize,
                                          &decryptedSize);

    if (cryptStatus == kCCSuccess) {
        result = [NSData dataWithBytesNoCopy:buffer length:decryptedSize];
    } else {
        free(buffer);
        NSLog(@"[ERROR] failed to decrypt| CCCryptoStatus: %d", cryptStatus);
    }

    return result;
}

此功能首先解码并解密后:

  + (NSString*)decryptBase64String:(NSString*)encryptedBase64String keyString:(NSString*)keyString
    {
        NSData* encryptedData = [NSData dataFromBase64String:encryptedBase64String];
        NSData* data = [self decryptData:encryptedData
                                     key:[keyString dataUsingEncoding:NSUTF8StringEncoding]
                                      iv:nil];
        if (data) {
            return [[[NSString alloc] initWithData:data
                                          encoding:NSUTF8StringEncoding] autorelease];
        } else {
            return nil;
        }
    }

解码功能:

+ (NSData *)dataFromBase64String:(NSString *)aString
{
    NSData *data = [aString dataUsingEncoding:NSASCIIStringEncoding];
    size_t outputLength;
    void *outputBuffer = NewBase64Decode([data bytes], [data length], &outputLength);
    NSData *result = [NSData dataWithBytes:outputBuffer length:outputLength];
    free(outputBuffer);
    return result;
}

void *NewBase64Decode(
    const char *inputBuffer,
    size_t length,
    size_t *outputLength)
{
    if (length == -1)
    {
        length = strlen(inputBuffer);
    }

    size_t outputBufferSize =
        ((length+BASE64_UNIT_SIZE-1) / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE;
    unsigned char *outputBuffer = (unsigned char *)malloc(outputBufferSize);

    size_t i = 0;
    size_t j = 0;
    while (i < length)
    {
        //
        // Accumulate 4 valid characters (ignore everything else)
        //
        unsigned char accumulated[BASE64_UNIT_SIZE];
        size_t accumulateIndex = 0;
        while (i < length)
        {
            unsigned char decode = base64DecodeLookup[inputBuffer[i++]];
            if (decode != xx)
            {
                accumulated[accumulateIndex] = decode;
                accumulateIndex++;

                if (accumulateIndex == BASE64_UNIT_SIZE)
                {
                    break;
                }
            }
        }

        //
        // Store the 6 bits from each of the 4 characters as 3 bytes
        //
        // (Uses improved bounds checking suggested by Alexandre Colucci)
        //
        if(accumulateIndex >= 2)  
            outputBuffer[j] = (accumulated[0] << 2) | (accumulated[1] >> 4);  
        if(accumulateIndex >= 3)  
            outputBuffer[j + 1] = (accumulated[1] << 4) | (accumulated[2] >> 2);  
        if(accumulateIndex >= 4)  
            outputBuffer[j + 2] = (accumulated[2] << 6) | accumulated[3];
        j += accumulateIndex - 1;
    }

    if (outputLength)
    {
        *outputLength = j;
    }
    return outputBuffer;
}

1 个答案:

答案 0 :(得分:1)

字符串长度为32个字节,这是两个块的大小。 Base64的长度也是32个字节,因此没有添加填充,没有空间用于填充,因此它不是PKCS7填充,删除kCCOptionPKCS7Padding选项。