2012-07-07 66 views
1

我的問題是我需要使用下面的十六進制密鑰進行加密,但它假定它是一個字符串?如何在AES加密中使用十六進制十六進制密鑰?

鍵= 0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11

我使用[密鑰字節]代替曾試圖在CCrypt功能keyptr,但它簡化版,工作...

- (NSData *)AES128EncryptWithKey:(NSString *)key theData:(NSData *)Data { 

// 'key' should be 32 bytes for AES256, will be null-padded otherwise 
char keyPtr[kCCKeySizeAES128]={'1'};; // room for terminator (unused) // oorspronkelijk 256 
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 

// fetch key data 
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding]; 

NSLog(@"keyPtr %s",keyPtr); 

NSUInteger dataLength = [Data 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, kCCOptionECBMode, 
             keyPtr, 
             kCCKeySizeAES128, // oorspronkelijk 256 
             nil, /* initialization vector (optional) */ 
             [Data bytes], 
             dataLength, /* input */ 
             buffer, 
             bufferSize, /* output */ 
             &numBytesEncrypted); 


if (cryptStatus == kCCSuccess) { 
    NSLog(@"Encrypt SUCCESS"); 
    //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 *)AES128DecryptWithKey:(NSString *)key theData:(NSData *)Data{ 

// 'key' should be 32 bytes for AES256, will be null-padded otherwise 
char keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused) // oorspronkelijk 256 
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) 

// fetch key data 
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSASCIIStringEncoding]; 

NSUInteger dataLength = [Data 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, kCCOptionECBMode+kCCOptionPKCS7Padding, 
             keyPtr, kCCKeySizeAES128, // oorspronkelijk 256 
             NULL /* initialization vector (optional) */, 
             [Data bytes], dataLength, /* input */ 
             buffer, bufferSize, /* output */ 
             &numBytesDecrypted); 

if (cryptStatus == kCCSuccess) { 
    NSLog(@"Decrypt SUCCESS"); 
    //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

主要是你需要溝通破壞AES128EncryptWithKey的實現。但具體而言,您會將您的十六進制解碼爲一個字節字符串,並在將'keyPtr'傳遞給'CCCrypt'的地方傳遞給它。你的密鑰長度應該是16個字節。 – 2012-07-07 12:17:38

+0

@HotLicks我在iOS 7上的AES128EncryptWithKey行爲與iOS 8/9的行爲有所不同。它在多大程度上「破碎」? – Drakes 2015-08-06 06:24:27

回答

0

您可以使用NSScanner,像這樣:

NSString *keyStr = @"0x11,0x12,0x13,0x14,0x13,0x1a,0x1b,0x1c,0x1f,0x11,0x11,0x11,0x11,0x11,0x11,0xfe"; 
NSScanner *scan = [NSScanner scannerWithString:keyStr]; 
[scan setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@","]]; 
unsigned int tmp; 
char key[16]; 
for (int i = 0 ; i != 16 ; i++) { 
    [scan scanHexInt:&tmp]; 
    key[i] = (char)tmp; 
} 

此時,陣列key包含16個字節的密鑰從十六進制轉換爲字節。

+0

謝謝他們的工作...... – Satheesh 2012-07-09 09:52:15