drctyr2869 2017-05-23 09:59
浏览 133
已采纳

如何使用libsodium php使用salt加密/解密密码

What i am trying to get is

  • encrypt a password + salt with libsodium
  • store it to a database
  • read it and decrypt it (getting the plain password back for authentication)

I got a list of salts that i want to use to encrypt / decrypt my password. When i encrypt the password i get a hash back so that one seems to work but at decrypting i always get false as return value.

Am i using the wrong methods for encrypt / decrypt with libsodium or am i completely driving in the wrong direction?

My source for encrypt / decrypt:

function encrypt_libsodium($to_encrypt, $salt_to_use){
        if(!$data || !$salt_to_use){
            return null;
        }

        //get stored salt
        $this->key_ = substr(md5($this->pw_key[$salt_to_use].'_'), 0, $this->ks);

        //some libsodium specific stuff
        $out_len = \Sodium\CRYPTO_SIGN_SEEDBYTES;
        $ops_limit = \Sodium\CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE;
        $mem_limit =\Sodium\CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE;

        //create hash using libsodium
        $hash = \Sodium\crypto_pwhash($out_len, $to_encrypt, $this->key_,$ops_limit, $mem_limit);
        return $hash;
    }

    function decrypt_libsodium($hash, $salt_to_use){
        if(!$hash || !$what){
            return null;
        }

        //get stored salt
        $this->key_ = substr(md5($this->pw_key[$salt_to_use].'_'), 0, $this->ks);

        //get verification hash
        $decrypted = \Sodium\crypto_pwhash_scryptsalsa208sha256_str_verify($this->key_, $hash);
        return $decrypted;
    }

I appreciate any help!

regards Dom

  • 写回答

2条回答 默认 最新

  • down101102 2017-05-23 11:08
    关注

    If you dont need specifically use libsodium, with this function you should be able to store the data encrypted in the database and decrypt it.

    define("ENCRYPT_METHOD", "AES-256-CBC");
    define("SECRET_KEY","randomtextrandomtextforthesecretkey");
    define("SECRET_IV", "randomtextforthesecretiv");
    function encriptar($action, $string)
    {
      $output = false;
      $key    = hash("sha256", SECRET_KEY);
      $iv     = substr(hash("sha256", SECRET_IV), 0, 16);
    
      if ($action == "encrypt")
      {
        $output = openssl_encrypt($string, ENCRYPT_METHOD, $key, 0, $iv);
        $output = base64_encode($output);
      }
      else if($action == "decrypt")
        {
            $output = base64_decode($string);
            $output = openssl_decrypt($output, ENCRYPT_METHOD, $key, 0, $iv);
        }
      return $output;
    }
    

    The output will be the data you will store/get to database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料