问题描述
我有一个问题, HTTP POST 请求加密& Objective-C 中写入 OS X应用程序,发送加密的(CocoaSecurity ) HTTP POST请求到服务器:
- (NSString *)secure
{
NSData * key = [@9eab87dc72b927c9dataUsingEncoding:NSASCIIStringEncoding];
NSData * iv = [@d6f8f85911c4d8d1dataUsingEncoding:NSASCIIStringEncoding];
CocoaSecurityResult * result = [CocoaSecurity aesEncrypt:@akey:key iv:iv];
return result.hexLower;
}
我正在加密
5219abd6c1439dc832ab512dae8cce80
保护函数processEncrypt()
b $ b {
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
任何人都可以告诉我,问题在哪里?
请注意,CocoaSecurity使用标准的PKCS#7填充( kCCOptionPKCS7Padding
),但mcrypt使用非标准/不安全的填充。您将需要删除PKCS#7填充您的PHP代码。您可以使用以下代码:
添加PKCS#7填充(php):
code> $ 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);
查看此查询详细信息。
I have an issue with HTTP POST requests encrypting & decrypting.
I have an OS X Application written in Objective-C which sends encrypted (CocoaSecurity) HTTP POST request to server:
- (NSString *)secure
{
NSData* key = [@"9eab87dc72b927c9" dataUsingEncoding:NSASCIIStringEncoding];
NSData* iv = [@"d6f8f85911c4d8d1" dataUsingEncoding:NSASCIIStringEncoding];
CocoaSecurityResult *result = [CocoaSecurity aesEncrypt:@"a" key:key iv:iv];
return result.hexLower;
}
and I am getting encryption
5219abd6c1439dc832ab512dae8cce80
Also I have a WEB Application written in PHP which decrypts sent request
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);
}
}
}
The encrypted message, at the end of request handling, is totally different from the decrypted one.
I am getting encryption
10967675e5cf70878ee063a73f2a8394
Until now I have found out, that this might be a PKCS#7
padding issue (PHP mcrypt library has null padding). I have tried to remove padding by changing CocoaSecurity.m
source and replacing kCCOptionPKCS7Padding
value to 0
. After this replacement, CocoaSecurity
raises exception Encrypt Error!
triggered by kCCAlignmentError
...
Could anyone tell me, where is the problem?
Note that CocoaSecurity uses standard PKCS#7 padding (kCCOptionPKCS7Padding
) but mcrypt uses non-standard/insecure null padding. You will need to remove the PKCS#7 padding in your php code. You can use the this code:
Add PKCS#7 padding (php):
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
Remove PKCS#7 padding (php):
$len = strlen($str);
$pad = ord($str[$len-1]);
$str = $strsubstr($str, 0, $len - $pad);
See this SO answer for detailed information.
这篇关于HTTP请求加密&解密失败与PHP& Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!