Ascii加密 - PHP

时间:2016-10-05 08:48:09

标签: php encryption ascii

我试图将输入字符串转换为ASCII字符"或者任何其他选择的字符"用PHP。但我可能需要一些帮助。

这是我迄今为止所做的。我必须在这个项目中用PHP制作这个,我之前从未真正使用过这种语言。

<?php

$input = "This is just a test";
print "$input

";

function crypter($input) {

//Variables & Arrays --> Gives each normal character a ascii value
$array = array(
    "a" => "001",
    "b" => "002",
    "c" => "003",
    "d" => "004",
    "e" => "005",
    "f" => "006",
    "g" => "007",
    "h" => "008",
    "i" => "009",
    "j" => "010",
    "k" => "011",
    "l" => "012",
    "m" => "013",
    "n" => "014",
    "o" => "015",
    "p" => "016",
    "q" => "017",
    "r" => "018",
    "s" => "019",
    "t" => "020",
    "u" => "021",
    "v" => "022",
    "w" => "023",
    "x" => "024",
    "y" => "025",
    "z" => "026",
    "æ" => "027",
    "ø" => "028",
    "å" => "029",
    "A" => "031",
    "B" => "032",
    "C" => "033",
    "D" => "034",
    "E" => "035",
    "F" => "036",
    "G" => "037",
    "H" => "038",
    "I" => "039",
    "J" => "040",
    "K" => "041",
    "L" => "042",
    "M" => "043",
    "N" => "044",
    "O" => "045",
    "P" => "046",
    "Q" => "047",
    "R" => "048",
    "S" => "049",
    "T" => "050",
    "U" => "051",
    "W" => "052",
    "V" => "053",
    "X" => "054",
    "Y" => "055",
    "Z" => "056",
    "Æ" => "057",
    "Ø" => "058",
    "Å" => "059",
);

$result = ""; //Empty string variable

//Give the result variable the corresponding ascii character to each input character

for($i = 0; $i < strlen($input); $i++) {
    $c = $input[$i];

    $result += $array[$c];
}

    //Testing - Printing the new result
    print_r($result);
}

crypter($input);

?>

2 个答案:

答案 0 :(得分:2)

使用resolve(results)) php函数将字符转换为ASCII码。

对于utf8输入必须使用ord()函数但没有mb_*,请查看手册 在this link

答案 1 :(得分:0)

我还使用了ord()。尝试一下,如果这对您有帮助,请告诉我们。添加到cabasaki答案,下面是解决方案。您可能需要调整它以满足您的需要。

<?php

$input = "This is just a test";
print crypter($input);

function crypter($input)
{
    $result = [];
    for ($i = 0; $i < strlen($input); $i++) {
        $c = ord($input[$i]);

        $result[] = $c;
    }

    return implode('', $result);
}

输出

841041051153210511532106117115116329732116101115116