使用固定长度的salt进行字符串加密


String encryption with salt of fixed length

我需要创建一个密码生成方法,其中用户传入一个字符串并获得使用SALT加密的密码。当用户输入相同的字符串时,他们会得到相同的密码。

<?php
$textToEncrypt = "String passed by user";
$encryptionMethod = "AES-256-CBC"; 
$secretHash = "25c6c7ff35b9979bdsfdsgsdfsdf";
//To encrypt
$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash);
//To Decrypt
$decryptedMessage = openssl_decrypt($encryptedMessage, $encryptionMethod, $secretHash);
//Result
echo "Encrypted: $encryptedMessage'n";
echo "Decrypted: $decryptedMessage'n";

我需要有一个长度为16或10的密码,并有字母数字和或特殊字符参数的选项。

我建议使用现有的方法。如果您使用自己的密码,则有许多方法可以错误地使其加密不安全(在这种情况下,特别是通过定时或功率差异攻击,这是不太可能的,但仍然)。这里有一个方法看起来很适合你的问题:

string hash_pbkdf2 ( string $algo , string $password , string $salt , int $iterations [, int $length = 0 [, bool $raw_output = false ]] )

这是整个链接