使用 T9 模式加密 SMS 消息
encrypt with T9 mode for SMS messages
我今天在工作,然后我有了这个想法。我想制作一个脚本,该脚本采用纯文本,然后使用 T9 模式对其进行加密,以便在移动设备上发送短信 phone。
纯文本:你好
结果:4433555555666
所以我创建了一些 class 这样的:
<?php
class decoder {
public $keys = array(
'2' => 'abc',
'3'=> 'def',
'4'=> 'ghi',
'5'=> 'jkl',
'6'=> 'mno',
'7'=> 'pqrs',
'8'=> 'tuv',
'9'=> 'wxyz',
'0'=> ' '
);
function key($string) {
// store every character from the string into an array
$str = str_split($string);
}
}
$deco = new decoder;
echo $deco->key('hello');
?>
问题是我需要一些算法和行来说明如何将给定纯文本中的每个字符与键数组进行比较,然后 return 如果匹配则给出键号。
你总是可以让它更简单,因为你只写一次,你可以使用这个:
$keys = array(
'a' => '2',
'b' => '22',
'c' => '222'... //and so on
然后使用简单的字符串替换。
否则您可以继续您的代码,但我认为它不会针对性能进行优化,因为您需要多次循环遍历数组才能找到给定的字符。
<?php
class decoder {
public $keys = array(
'a' => '2',
'b'=> '22',
'c'=> '222',
'd'=> '3',
'e'=> '33',
'f'=> '333',
'g'=> '4',
'h'=> '44',
'i'=> '444',
'j'=> '5',
'k'=> '55',
'l'=> '555',
'm'=> '6',
'n'=> '66',
'o'=> '666',
'p'=> '7',
'q'=> '77',
'r'=> '777',
's'=> '7777',
't'=> '8',
'u'=> '88',
'v'=> '888',
'w'=> '9',
'x'=> '99',
'y'=> '999',
'z'=> '9999',
' '=> '0'
);
function key($string) {
// store every character from the string into an array
$char = str_split($string);
$i = 0;
while ($i<count($char)) {
//foreach all keys and values
foreach ($this->keys as $key => $key_value) {
if ($char[$i] == $key) {
echo $key_value;
}
}
$i++;
}
}
}
$deco = new decoder;
echo $deco->key('hello');
?>
我今天在工作,然后我有了这个想法。我想制作一个脚本,该脚本采用纯文本,然后使用 T9 模式对其进行加密,以便在移动设备上发送短信 phone。
纯文本:你好
结果:4433555555666
所以我创建了一些 class 这样的:
<?php
class decoder {
public $keys = array(
'2' => 'abc',
'3'=> 'def',
'4'=> 'ghi',
'5'=> 'jkl',
'6'=> 'mno',
'7'=> 'pqrs',
'8'=> 'tuv',
'9'=> 'wxyz',
'0'=> ' '
);
function key($string) {
// store every character from the string into an array
$str = str_split($string);
}
}
$deco = new decoder;
echo $deco->key('hello');
?>
问题是我需要一些算法和行来说明如何将给定纯文本中的每个字符与键数组进行比较,然后 return 如果匹配则给出键号。
你总是可以让它更简单,因为你只写一次,你可以使用这个:
$keys = array(
'a' => '2',
'b' => '22',
'c' => '222'... //and so on
然后使用简单的字符串替换。
否则您可以继续您的代码,但我认为它不会针对性能进行优化,因为您需要多次循环遍历数组才能找到给定的字符。
<?php
class decoder {
public $keys = array(
'a' => '2',
'b'=> '22',
'c'=> '222',
'd'=> '3',
'e'=> '33',
'f'=> '333',
'g'=> '4',
'h'=> '44',
'i'=> '444',
'j'=> '5',
'k'=> '55',
'l'=> '555',
'm'=> '6',
'n'=> '66',
'o'=> '666',
'p'=> '7',
'q'=> '77',
'r'=> '777',
's'=> '7777',
't'=> '8',
'u'=> '88',
'v'=> '888',
'w'=> '9',
'x'=> '99',
'y'=> '999',
'z'=> '9999',
' '=> '0'
);
function key($string) {
// store every character from the string into an array
$char = str_split($string);
$i = 0;
while ($i<count($char)) {
//foreach all keys and values
foreach ($this->keys as $key => $key_value) {
if ($char[$i] == $key) {
echo $key_value;
}
}
$i++;
}
}
}
$deco = new decoder;
echo $deco->key('hello');
?>