2014-12-27 191 views
0

我正在使用AES加密算法(ECB)&使用ASI http庫,但由於401未經授權,我的請求已失敗。 我已在下面附上我的代碼。請求失敗Asi HTTP

加密ALGO。(AES 128加密)

+ (NSData*)encryptData:(NSData*)data key:(NSData*)key iv:(NSData*)iv; 
{ 
    NSData* result = nil; 

    // setup key 
    unsigned char cKey[FBENCRYPT_KEY_SIZE]; 
    bzero(cKey, sizeof(cKey)); 
    [key getBytes:cKey length:FBENCRYPT_KEY_SIZE]; 

    // setup iv 
    char cIv[FBENCRYPT_BLOCK_SIZE]; 
    bzero(cIv, FBENCRYPT_BLOCK_SIZE); 
    if (iv) { 
     [iv getBytes:cIv length:FBENCRYPT_BLOCK_SIZE]; 
    } 

    // setup output buffer 
    size_t bufferSize = [data length] + FBENCRYPT_BLOCK_SIZE; 
    void *buffer = malloc(bufferSize); 

    // do encrypt 
    size_t encryptedSize = 0; 

///CCCryptorStatus cryptStatus2 = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, [data bytes], [data length], NULL, [cipherData bytes], outLength, [decodedData mutableBytes], [decodedData length], &outLength); 

    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, 
              FBENCRYPT_ALGORITHM, 
              kCCOptionPKCS7Padding | kCCOptionECBMode, 
              cKey, 
              FBENCRYPT_KEY_SIZE, 
              cIv, 
              [data bytes], 
              [data length], 
              buffer, 
              bufferSize, 
              &encryptedSize); 
    if (cryptStatus == kCCSuccess) { 
     result = [NSData dataWithBytesNoCopy:buffer length:encryptedSize]; 
    } else { 
     free(buffer); 
     NSLog(@"[ERROR] failed to encrypt|CCCryptoStatus: %d", cryptStatus); 
    } 

    return result; 
} 

解密ALGO。(AES解密128)

+ (NSData*)decryptData:(NSData*)data key:(NSData*)key iv:(NSData*)iv; 
{ 
    NSData* result = nil; 

    // setup key 
    unsigned char cKey[FBENCRYPT_KEY_SIZE]; 
    bzero(cKey, sizeof(cKey)); 
    [key getBytes:cKey length:FBENCRYPT_KEY_SIZE]; 

    // setup iv 
    char cIv[FBENCRYPT_BLOCK_SIZE]; 
    bzero(cIv, FBENCRYPT_BLOCK_SIZE); 
    if (iv) { 
     [iv getBytes:cIv length:FBENCRYPT_BLOCK_SIZE]; 
    } 

    // setup output buffer 
    size_t bufferSize = [data length] + FBENCRYPT_BLOCK_SIZE; 
    void *buffer = malloc(bufferSize); 

    // do decrypt 
    size_t decryptedSize = 0; 
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, 
              FBENCRYPT_ALGORITHM, 
              kCCOptionECBMode, 
              cKey, 
              FBENCRYPT_KEY_SIZE, 
              cIv, 
              [data bytes], 
              [data length], 
              buffer, 
              bufferSize, 
              &decryptedSize); 

    if (cryptStatus == kCCSuccess) { 
     result = [NSData dataWithBytesNoCopy:buffer length:decryptedSize]; 
    } else { 
     free(buffer); 
     NSLog(@"[ERROR] failed to decrypt| CCCryptoStatus: %d", cryptStatus); 
    } 

    return result; 
} 

基本64 ALGO。

- (NSString *)base64EncodedString 
{ 
    size_t outputLength; 
    char *outputBuffer = 
     NewBase64Encode([self bytes], [self length], true, &outputLength); 

    NSString *result = 
     [[[NSString alloc] 
      initWithBytes:outputBuffer 
      length:outputLength 
      encoding:NSASCIIStringEncoding] 
     autorelease]; 
    free(outputBuffer); 
    return result; 
} 

請確實需要。 在此先感謝

+0

什麼讓你認爲問題是加密?數據輸入和輸出的密鑰的十六進制轉儲在哪裏? ECB實際上並不安全,尤其是對於圖像而言,但代碼設置爲iv。爲什麼你甚至在作者建議不使用ASI時使用ASI,AFNetworking是最新的並且正在積極維護。爲什麼Apple提供第三方Base64代碼?你爲什麼不使用ARC? – zaph

+0

我想大部分問題都可以通過選擇第三方項目來選擇,選擇更好,更新。 RNCryptor是最新的,可能適合您的需求。 – zaph

+0

感謝您的回覆,但已經實施了ECB在PHP方面,所以我不能使用CBC,否則PHP服務器無法解密我的請求。 – user1066642

回答

0

這是一個基本的加密/解密方法。鑰匙必須是正確的長度。值的上下文是kCCEncryptkCCDcrypt

將字符串轉換爲數據並加密/解密數據,然後轉換回所需的任何格式。對於Base64編碼,使用NSData提供的方法。

在問題PKCS7Padding被用於加密但不是在解密,它需要在兩個相同。

+ (NSData *)doCipher:(NSData *)dataIn 
       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 | kCCOptionECBMode, 
         symmetricKey.bytes, 
         kCCKeySizeAES128, 
         nil, 
         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

我已經嘗試過你的方法,但仍然有相同的響應請求失敗:( – user1066642

+0

得到這個。狀態碼:401,狀態消息:HTTP/1.1 401未授權 – user1066642

+0

你需要真正檢查API,究竟需要什麼 – zaph