2012-09-05 35 views
-1

請找到下面的方法,我們使用的是iOS中的消息。從服務器接收任何一個可以幫助我如何解密(AES128),這在JAVA(AES解密128)進行加密

的關鍵在於六:1EF5F2D644FBA8E4142778CD43D70BFA

從服務器接收到的編碼信息:/ lGtPU2K29FZPNtrc9DKow ==

如果我們解密上述消息應該是「喜」

我無法解密它在iOS端。任何人都可以幫助我。

- (NSData *)AES128DecryptWithKey:(NSString *)key 
{ 
    // 'key' should be 16 bytes for AES128, 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]; 

    //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, 
              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

您可以添加加密文本Java代碼? – rayyildiz

+0

@rayyildiz:請在下面找到Java代碼。 –

回答

0

你可以試試這個:

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

我嘗試了您提供的代碼,但它沒有返回任何數據。但是我以前使用的代碼返回數據。但是將數據轉換爲字符串。我得到空字符串。 NSString * str = [[NSString alloc] initWithData:encodedData encoding:NSUTF8StringEncoding]; –

+0

Java代碼不使用任何模式和填充。 –

+0

喜的Sudhir,有你解決了這個問題,因爲我ASLO面對你上面提到的同樣的問題。如果您已解決此問題,請爲我提供代碼段。提前致謝。 –