PHP & Objective-C 的 HTTP 请求加密和解密失败
HTTP Request encrypt & decrypt failure with PHP & Objective-C
我遇到 HTTP POST 请求加密和解密的问题。
我有一个 OS X 应用程序 写在 Objective-C 中发送加密 (CocoaSecurity) 对服务器的 HTTP POST 请求:
- (NSString *)secure
{
NSData* key = [@"9eab87dc72b927c9" dataUsingEncoding:NSASCIIStringEncoding];
NSData* iv = [@"d6f8f85911c4d8d1" dataUsingEncoding:NSASCIIStringEncoding];
CocoaSecurityResult *result = [CocoaSecurity aesEncrypt:@"a" key:key iv:iv];
return result.hexLower;
}
我正在加密
5219abd6c1439dc832ab512dae8cce80
我还有一个 WEB 应用程序 用 PHP 编写,它解密发送的请求
protected function processEncrypt()
{
if ($this->input) {
$crypt = mcrypt_module_open($this->algorithm, '', $this->mode, $this->encryptIv);
mcrypt_generic_init($crypt, $this->encryptKey, $this->encryptIv);
$this->input = mcrypt_generic($crypt, $this->input);
mcrypt_generic_deinit($crypt);
mcrypt_module_close($crypt);
if ($this->template == 'hex') {
$this->input = bin2hex($this->input);
} elseif ($this->template == 'base64') {
$this->input = base64_encode($this->input);
}
}
}
请求处理结束时的加密消息与解密消息完全不同。
我正在加密
10967675e5cf70878ee063a73f2a8394
直到现在我才发现,这可能是一个 PKCS#7
填充问题(PHP mcrypt 库有空填充)。我试图通过更改 CocoaSecurity.m
源并将 kCCOptionPKCS7Padding
值替换为 0
来删除填充。替换后,CocoaSecurity
引发由 kCCAlignmentError
...
触发的异常 Encrypt Error!
谁能告诉我,问题出在哪里?
请注意,CocoaSecurity 使用标准 PKCS#7 填充 (kCCOptionPKCS7Padding
),但 mcrypt 使用 non-standard/insecure 空填充。您需要删除 php 代码中的 PKCS#7 填充。您可以使用此代码:
添加 PKCS#7 填充 (php):
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
删除 PKCS#7 填充 (php):
$len = strlen($str);
$pad = ord($str[$len-1]);
$str = $strsubstr($str, 0, $len - $pad);
查看此SO answer了解详细信息。
我遇到 HTTP POST 请求加密和解密的问题。
我有一个 OS X 应用程序 写在 Objective-C 中发送加密 (CocoaSecurity) 对服务器的 HTTP POST 请求:
- (NSString *)secure
{
NSData* key = [@"9eab87dc72b927c9" dataUsingEncoding:NSASCIIStringEncoding];
NSData* iv = [@"d6f8f85911c4d8d1" dataUsingEncoding:NSASCIIStringEncoding];
CocoaSecurityResult *result = [CocoaSecurity aesEncrypt:@"a" key:key iv:iv];
return result.hexLower;
}
我正在加密
5219abd6c1439dc832ab512dae8cce80
我还有一个 WEB 应用程序 用 PHP 编写,它解密发送的请求
protected function processEncrypt()
{
if ($this->input) {
$crypt = mcrypt_module_open($this->algorithm, '', $this->mode, $this->encryptIv);
mcrypt_generic_init($crypt, $this->encryptKey, $this->encryptIv);
$this->input = mcrypt_generic($crypt, $this->input);
mcrypt_generic_deinit($crypt);
mcrypt_module_close($crypt);
if ($this->template == 'hex') {
$this->input = bin2hex($this->input);
} elseif ($this->template == 'base64') {
$this->input = base64_encode($this->input);
}
}
}
请求处理结束时的加密消息与解密消息完全不同。
我正在加密
10967675e5cf70878ee063a73f2a8394
直到现在我才发现,这可能是一个 PKCS#7
填充问题(PHP mcrypt 库有空填充)。我试图通过更改 CocoaSecurity.m
源并将 kCCOptionPKCS7Padding
值替换为 0
来删除填充。替换后,CocoaSecurity
引发由 kCCAlignmentError
...
Encrypt Error!
谁能告诉我,问题出在哪里?
请注意,CocoaSecurity 使用标准 PKCS#7 填充 (kCCOptionPKCS7Padding
),但 mcrypt 使用 non-standard/insecure 空填充。您需要删除 php 代码中的 PKCS#7 填充。您可以使用此代码:
添加 PKCS#7 填充 (php):
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
删除 PKCS#7 填充 (php):
$len = strlen($str);
$pad = ord($str[$len-1]);
$str = $strsubstr($str, 0, $len - $pad);
查看此SO answer了解详细信息。