iPhone AES Rijndael 128 解密

标签 iphone aes decode encryption

我有一个问题:

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

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

明文:xLxE9sY8vkBTGDpvz0DkLkejLSjuVhBq

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

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

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

来自应用程序的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;
}

最佳答案

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

关于iPhone AES Rijndael 128 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14299641/

相关文章:

iphone - 在 iPhone/iPad 中使用加速度计进行水平和垂直抖动计数

linux - 想要一个2.6.38以上内核版本使用aes加密方式的例子

javascript - AES 加密 PHP 到 NodeJS?

java - 移位密码错误地移位了一些字符

python - 解码 os.urandom() 字节对象

iphone - 如何捕获自定义uitableViewCell中的开关更改事件?

iphone - 如何识别 UIPickerView 上的滚轮旋转动画?

iphone - Objective-C 中的延迟加载

vb.net - VB.NET Rijndael 托管加密或 AES 的安全性如何?

json - 将 JSON 解码为 Elm Maybe