note 103471 rejected from function.crypt by danbrown
| From: | danbrown@php.net | Date: | Fri, 15 Apr 2011 12:34:53 +0000 |
| Subject: | note 103471 rejected from function.crypt by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178869@lists.php.net to get a copy of this message | ||
Note Submitter: matthewallenjordan at hotmail dot com
----
I would like to know if this would be a safe use of a script for simplistic ascii
encryption/decryption. The output would look like garbled junk but it works due to the low shifting
of the ord<->chr use, and without the key I can't see the possible way someone would be
able to break the crypt. Obvious that the key would have to be a bit more complex than
"mydogsnameisFrank", something such as "j&,2Oc8%" and all character based
within the key. Anyway...
function fn_Encrypt ($KEY = NULL, $STRING = NULL) { return fn_Crypt (TRUE, $KEY, $STRING); }
function fn_Decrypt ($KEY = NULL, $STRING = NULL) { return fn_Crypt (FALSE, $KEY, $STRING); }
function fn_Crypt ($POLARITY = TRUE, $KEY = NULL, $STRING = NULL) {
if (($KEY != NULL) && ($STRING != NULL)) {
if ((strlen($KEY) > 0) && (strlen($STRING) > 0)) {
$OUTPUT = "";
$KEY_INDEX = 0;
for ($STRING_INDEX = 0; $STRING_INDEX < strlen($STRING); $STRING_INDEX++) {
if ($POLARITY) { $NUMERIC = ord($STRING[$STRING_INDEX]) + ord($KEY[$KEY_INDEX]); $POLARITY =
!$POLARITY; }
else { $NUMERIC = ord($STRING[$STRING_INDEX]) - ord($KEY[$KEY_INDEX]); $POLARITY = !$POLARITY;
}
$OUTPUT .= chr($NUMERIC);
if (($KEY_INDEX + 1) > (strlen($KEY) - 1)) { $KEY_INDEX = 0; }
else { $KEY_INDEX++; }
}
return $OUTPUT;
}
}
return NULL;
}
Even with people knowing the following, the key being longer and hard to guess, rotating the key
over time etc; seems this would do the job if not processing a ton of data throught the $STRING.