У меня проблема с преобразованием шифрования .NET C # AES 128 в iOS (iPhone).Все сгенерировано правильно, кроме последнего блока.Последний вызов xfrm.TransformFinalBlock генерирует не так, как я получил из кода iOS (используя CCCryptorFinal).Я использую один и тот же параметр для BlockSize, Key и IV.Что еще мне не хватает?Спасибо.
Obj-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();