douguanyan9928 2013-06-21 15:56
浏览 65
已采纳

PHP上的256加密和ios pannding的解密

I'm trying to encrypt strings on my server and send it to my iPhone.

When I'm encrypting on my iphone and send it to decryption on my server, every thing is OK.

But when I'm trying to encrypt on my server (php page) I can't decrypt it on my iPhone.

e.g.: When I encrypt "True" on my PHP, I am getting:

ca 82 66 c8 be 5a a9 fb d8 7f 25 b6 1e f0 fb 68 54 72 75 65 0 0 0 0 0 0 0 0 0 0 0 0

then I'm doing some - base64

yoJmyL5aqfvYfyW2HvD7aA== when im sending it to my iphone i cant decrypt it right ..

But when I'm trying to encrypt "True" on my Iphone I'm getting:

ca 82 66 c8 be 5a a9 fb d8 7f 25 b6 1e f0 fb 68 54 72 75 65 c c c c c c c c c c c c

base64-

fwqkKsopev3Xmu4BF4OE5Q==

Can someone guide me towards a solution?

  • 写回答

1条回答 默认 最新

  • duanjian9148 2013-06-22 18:39
    关注

    The difference is is the padding. The PHP solution does not perform a standardized padding mechanism. Instead it uses zero-padding, which can lead to trouble if the plain text ends with a string of 00h valued bytes as they will be removed from the plain text by the unpadding mechanism (or any trim or right_trim method for that matter).

    You should pad the plain text before encryption in PHP, just like CCCrypt does. CCCrypt uses a good standardized padding mode called PKCS#7 padding. This mode always pad 1 to 16 (blocksize) bytes. The value of the bytes are identical to the number of padding bytes. In your example it is 0Ch in hexadecimals, or 12 padding bytes.

    Check out this link to StackOverflow on how to pad/unpad in PHP. Padding/unpadding is always performed on the raw plain text, never on the ciphertext.

    Note that your ciphertext is probably pre-fixed with the IV value. These first 16 bytes should not be decrypted. They should be used as IV and skipped by the decrypt functions. Normally a new IV value should be created for each encryption. Otherwise you would be able to see if the (start of the) plaintext is identical to an earlier one. The random IV changes the entire ciphertext for each encryption.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面