Caeser encryption algorithm in PHP -
is there way use caeser encryption string in php; have come .net not know much.
take example:
original aaaaaa after ceaser encryption bbbbbb next example:
original abcd after ceaser encryption bcde could make function out of echo $output;
here's 1 version works ascii letters:
function caesar($str, $shift) { $len = strlen($str); $ord_alpha_upper = ord('a'); $ord_alpha_lower = ord('a'); $shift = $shift % 26 + 26; // takes care of negative shifts for($i = 0; $i < $len; ++$i) { $chr = $str[$i]; if (ctype_upper($chr)) { $str[$i] = chr($ord_alpha_upper + (ord($chr) - $ord_alpha_upper + $shift) % 26); } else if (ctype_lower($chr)) { $str[$i] = chr($ord_alpha_lower + (ord($chr) - $ord_alpha_lower + $shift) % 26); } } return $str; } you can use both positive , negative shifts , work correctly no matter shift distance.
however, aware php has no concept of character encoding (strings byte arrays), if input not in single-byte encoding or utf-8 not work correctly.
Comments
Post a Comment