2014-10-16 1 views
4

Я использую AES256 для шифрования и дешифрования с помощью ключа. Все работает нормально, и я получаю идеальный результат с iPhone5. Но когда я пытаюсь использовать его на iPhone6, симулятор 6+ возвращает мне нулевые данные. Это вопрос 64 бит? Не уверен.AES256Encryption возвращает данные nil на 64-битном устройстве

Я использую этот два метода для шифрования и дешифрования данных.

Моя длина ключа 44, которую я передаю для шифрования и дешифрования данных.

- (NSData *)AES256EncryptWithKey:(NSString *)key { 

BOOL LongKey = NO; 
// 'key' should be 32 bytes for AES256, will be null-padded otherwise 
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) 
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 
// fetch key data 
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 

NSUInteger dataLength = [self length]; 

//See the doc: For block ciphers, the output size will always be less than or 
//equal to the input size plus the size of one block. 
//That's why we need to add the size of one block here 
size_t bufferSize = dataLength + kCCBlockSizeAES128; 
void *buffer = malloc(bufferSize); 

size_t numBytesEncrypted = 0; 
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 
             keyPtr, kCCKeySizeAES256, 
             NULL /* initialization vector (optional) */, 
             [self bytes], dataLength, /* input */ 
             buffer, bufferSize, /* output */ 
             &numBytesEncrypted); 
if (cryptStatus == kCCSuccess) { 
    //the returned NSData takes ownership of the buffer and will free it on deallocation 
    return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; 
} 

free(buffer); //free the buffer; 
return nil; 
} 



- (NSData *)AES256DecryptWithKey:(NSString *)key { 

BOOL LongKey = NO; 
// 'key' should be 32 bytes for AES256, will be null-padded otherwise 

char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) 
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 
// fetch key data 
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 

NSUInteger dataLength = [self length]; 

//See the doc: For block ciphers, the output size will always be less than or 
//equal to the input size plus the size of one block. 
//That's why we need to add the size of one block here 
size_t bufferSize = dataLength + kCCBlockSizeAES128; 
void *buffer = malloc(bufferSize); 

size_t numBytesDecrypted = 0; 
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 
             keyPtr, kCCKeySizeAES256, 
             NULL /* initialization vector (optional) */, 
             [self bytes], dataLength, /* input */ 
             buffer, bufferSize, /* output */ 
             &numBytesDecrypted); 

if (cryptStatus == kCCSuccess) { 
    //the returned NSData takes ownership of the buffer and will free it on deallocation 
    return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; 
} 

free(buffer); //free the buffer; 
return nil; 
} 

Я попытался с помощью Модификация эти методы, как показано ниже. Но успеха нет.

- (NSData *)AES256EncryptWithKey:(NSString *)key { 

BOOL LongKey = NO; 
// 'key' should be 32 bytes for AES256, will be null-padded otherwise 
if (key.length>32) 
{ 
    LongKey = YES; 
    key = [key substringToIndex:32]; 
} 
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) 
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 
// fetch key data 
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 

if (LongKey) { 
    keyPtr[0]= 0; 
} 
keyPtr[32]= 0; 

NSUInteger dataLength = [self length]; 

//See the doc: For block ciphers, the output size will always be less than or 
//equal to the input size plus the size of one block. 
//That's why we need to add the size of one block here 
size_t bufferSize = dataLength + kCCBlockSizeAES128; 
void *buffer = malloc(bufferSize); 

size_t numBytesEncrypted = 0; 
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 
             keyPtr, kCCKeySizeAES256, 
             NULL /* initialization vector (optional) */, 
             [self bytes], dataLength, /* input */ 
             buffer, bufferSize, /* output */ 
             &numBytesEncrypted); 
if (cryptStatus == kCCSuccess) { 
    //the returned NSData takes ownership of the buffer and will free it on deallocation 
    return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; 
} 

free(buffer); //free the buffer; 
return nil; 
} 


- (NSData *)AES256DecryptWithKey:(NSString *)key { 

BOOL LongKey = NO; 
// 'key' should be 32 bytes for AES256, will be null-padded otherwise 
if (key.length>32) 
{ 
    LongKey = YES; 
    key = [key substringToIndex:32]; 
} 
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) 
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 
// fetch key data 
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 

if (LongKey) { 
    keyPtr[0]= 0; 
} 
keyPtr[32]= 0; 

NSUInteger dataLength = [self length]; 

//See the doc: For block ciphers, the output size will always be less than or 
//equal to the input size plus the size of one block. 
//That's why we need to add the size of one block here 
size_t bufferSize = dataLength + kCCBlockSizeAES128; 
void *buffer = malloc(bufferSize); 

size_t numBytesDecrypted = 0; 
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 
             keyPtr, kCCKeySizeAES256, 
             NULL /* initialization vector (optional) */, 
             [self bytes], dataLength, /* input */ 
             buffer, bufferSize, /* output */ 
             &numBytesDecrypted); 

if (cryptStatus == kCCSuccess) { 
    //the returned NSData takes ownership of the buffer and will free it on deallocation 
    return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; 
} 

free(buffer); //free the buffer; 
return nil; 
} 

Не могли бы вы предложить, что может быть проблемой здесь? Спасибо.

+0

Вы нашли решение для этого. Я получаю такую ​​же проблему даже на устройствах? – iPrabu

ответ

1

Вам необходимо протестировать его на реальном устройстве. Это также терпит неудачу для нас на эмуляторах (Xcode5/6)

+0

означает ли это, что он работает на реальном устройстве для вас и не работает на симуляторах? – Nikunj

+0

да. мы тестируем шифрование/дешифрование только на реальных устройствах. Он всегда терпит неудачу на эмуляторах даже в старых Xcodes. – kabarga

+0

ok bro спасибо, Позвольте мне проверить устройство. – Nikunj

Смежные вопросы