2

我有个问题:

我加密数据,并在 base64 中对其进行编码..之后,我将其发送到我的页面 php 并解码和解密数据。好的!

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

明文:xLxE9sY8vkBTGDpvz0DkLkejLSjuVhBq

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

在我的应用程序中解密 CHIPER 服务器:xLxE9sY8vkBTGDpv <--- 它是一半!!!!

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

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

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

和解密是好的:

解密芯片应用程序: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;
}
4

1 回答 1

1

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

于 2013-01-13T02:03:16.213 回答