douzhao2047 2016-12-28 17:36
浏览 45
已采纳

加密/解密数据php

I'm trying to encrypt/decrypt data from a form.I took the class that helps me to do that from:

https://github.com/o/crypt-php

The encrypt works but when I try to decrypt it gives me an error saying that:

Uncaught exception 'Exception' with message 'The given data does not appear to be encrypted with Crypt'

My code:

  //for encrypt(this works)
  require_once("Crypt.php");

  $crypt = new Crypt;
  $crypt->setKey('keykeykeyy');
  $password = $_POST['password'];
  $crypt->setData($password);
  $encrypted = $crypt->encrypt();


  //for decrypt almost the same thing
  $password = $_POST['password'];
  $crypt->setData($password);
  $decrypted = $crypt->decrypt();

What's the problem?

  • 写回答

1条回答 默认 最新

  • doushuangdui5419 2016-12-28 17:44
    关注

    The problem is this lines

      //for decrypt almost the same thing
      $password = $_POST['password'];
      $crypt->setData($password);
      $decrypted = $crypt->decrypt();
    

    you are setting data for decrypt the raw password instead that you need pass as data the password encrypted

      $crypt->setData($encrypted);
      $decrypted = $crypt->decrypt();
    

    regards

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

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题