PHP iOS AES 加密
我在尝试使用 AES 加密在 PHP 和我的 iOS 应用程序之间进行通信时遇到了问题。
到目前为止,我已经考虑了两种实现方法。第一个是使用 OpenSSL。
在 iOS 方面,我以模仿此处显示的代码的方式实现: http ://saju.net.in/code/misc/openssl_aes.c.txt。
在 PHP 方面,我获取了生成的密钥和 IV(来自 iPhone),并将其用作 PHP openssl 加密的输入。
结果在输出方面有所不同...
我也考虑过: http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html
但是这个帖子:iOS 和 PHP 之间的 AESCrypt 解密 让我望而却步。
该项目并不局限于 AES,它只是看起来是一种强大的加密算法,实现起来并不太难。
我的基本问题是:实现可轻松用于 iOS 和 PHP 之间通信的良好加密算法的最简单方法是什么?
I've been having trouble trying to communicate between PHP and my iOS application using AES encryption.
So far, I've considered two methods of implementation. The first was to use OpenSSL.
On the iOS side, I implemented in a way to mimic the code shown here: http://saju.net.in/code/misc/openssl_aes.c.txt.
On the PHP side, I took the generated key and IV (from the iPhone) and used it as input to the PHP openssl encrypt.
The results differed in terms of the output...
I have also considered: http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html
but this SO post: AESCrypt decryption between iOS and PHP deterred me.
The project is not tied down to AES, it just seemed like a strong encryption algorithm that wouldn't be too hard to implement.
My basic question is: what is the easiest way to implement a good encryption algorithm that can easily be used to communicate between iOS and PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚完成了同样的项目。我使用了您在“也考虑过...”中引用的库
,这是一些使用 php 解密的示例代码:
确保您的密钥都是 256 位(32 个字符,我还没有遇到任何编码问题,但如果您这样做,请记住您正在加密字节,而不是字符)。请注意,MCRYPT_RIJNDAEL_128 中的 128 是块大小而不是密钥大小,而在方法 AES256DecryptWithKey 中,256 是对密钥大小的引用,而块大小为 128。 AES256DecryptWithKey 在 CBC 模式下运行,但具有空初始化向量(四)。
CBC 意味着每个块都依赖于最后一个块,因此它使用一个预设的(通常是随机的)“块 -1”,称为 IV
ECB 意味着每个块都以相同的方式加密,因此它揭示了两个块何时存在相同的消息是相同的。提到的库没有使用它,所以我提到它只是为了对比。
使用零 iv(0000000000000000 字节)被认为是不安全的。要解决此问题,您必须为 IV 创建一个 NSData *iv 变量,并修改 NSData+AESCrypt.m 的 CCcrypt 参数,为 iv 参数添加 [iv bytes] (我尚未测试此代码),您将需要存储这个 iv 并将其与您的消息一起传递给 php。但首先我会测试并让一切在零静脉注射下正常工作。
I just got through this same sort of project. I used the library you referenced in "also considered..."
Here is some example code to decrypt with php:
Make sure your keys are both 256-bit (32 characters, I have not yet had any encoding issues, but if you do, remember that you are encrypting bytes, not characters). Note that 128 in MCRYPT_RIJNDAEL_128 is the block size and not the key size, while in the method AES256DecryptWithKey, 256 is a reference to the key size, while the block size is 128. AES256DecryptWithKey runs in CBC mode, but has a null initialization vector (iv).
CBC means that each block depends on the last block, and so it uses a pre-set, usually random, "block -1" called the IV
ECB means that each block is encrypted in the same way, hence it reveals when two blocks in the same message are the same. The library mentioned does not use it, so I mentioned it just for contrast.
The use of a zero iv (0000000000000000 in bytes) is considered insecure. To fix this you would have to create an NSData *iv variable for the IV and modify the CCcrypt argument of NSData+AESCrypt.m to add [iv bytes] for the iv parameter (I have not yet tested this code), and you would need to store this iv and pass it to the php along with you message. But first I would test and have everything working with a zero iv.
正如评论中所述,使用 HTTPS 可能是最简单的。
我曾经设置过一个 iPhone 应用程序,该应用程序必须通过 HTTPS 与 PHP 后端进行通信,并花了很多时间试图找出 iPhone 不接受加密连接的原因。
事实证明,它不起作用,因为我在服务器端使用自签名证书。从证书颁发机构购买 SSL 证书解决了所有问题。
在没有公司或扩展验证的情况下验证单个域名的 SSL 证书非常便宜,所以我建议您尝试一下!
As said in the comments, it would probably easiest for you to use HTTPS.
I once set up an iPhone app that had to communicate with a PHP backend over HTTPS, and spent many hours trying to find out why the iPhone wouldn't accept the encrypted connection.
As it turned out, it didn't work because I was using a self-signed certificate on the server side. Buying an SSL certificate from a Certificate Authority solved all issues.
SSL certificates that validate a single domain name without company or extended validation are really cheap, so I suggest you give that a try!
举个直接的例子,我的开源项目“Techno Tap”包含成功使用AES加密的PHP和iOS源代码,请随意查看 这里
iOS 上的加密是在 ScoreboardManager.m 中完成的(使用 NSData+AES),解密是在 PHP 端的 Scoreboard.php 中完成的
For a direct example, my open source project "Techno Tap" contains PHP and iOS source that uses AES encryption successfully, feel free to take a look here
The encryption on iOS is done in ScoreboardManager.m (using NSData+AES) and decryption is done on the PHP side in Scoreboard.php