问题描述
从搜索全部,我可以找到一些引用类似问题的链接,但没有什么是相当的工作,它驱使我疯狂....
有人可以请给我一个关于如何在Objective C中编写以下PHP代码的分步指南。
我们正在创建一些需要加密内容的Web服务调用,而我们只给了PHP加密样本。要清楚我们不想解密,我们要在IOS中镜像此加密过程。
非常感谢
函数加密($ plainText){
$ initVector =mysite.com;
$ key = base64_decode(GfSDfXS14U8I3YglEFrMjMOOnESUMxN3wRTt1TeDosY =);
$ block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC);
$ padding = $ block - (strlen($ plainText)%$ block);
$ plainText。= str_repeat(chr($ padding),$ padding);
$ crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$ key,$ plainText,MCRYPT_MODE_CBC,$ initVector);
$ crypttext64 = base64_encode($ crypttext);
return $ crypttext64;
}
论坛。
PHP使用恶作剧加密IMO,尽管代码似乎在做最好的方式来避免恶作剧(例如, AFAICT填充代码实现PKCS#7填充)。您看到的问题很可能与PHP如何将字符串mysite.com翻译成有效的AES-128 IV(其长度必须与块大小,即16个字节必须匹配)有关。很有可能PHP的零(这似乎是PHP的方式),但是你必须在PHP方面研究这些东西。
除此之外看起来像Common Crypto的一个相当普遍的应用。您需要:
o传入kCCOptionPKCS7Padding标志
o不传入kCCOptionECBMode标志(所以你获得CBC)
o使用kCCAlgorithmAES128,kCCKeySizeAES128和kCCBlockSizeAES128
o做你自己的Base64 [1] / p>
关于最后一点,我建议你最后一件事。修改PHP代码打印$ key,$ plainText和$ crypttext作为十六进制转储,然后在iOS端使用它。一旦工作,然后添加Base64。这将问题分成两半,这意味着Base64问题不会与您的加密问题相融合。
最后,请记住,OS X有一个方便的Base64编码器和解码器在命令行上。例如,要获取样例中加密密钥的十六进制转储,您可以执行以下操作:
$ openssl enc -d -base64 | hexdump -C
GfSDfXS14U8I3YglEFrMjMOOnESUMxN3wRTt1TeDosY =
^ D
00000000 19 f4 83 7d 74 b5 e1 4f 08 dd 88 25 10 5a cc 8c ... ... t ... 0 ...%。 ž.. |
00000010 c3 8e 9c 44 94 33 13 77 c1 14 ed d5 37 83 a2 c6 | ... D.3.w .... 7 ... |
00000020
分享享受
感谢eskimo1
From searching all around, I can find a number of links referencing similar issues, but nothing is quite working, its driving me mad....
Can someone please give me a step by step guide on how to write the following PHP code in Objective C.
We are creating some web service calls, which require encrypted content, and we have only been given PHP encrypt sample. To be clear we do not want to un-encrypt, we want to mirror this encryption process in IOS.
Many thanks
function encrypt($plainText) {
$initVector = "mysite.com";
$key = base64_decode("GfSDfXS14U8I3YglEFrMjMOOnESUMxN3wRTt1TeDosY=");
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$padding = $block - (strlen($plainText) % $block);
$plainText .= str_repeat(chr($padding), $padding);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plainText, MCRYPT_MODE_CBC, $initVector);
$crypttext64 = base64_encode($crypttext);
return $crypttext64;
}
was answered here on apple dev forums.
https://devforums.apple.com/message/791166#791166
PHP uses wacky crypto IMO, although code seems to be doing it's best to avoid the wackiness (for example, AFAICT the padding code implements PKCS#7 padding). It's likely that the problem you're seeing relates to how PHP translates the string "mysite.com" into a valid AES-128 IV (whose length must necessarily match the block size, that is, 16 bytes). It's quite possible that PHP pads with zeroes (that seems to be the PHP way) but you'll have to research that on the PHP side of things.
Beyond that this looks like a fairly common application of Common Crypto. You need to:
o pass in the kCCOptionPKCS7Padding flag
o not pass in the kCCOptionECBMode flag (so you get CBC)
o use kCCAlgorithmAES128, kCCKeySizeAES128, and kCCBlockSizeAES128
o do your own Base64 [1]
With regards the last point, I recommend you do this last. Modify the PHP code to print the $key, $plainText, and $crypttext as a hex dump and then work with that on the iOS side. Once that's working, then add the Base64. That splits the problem in half, meaning that Base64 issues won't get conflated with your encryption issues.
Finally, keep in mind that OS X has a handy-dandy Base64 encoder and decoder on the command line. For example, to get a hex dump of the encryption key in your sample, you can do this:
$ openssl enc -d -base64 | hexdump -CGfSDfXS14U8I3YglEFrMjMOOnESUMxN3wRTt1TeDosY=^D00000000 19 f4 83 7d 74 b5 e1 4f 08 dd 88 25 10 5a cc 8c |...}t..O...%.Z..|00000010 c3 8e 9c 44 94 33 13 77 c1 14 ed d5 37 83 a2 c6 |...D.3.w....7...|00000020
Share and Enjoy
Thanks to eskimo1
这篇关于Objective-C版本的PHP mcrypt_encrypt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!