2012-07-03 13 views
1

我一直在解密一段時間,不能讓它工作。當我使用加密下面的代碼:地穴狀態不等於kCCSuccess

private static string Decrypt(string plainText, string completeEncodedKey, int keySize) 
    { 
    RijndealManaged aesEncryption = new RijndealManaged(); 
    aesEncryption.KeySize = keySize; //keySize is 256 
    aesEncryption.BlockSize = 128; 
    aesEncryption.Mode = CipherMode.CBC; 
    aesEncryption.Padding = PaddingMode.PKCS7; 
    aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.ACSII.GetString(Convert.FromBase64String(completeEncodedString)).Split(',')[0]); 
    aesEncryption.Key = Convert.FromBase64String(ASCIIEncoding.ACSII.GetString(Convert.FromBase64String(completeEncodedString)).Split(',')[1]); 
    byte[] plainText = Encoding.UTF8.GetBytes(plainStr); 
    ICryptoTransform crypto = aesEncryption.CreateEncryptor(); 
    byte[] cipherText = crypto.TransformFinalBlock(plainText, 0, plainText.Length); 
    return Convert.ToBase64String(cipherText); 
    } 

傳遞名爲「安東尼」的明文我得到uRO2DBKAhFsOed/p10dz+w==

,我使用解密

-(NSData *)AES256DecryptWithKey:(NSString *)key{ 
// '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; 
} 

但我得到什麼回報。代碼到達if(cryptStatus == kCCSuccess){行,它不會進入if語句。所以解密返回nil

任何有關爲什麼這不起作用的幫助將是偉大的。謝謝。

+0

如果刪除if語句會發生什麼? – Dustin

+0

什麼都沒有。即使我刪除了if語句,也沒有任何東西出現。 @Dustin Rowland – BigT

+0

然後檢查你的緩衝區 – Dustin

回答

0

所以我認爲你有一對夫婦的問題在這裏:

  1. 您要加密時指定的IV,但解密,當你離開它 NULL。儘管IV是可選的,但您必須使用同一個進行加密和解密。

  2. 我敢肯定,當解密密碼時,你不能正確解析密鑰。從我可以告訴,你似乎在服務器上使用 雙base64編碼的字符串(出於某種原因)。在 至少,這是我看你用它做:

    1. Base64 decoding the string. 
    
    2. Splitting the resulting string on ','. 
    
    3. Taking the first part of the split, base64 decoding it again to use as the IV. 
    
    4. Taking the second part of the split, base64 decoding it again to use as the key. 
    

    你沒有做這些事情的OBJ - C的代碼。假設你以同樣的格式獲得密鑰,你需要。你在Obj-C代碼中做的所有事情都是把NSString對象轉換成一個C字符串。

+0

如何在服務器上使用base64編碼?我認爲這是'Convert.FromBase64String'做了什麼。 – BigT

+0

是的,你是base64解碼服務器上的密鑰。但是你不是base64解碼Objective-C代碼中的密鑰。 –

+0

因此,而不是'NSUTF8Encoding'使用base64編碼。有沒有這樣的事情? – BigT