0

我有问题好几天了,不知道如何解决。我正在制作一个使用 Python-TornadoWeb 将 NSDictionary 发送到服务器的应用程序。好吧,我发送用 AES 256 加密的数据,问题是解密。

用于加密的 Objective-C 代码 (key = "0123456789abcdef")

// Copy the key data, padding with zeroes if needed
char key[kKeySize];
bzero(key, sizeof(key));
memcpy(key, keyData, keyLength > kKeySize ? kKeySize : keyLength);

size_t bufferSize = [self length] + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);

size_t dataUsed;

CCCryptorStatus status = CCCrypt(decrypt ? kCCDecrypt : kCCEncrypt,
                                 kCCAlgorithmAES128,
                                 kCCOptionPKCS7Padding,
                                 key, kKeySize,
                                 NULL,
                                 [self bytes], [self length],
                                 buffer, bufferSize,
                                 &dataUsed);

switch(status){
    case kCCSuccess:
        return [NSData dataWithBytesNoCopy:buffer length:dataUsed];
    case kCCParamError:
        NSLog(@"Error: NSDataAES256: Could not %s data: Param error", decrypt ? "decrypt" : "encrypt");
        break;
    case kCCBufferTooSmall:
        NSLog(@"Error: NSDataAES256: Could not %s data: Buffer too small", decrypt ? "decrypt" : "encrypt");
        break;
    case kCCMemoryFailure:
        NSLog(@"Error: NSDataAES256: Could not %s data: Memory failure", decrypt ? "decrypt" : "encrypt");
        break;
    case kCCAlignmentError:
        NSLog(@"Error: NSDataAES256: Could not %s data: Alignment error", decrypt ? "decrypt" : "encrypt");
        break;
    case kCCDecodeError:
        NSLog(@"Error: NSDataAES256: Could not %s data: Decode error", decrypt ? "decrypt" : "encrypt");
        break;
    case kCCUnimplemented:
        NSLog(@"Error: NSDataAES256: Could not %s data: Unimplemented", decrypt ? "decrypt" : "encrypt");
        break;
    default:
        NSLog(@"Error: NSDataAES256: Could not %s data: Unknown error", decrypt ? "decrypt" : "encrypt");
}

free(buffer);

好了,将加密后的数据发送到服务器并执行以下操作

class LoginHandler(tornado.web.RequestHandler):
  def post(self):

    # Body
    body = self.request.body
    print "--------------------------- Before decrypt ---------------------------"
    print body
    print "----------------------------------------------------------------------"

    # Decrypt
    key = '0123456789abcdef'
    mode = AES.MODE_CBC
    decryptor = AES.new(key, mode)
    bodyDecrypted = decryptor.decrypt(body)
    print "--------------------------- After decrypt ----------------------------"
    print bodyDecrypted
    print "----------------------------------------------------------------------"

结果是:

------------------------- 解密前 --------- ------

BU??c??? ?????d?PP??I?G?'??

------------------------- 解密后 --------- --------

?Ƒg,??e#??? ?(?>97R??? ??

如您所见,解密不正确。如果您发送未加密,则一切正常。

有任何想法吗?

4

1 回答 1

0

我会检查以确保 Objective-C 和 Python 之间的编码是相同的。在应用程序级别处理加密时,我喜欢以十六进制格式转换和发送所有内容,然后再次转换以确保编码不是问题。

顺便问一下,您使用的是哪个版本的 Python?(3.0+及以上使用unicode)

于 2012-07-25T09:35:33.593 回答