-1

请找到我们正在使用的以下方法。

从 Hexa 中的服务器接收到的密钥:1EF5F2D644FBA8E4142778CD43D70BFA

从服务器收到的编码消息:/lGtPU2K29FZPNtrc9DKow==

如果我们解密上面的消息,它应该是“hi”

我无法在 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;
}
4

1 回答 1

0

你可以试试这个:

CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, 
                                      kCCAlgorithmAES128, 
                                      kCCOptionECBMode | kCCOptionPKCS7Padding,
                                      keyPtr, 
                                      kCCKeySizeAES128,
                                      NULL /* initialization vector (optional) */,
                                      [self bytes], dataLength, /* input */
                                      buffer, bufferSize, /* output */
                                      &numBytesDecrypted);
于 2012-09-05T10:24:07.860 回答