Jump to content

Encrypt Decrypt Function


mega77

Recommended Posts

This Encrypt/Decrypt function results in an encrypted code with non-alphanumeric chars. Is anybody able to modify this php code slightly to only include alphanumeric chars ?

 

 

<?php
// String EnCrypt + DeCrypt function
// Author: halojoy, July 2006
// Modified and commented by: laserlight, August 2006

function convert($text, $key = '') {
    // return text unaltered if the key is blank
    if ($key == '') {
        return $text;
    }

    // remove the spaces in the key
    $key = str_replace(' ', '', $key);
    if (strlen($key) <  {
        exit('key error');
    }
    // set key length to be no more than 32 characters
    $key_len = strlen($key);
    if ($key_len > 32) {
        $key_len = 32;
    }

    $k = array(); // key array
    // fill key array with the bitwise AND of the ith key character and 0x1F
    for ($i = 0; $i < $key_len; ++$i) {
        $k[$i] = ord($key{$i}) & 0x1F;
    }

    // perform encryption/decryption
    for ($i = 0; $i < strlen($text); ++$i) {
        $e = ord($text{$i});
        // if the bitwise AND of this character and 0xE0 is non-zero
        // set this character to the bitwise XOR of itself
        // and the ith key element, wrapping around key length
        // else leave this character alone
        if ($e & 0xE0) {
            $text{$i} = chr($e ^ $k[$i % $key_len]);
        }
    }
    return $text;
}
?>

<?php
$encrypted = convert("Alpha numeric only,34,23.55,50", $key="password");
print $encrypted;
print convert($encrypted, $key="password");
?>

 

 

Results:

Encrypted: Qmc{v/|q}dazt/}j|x? ## 7>4&?"?

Decrypted: Alpha numeric only,34,23.55,50

Link to comment
https://forums.phpfreaks.com/topic/82911-encrypt-decrypt-function/
Share on other sites

for sure...:

<?php
function encodeDec($s) {
	$n = '';
	//convert every sign into hex.
	for ($i=0; $i<strlen($s); $i++)
		$n .= str_pad(dechex(ord(substr($s, $i, 1))), '0', STR_PAD_LEFT);
	return $n;
}

function decodeHex($s) {
	$n = '';
	//convert every hex into dec and into a sign.
	for ($i=0; $i<strlen($s); $i+=2)
		$n .= chr(hexdec(substr($s, $i, 2)));
	return $n;
}

$yourPassword = yourPasswordFunction('hello world');
$yourPassword = encodeDec($yourPassword);
echo 'Encrypted+converted in hex: '.$yourPassword;
$yourPassword = decodeHex($yourPassword);
$yourPassword = yourPasswordDecryptFunction($yourPassword);
echo 'Decrypted: '.$youPassword;
?>

Thank you for your answer. Your 2 functions result in the following. As far as I read moving between different code bases result in differences between the start and the end. Is there any way around this problem ? Is it possible to modify the actual encryption/decryption formula to bypass base code differences ? The Encryption and Decryption function is the same, the same function goes both ways.

 

Start:                Alpha numeric only,34,23.55,50

Encrypted:        Qmc{v/|q}dazt/}j|x? ## 7>4&?"?

 

Converted:        516d637b762f7c717d64617a742f7d6a7c783f20232320373e34263f223f3c6272202f3e

Convert back:    Qmc{v/|q}dazt/}j|x? ## 7>4&?"?

 

End result:        Alpha numeric only,34,23.55,50.fb!<-

 

<?php
$encrypted = convert("Alpha numeric only,34,23.55,50", $key="password")."<br />";
print $encrypted."<br />";

function encodeDec($s) {
	$n = '';
	//convert every sign into hex.
	for ($i=0; $i<strlen($s); $i++)
		$n .= str_pad(dechex(ord(substr($s, $i, 1))), '0', STR_PAD_LEFT);
	return $n;
}

function decodeHex($s) {
	$n = '';
	//convert every hex into dec and into a sign.
	for ($i=0; $i<strlen($s); $i+=2)
		$n .= chr(hexdec(substr($s, $i, 2)));
	return $n;
}

$converted = encodeDec($encrypted);
print $converted."<br />";

$reconverted = decodeHex($converted);
print $reconverted."<br />";

$decrypted = convert($reconverted, $key="password")."<br />";
print $decrypted;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.