最后一个块为AES 128加密生成(在iOS和.NET之间)

时间:2012-03-13 21:47:39

标签: iphone .net encryption aes

我在将.NET C#AES 128加密转换为iOS(iPhone)时遇到问题。除最后一个块外,所有内容都正确生成。对xfrm.TransformFinalBlock的最终调用生成与我从iOS代码(使用CCCryptorFinal)获得的不同。我对BlockSize,Key和IV使用相同的参数。我还缺少什么?谢谢。

的OB​​J-C

CCCryptorStatus ccStatus = kCCSuccess;


CCCryptorRef thisEncipher = NULL;

// Create and Initialize the crypto reference.
ccStatus = CCCryptorCreate(kCCEncrypt, 
                           kCCAlgorithmAES128, 
                           kCCOptionPKCS7Padding, 
                           szKey,
                           kCCKeySizeAES128, 
                           iv, 
                           &thisEncipher
                           );

// Calculate byte block alignment for all calls through to and including final.
int bufferPtrSize = CCCryptorGetOutputLength(thisEncipher, InputLength, true);

unsigned char *szResult = (unsigned char *)malloc(bufferPtrSize+1);

memset(szResult, 0x00, bufferPtrSize+1);

while (dataEncrypted < dataToEncrypt)
{
    bool final = (dataToEncrypt - dataEncrypted) <= lrcEncryptionBlockSize;

    if (final)
    {
        // Finalize everything to the output buffer.

        ccStatus = CCCryptorFinal(thisEncipher,
                                  szResult + dataEncrypted,
                                  bufferPtrSize - dataEncrypted,
                                  pOutSize
                                  );


        dataEncrypted += *pOutSize;

        for(int c=0;c<dataEncrypted;++c)
        {
            printf("\n%d => %d\n", c, szResult[c]);
        }

        success = true;
        break;
    }
    else
    {

    // Actually perform the encryption or decryption.
        ccStatus = CCCryptorUpdate( thisEncipher,
                                   szInput+cb,
                                   lrcEncryptionBlockSize,
                                   szResult+dataEncrypted,
                                   bufferPtrSize - dataEncrypted,
                                   pOutSize
                                   );   


        dataEncrypted += *pOutSize;

        cb += lrcEncryptionBlockSize;

    }
}

if (thisEncipher) {
    (void) CCCryptorRelease(thisEncipher);
    thisEncipher = NULL;
}

C#

        AesManaged aesAlg = new AesManaged();
        ICryptoTransform xfrm;

        aesAlg.IV = GenerateIV();
        aesAlg.Key = "1111111111111111";
        aesAlg.BlockSize = 128;

        xfrm = aesAlg.CreateEncryptor();

        while (dataEncrypted < dataToEncrypt)
        {
            bool final = (dataToEncrypt - dataEncrypted) <= lrcEncryptionBlockSize;
            int nbytes = !final ? lrcEncryptionBlockSize : (int)(dataToEncrypt - dataEncrypted); 

            if (final)
            {
                byte[] finalblock = xfrm.TransformFinalBlock(message, cb, nbytes);
                Buffer.BlockCopy(finalblock, 0, message, cb, lrcEncryptionBlockSize);
                success = true;
                break;
            }
            else
            {
                dataEncrypted += (uint)xfrm.TransformBlock(message, cb, nbytes, message, cb);
                cb += lrcEncryptionBlockSize;
            }
        }

        xfrm.Dispose();
        aesAlg.Clear();

1 个答案:

答案 0 :(得分:2)

如果只有最后一个块不同,则差异可能是它们使用不同的填充。第一个例子使用PKCS7,但我看不出第二个使用的是什么。