数日間問題があり、修正方法がわかりません。Python-TornadoWebでNSDictionaryをサーバーに送信するアプリを作っています。さて、AES 256 で暗号化されたデータを送信しましたが、問題は復号化です。
暗号化用の Objective-C コード (キー = "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??? ??
ご覧のとおり、正しく復号化されていません。暗号化せずに送信すると、すべてが完全に機能します。
何か案は?