2014-05-13 43 views
2

在我的項目中,我實現了AES 128 CBC加密。我正在使用類別,並基於NSData。這是我的加密代碼:iOS AES加密 - 無法加密

- (NSData*)AES128Decrypt 
{ 
    char ivPtr[kCCKeySizeAES128 + 1]; 
    bzero(ivPtr, sizeof(ivPtr)); 

    // fetch iv data 
    [iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding]; 


    // 'key' should be 32 bytes for AES256, will be null-padded otherwise 
    char keyPtr[kCCKeySizeAES128 + 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]; // dataLength = 19 

    //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, 0, 
              keyPtr, kCCKeySizeAES128, 
              ivPtr, 
              [self bytes], dataLength, 
              buffer, bufferSize, 
              &numBytesDecrypted); // buffer = 0 & numBytesDecrypted = 0 

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

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

這就是我如何從我的視圖類調用它:

- (void) testActuallyEncrypting :(NSString*) hexString { 
    NSLog(@"String to Encrypt : %@", hexString); // prints test12 

    @try { 
    //Convert NSString to NSData 
    NSData *data = [self dataFromHexString:hexString]; // [hexString dataUsingEncoding:NSUTF8StringEncoding]; // 
    // // Prepare the NSDAta obj to store the encrypted pswd 
    NSData *encryptedData = [NSData dataWithBytes:[data bytes] length:[data length]]; // 6bytes 
    NSData *decryptedData = [encryptedData AES128Decrypt]; // 0bytes 
    NSString *decryptedString = [NSString stringWithUTF8String:[decryptedData bytes]]; // NULL Exception 
    NSLog(@"Decrypted String : %@", decryptedString); 

    decryptedString = [self addPaddingToString:decryptedString]; 
    decryptedData = [NSData dataWithBytes:[decryptedString UTF8String] length:[[decryptedString dataUsingEncoding:NSUTF8StringEncoding] length]]; 
    encryptedData = [decryptedData AES128Encrypt]; 
    if (encryptedData!=nil) 
    { 
     NSString *encryptedHexString = [self hexStringFromData:encryptedData]; 
     NSLog(@"Encrypted HexString : %@",encryptedHexString); 

    } 
    }@catch (NSException *ex) { 
     NSLog(@"Exception : %@", ex); 
    } 
} 

我傳遞"test12"字符串進行加密。在撥打AES128Decrypt時,decryptedData爲0,因此下一行decryptedString會拋出Null異常 - Exception : *** +[NSString stringWithUTF8String:]: NULL cString

任何人都可以幫助我知道爲什麼解密數據爲空。我在AES128Decrypt方法中哪裏出錯?

請幫幫我。我從最近2天就陷入了這個問題。在互聯網上搜索了很多,但無法獲得任何解決方案。 任何幫助是高度讚賞。謝謝。

更新: - 在我的課堂上添加了@Zaph的方法,我正在調用它。

NSLog(@"String to Encrypt : %@", hexString); 
NSString *iv = @"fedcba"; 
NSString *key = @"abcdef"; 

// Convert str to encrypt, iv & key from NSString to NSData 
NSData *dataIn = [hexString dataUsingEncoding:NSUTF8StringEncoding]; 
NSData *ivData = [iv dataUsingEncoding:NSUTF8StringEncoding]; 
NSData *symKey = [key dataUsingEncoding:NSUTF8StringEncoding]; 
NSError *error; 

NSData *result = [LoginViewController doCipher:dataIn iv:ivData key:symKey context:kCCEncrypt error:&error]; // result = 16bytes 

if (result != nil) { 
    // Convert result to satring 
    NSString *resultStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; 
    NSLog(@"Encrypted Str = %@", resultStr); // Encrypted Str = (null) ???? 

爲什麼轉換後的字符串爲空?請任何幫助。謝謝

+2

不要使用'@ try'和'@ catch',除非發現編程錯誤。在Objective-C中,它們不是**控制結構。 – zaph

+0

我發現將NSData轉換爲NSString - 使用NSUTF8StringEncoding編碼返回null。我試過使用NSASCIIStringEncoding,它返回值。但字符串值和數據值是不同的。他們都應該是相同的嗎? – Tvd

+0

並非所有的數據也是有效的UTR-8。對於數據的字符串表示,通常的慣例是使用Base64編碼。而不是'NSString * resultStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];'使用base64編碼:'NSString * resultBase64Str = [result base64EncodedStringWithOptions:0];'如果您使用'NSASCIIStringEncoding',您將不可顯示字符,可能不是你想要的。 – zaph

回答

3

有沒有必要使加密如此複雜,這裏是一個基本的加密/解密方法。 iv和鍵必須是正確的長度。值的上下文是kCCEncryptkCCDecrypt

+ (NSData *)doCipher:(NSData *)dataIn 
        iv:(NSData *)iv 
       key:(NSData *)symmetricKey 
      context:(CCOperation)encryptOrDecrypt 
       error:(NSError **)error 
{ 
    CCCryptorStatus ccStatus = kCCSuccess; 
    size_t   cryptBytes = 0; 
    NSMutableData *dataOut = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128]; 

    ccStatus = CCCrypt(encryptOrDecrypt, 
         kCCAlgorithmAES128, 
         kCCOptionPKCS7Padding, 
         symmetricKey.bytes, 
         kCCKeySizeAES128, 
         iv.bytes, 
         dataIn.bytes, 
         dataIn.length, 
         dataOut.mutableBytes, 
         dataOut.length, 
         &cryptBytes); 

    if (ccStatus == kCCSuccess) { 
     dataOut.length = cryptBytes; 
    } 
    else { 
     if (error) { 
      *error = [NSError errorWithDomain:@"kEncryptionError" 
             code:ccStatus 
            userInfo:nil]; 
     } 
     dataOut = nil; 
    } 

    return dataOut; 
} 
+0

感謝上面的代碼。我將它添加到我的viewController類中。通過NSData形式(iv和16字節的密鑰)中的iv,key,str並調用方法 - NSData * result = [LoginViewController doCipher:dataIn iv:ivData key:symKey context:kCCEncrypt error:&error];但是我得到的結果是16bytes,當我將它轉換爲NSString時它是null。你可以請檢查上面的代碼 - 已添加此更新下。錯誤也是零 - 所以沒有錯誤。爲什麼字符串爲空?你能幫我找到嗎? – Tvd

+0

關於獲取結果,我把它轉換爲字符串(它通過NSASCIIStringEncoding只工作,然後我試圖將字符串轉換回nsdata。我得到不同的值爲3他們不應該是相同或至少兩個nsdata 。毫無疑問,解密結果和轉換爲字符串我得到原來的字符串傳遞和它的工作。但想知道這是知識,並且好奇知道相同。謝謝。 – Tvd

+0

在你的代碼中,你已經使用kCCOptionPKCS7Padding,我認爲這將用於PKCS的實現我必須使用CBC任何CBC的特殊設置/參數值 – Tvd