I have been working this on and off for a while to encrypt data strings. Seems to work fine except Upper case letters are not encrypted, just stay as Upper case letters. It's got to be something simple, I just don't see it.

Any help would be appreciated.

<?php
// ASCHII codes are here : http://www.ascii-code.com/
// Create an email looking string for example to test mix of characters
$teststr = "User.Email@SomeWhere-OutThere1234.com";
// time() is used to give an ever changing variable
$nfo = time();
    echo "NFO Code: "; echo $nfo; echo "<br>";
$ekey = (substr($nfo, -1) +1); // add 1 so the answer can't be '0'
    echo "The last digit of NFO Code + 1 so it can't be zero: "; echo $ekey; echo "<br>";
$xx = $ekey; $n = $ekey; $s = $teststr;
    echo "Original string -------    "; echo $s; echo "<br>";
//$Char = all the characters to be used to randomly generate the code. 
$Char = "-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[^_`abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$QtyChar = strlen($Char);
$qtd = $xx;
//Create a Hash string to add confusion
$Hash=NULL; 
    for($x=1;
    $x<=$xx;$x++){
        $Pos = rand(1,$QtyChar);
        $Hash .= substr($Char,$Pos,1); 
    }
    echo "Hash to append ----- "; echo $Hash; echo "<br>";

$s = "{$teststr}{$Hash}"; // adds character(s) to the string
    echo "String to Encode ----    "; echo $s; echo "<br>";echo "<br>";
    for ($i = 0, $l = strlen($s); $i < $l; $i++) { 
        $c = ord($s[$i]); 
        if ($c >= 91 && $c <= 126) { 
            $s[$i] = chr(($c - 71 + ($n + $n + $n)) % 26 + 97); 
        if ($c >= 65 && $c <= 90)  
            $s[$i] = chr(($c - 39 + ($n + $n + $n)) % 26 + 64);
        } else if ($c >= 33 && $c <= 64) { 
            $s[$i] = chr(($c - 13 + ($n + $n)) % 26 + 39); 
 }}
    echo "Encoded string ------    "; echo $s; echo "<br>";echo "<br>";
// Now decode the encoded string
$s = substr($s, 0, -($xx)); //this strips off the added characters
$n = $ekey = 0 - $ekey; // negate the key for decoding
    for ($i = 0, $l = strlen($s); $i < $l; $i++) { 
        $c = ord($s[$i]); 
        if ($c >= 91 && $c <= 126) { 
            $s[$i] = chr(($c - 71 + ($n + $n + $n)) % 26 + 97); 
        if ($c >= 65 && $c <= 90)  
            $s[$i] = chr(($c - 39 + ($n + $n + $n)) % 26 + 64);
        } else if ($c >= 33 && $c <= 64) { 
            $s[$i] = chr(($c - 13 + ($n + $n)) % 26 + 39);   
 } }

    echo "Decoded string ------    ";echo $s; echo "<br>";echo "<br>";
    echo "Problems that remain, Upper case letters are not encoded.";
?>
Member Avatar for LastMitch

I have been working this on and off for a while to encrypt data strings. Seems to work fine except Upper case letters are not encrypted, just stay as Upper case letters. It's got to be something simple, I just don't see it.

You really took some time calculating those characters.

Try used this function:

http://php.net/manual/en/function.mcrypt-encrypt.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.