Как сделать персональный алгоритм для хеширования в Stack Overflow

Я хочу сделать персональный алгоритм для хеширования текстов в PHP. Буква «a» склепа в «xyz», «b» в «256» и некоторые другие. Как это возможно?

0

Решение

Мне скучно на работе, поэтому я подумал, что смогу это сделать.
Это совсем не безопасно. Склеп должен быть жестко закодирован, а зашифрованный персонаж должен иметь размер 3.

<?php
//define our character->crypted text
$cryptArray = array( "a"=>"xyz","b"=>"256");
//This is our input
$string = "aab";

//Function to crypt the string
function cryptit($string,$cryptArray){
//create a temp string
$temp = "";
//pull the length of the input
$length = strlen($string);
//loop thru the characters of the input
for($i=0; $i<$length; $i++){
//match our key inside the crypt array and store the contents in the temp array, this builds the crypted output
$temp .= $cryptArray[$string[$i]];
}
//returns the string
return $temp;

}

//function to decrypt
function decryptit($string,$cryptArray){
$temp = "";
$length = strlen($string);
//Swap the keys with data
$cryptArray = array_flip($cryptArray);
//since our character->crypt is count of 3 we must $i+3 to get the next set to decrypt
for($i =0; $i<$length; $i = $i+3){
//read from the key
$temp .= $cryptArray[$string[$i].$string[$i+1].$string[$i+2]];
}
return $temp;
}

$crypted =  cryptit($string,$cryptArray);
echo $crypted;

$decrypted = decryptit($crypted,$cryptArray);
echo $decrypted;

Вход был: aab

Выход:
xyzxyz256
aab

Вот ссылка на 3v4l:
https://3v4l.org/chR2A

0

Другие решения

Это можно просто создать функцию, которая заменяет символы, например:

function myEncrypt ($text)
{
$text = str_replace(array('a', 'b'), array('xby', '256'), $text);
// ... others

return $text;
}

Версия с двумя массивами «search» и «replaceWith» передана в качестве аргументов:

function myEncrypt ($text, $search=array(), $replaceWith=array())
{
return str_replace($search, $replaceWith, $text);
}

ВНИМАНИЕ: этот способ не является правильным решением для шифрования текста, Есть много лучших способов сделать безопасное шифрование с помощью PHP (см., например, эта почта).

1

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector