Jump to content

Recommended Posts

Hi guys.

 

I've looking for a way to make my own simple encrypt/decrypt function for my site.

Is just a replace "a" with "b" sort of crypt, and i know its a security risk. Its not for any important data :)

 

But my question is: is there any way to str_replace all characters at one time? if i have a sentence, example sbsbsb, and the code is:

 

$phrase  = "ababab";
$from = array("a", "b");
$too = array("b", "a");

$newphrase = str_replace($from, $too, $phrase);

 

RESULT: aaaaaa

and i want: bababa

 

Any suggestions? =)

Link to comment
https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/
Share on other sites

Try something like:

 

<?php

function encrypt($string) {

$find = array_merge(range('a', 'z'), range('A', 'Z')); 

$replace = array (

  0 => 'R',

  1 => 'x',

  2 => 'q',

  3 => 'i',

  4 => 'J',

  5 => 'j',

  6 => 'Z',

  7 => 'e',

  8 => 'P',

  9 => 'c',

  10 => 'L',

  11 => 'v',

  12 => 'm',

  13 => 'B',

  14 => 'V',

  15 => 'D',

  16 => 'w',

  17 => 'f',

  18 => 'Y',

  19 => 's',

  20 => 'O',

  21 => 'M',

  22 => 'E',

  23 => 'y',

  24 => 'a',

  25 => 'g',

  26 => 'I',

  27 => 'u',

  28 => 'p',

  29 => 'G',

  30 => 'C',

  31 => 'K',

  32 => 't',

  33 => 'n',

  34 => 'r',

  35 => 'X',

  36 => 'l',

  37 => 'Q',

  38 => 'S',

  39 => 'b',

  40 => 'k',

  41 => 'F',

  42 => 'A',

  43 => 'd',

  44 => 'W',

  45 => 'U',

  46 => 'h',

  47 => 'T',

  48 => 'N',

  49 => 'H',

  50 => 'o',

  51 => 'z',

);

$string = str_replace($find, $replace, $string);

return $string;

}

 

That $replace array was made with:

<?php

$let = range('a', 'z');

$uplet = range('A', 'Z');

$merge = array_merge($let, $uplet);

shuffle($merge);

var_export($merge);

?>

 

you can't really do it as one massive replace

 

i would probably do something like this

<?php
$phrase  = "abababDbbb";

$crypt = array(
"a" => "b", 
"b" => "c", 
"c" => "a"
);

$p = preg_split("//", $phrase);
//cleanup
unset($p[0]);
unset($p[count($p)]);

$newphrase = "";
foreach($p as $letter)
{
if(isset($crypt[$letter]))
{
	$newphrase .= $crypt[$letter];
}else{
	$newphrase .= $letter;
}
}
echo $newphrase;

?>

preg_replace is a bit slower however it will accept arrays

This is a small script I have that does something similar maybe you can get an idea.

<?php

/**
* @author achapman@unkwndesign.com
* @copyright 2008
**/

if($_GET['act'] == 'do')
{
$pattern['a'] = '/[a]/'; $replace['a'] = '[a A @]';
$pattern['b'] = '/[b]/'; $replace['b'] = '[b B I3 l3 i3]';
$pattern['c'] = '/[c]/'; $replace['c'] = '(?:[c C (]|[k K])';
$pattern['d'] = '/[d]/'; $replace['d'] = '[d D]';
$pattern['e'] = '/[e]/'; $replace['e'] = '[e E 3]';
$pattern['f'] = '/[f]/'; $replace['f'] = '(?:[f F]|[ph pH Ph PH])';
$pattern['g'] = '/[g]/'; $replace['g'] = '[g G]';
$pattern['h'] = '/[h]/'; $replace['h'] = '[h H]';
$pattern['i'] = '/[i]/'; $replace['i'] = '[i I l ! 1]';
$pattern['j'] = '/[j]/'; $replace['j'] = '[j J]';
$pattern['k'] = '/[k]/'; $replace['k'] = '(?:[c C (]|[k K])';
$pattern['l'] = '/[l]/'; $replace['l'] = '[l L 1 ! i]';
$pattern['m'] = '/[m]/'; $replace['m'] = '[m M]';
$pattern['n'] = '/[n]/'; $replace['n'] = '[n N]';
$pattern['o'] = '/[o]/'; $replace['o'] = '[o O 0]';
$pattern['p'] = '/[p]/'; $replace['p'] = '[p P]';
$pattern['q'] = '/[q]/'; $replace['q'] = '[q Q]';
$pattern['r'] = '/[r]/'; $replace['r'] = '[r R]';
$pattern['s'] = '/[s]/'; $replace['s'] = '[s S $ 5]';
$pattern['t'] = '/[t]/'; $replace['t'] = '[t T 7]';
$pattern['u'] = '/[u]/'; $replace['u'] = '[u U v V]';
$pattern['v'] = '/[v]/'; $replace['v'] = '[v V u U]';
$pattern['w'] = '/[w]/'; $replace['w'] = '[w W vv VV uu UU]';
$pattern['x'] = '/[x]/'; $replace['x'] = '[x X]';
$pattern['y'] = '/[y]/'; $replace['y'] = '[y Y]';
$pattern['z'] = '/[z]/'; $replace['z'] = '[z Z 2]';
$word = str_split(strtolower($_POST['word']));
$i=0;
while($i < count($word))
 {
 	if(!is_numeric($word[$i]))
	 {
	 	if($word[$i] != ' ' || count($word[$i]) < '1')
	 	 {
			$word[$i] = preg_replace($pattern[$word[$i]], $replace[$word[$i]], $word[$i]);
		 }
	 }
	$i++;
 }
//$word = "/" . implode('', $word) . "/";
echo implode('', $word);
}
?>

This is used to generate regex patters from user imput, we use this for admins to insert a 'bad' word into our filter database, it will catch all variations of a word (ie badword = b@duu0rd)

strtr()

 

<?php
$phrase  = "ababab";
$tran = array(
        "a" => "b",
        "b" => "a"
        );

$newphrase = strtr($phrase,$tran);
echo $newphrase;                         // bababa

?>

<?php
function Encrypt($text) {
$length=strlen($text);
for ($i = 0; $i < $length; $i++) {
$char = ord($text[$i]); //gets the ascii character number of the letter
$char1 = chr($char +1); //adds 1 onto the ascii charachter a=b f=g etc
$output .= $char1; //remakes the string
}
return $output;
}

$text = "hello world";
echo Encrypt($text); //prints out ifmmp!xpsme
?>

 

rewrite it slightly to decrypt the line by making it minus1

Thank you guys.

 

I've tried Barand's way, but i cant get it to work properly..

 

<?php

$phrase  = "test";

$tran = array(

        "a" => "u",

        "b" => "v",

"c" => "A",

        "d" => "ø",

        "e" => "å",

        "f" => "F",

        "g" => "B",

        "h" => "æ",

        "i" => "7",

        "j" => "Æ",

        "k" => "1",

        "l" => "L",

        "m" => "E",

        "n" => "5",

        "o" => "s",

        "p" => "8",

        "q" => "w",

        "r" => "l",

        "s" => "3",

        "t" => "Å",

        "u" => "Q",

"v" => "Y",

        "w" => "-",

        "x" => "*",

        "y" => "S",

        "z" => "V",

        "æ" => "!",

        "ø" => "n",

        "å" => "y",

        "A" => "2",

        "B" => "H",

        "C" => ",",

        "D" => "t",

        "E" => "6",

        "F" => ".",

        "G" => "X",

        "H" => "R",

        "I" => "P",

        "J" => "T",

        "K" => "G",

        "L" => "K",

        "M" => "d",

        "N" => "z",

        "O" => "r",

        "P" => "Ø",

        "Q" => "M",

"R" => "h",

        "S" => ";",

        "T" => "c",

        "U" => "?",

        "V" => "m",

        "W" => "N",

        "X" => "J",

        "Y" => "q",

        "Z" => "o",

        "Æ" => "O",

        "Ø" => "f",

        "Å" => "0",

        "1" => "U",

        "2" => "p",

        "3" => "j",

        "4" => "a",

        "5" => "=",     

        "6" => ":",

        "7" => "g",

        "8" => "C",

        "9" => "_",

        "0" => "x",

        "!" => "4",

        "?" => "I",

        "=" => "i",

        "-" => "b",

        "_" => "k",

        "*" => "e",

        "/" => "D",

"." => "Z",

"," => "W",

":" => "/",

";" => "9"

      );

$newphrase = strtr($phrase,$tran);

echo $newphrase;

 

?>

It outputs this: Ã…Ã¥3Ã…

 

any suggestions?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.