问题描述
我想制作一个如下图所示的程序
i want to make a program like the following picture
这是我的代码
<?php
$iv = 0;
$Kunci = "U";
$key = dechex(ord($Kunci));
$k = sprintf("%08d",decbin(hexdec($key)));
$c0 = sprintf("%08d", decbin($iv));
$Cip= "0C52CCD7EDB3";
$Cbs = array();
$Cbs[0]= $c0;
$Plaintext = array();
$Cas = array();
$P = array();
$m= 1;
$n=1;
//$Cbs=
$Csplit = str_split($Cip, 2);
$Cas= str_split($Cip,2);
for ($i=0; $i<count($Csplit); $i++) {
$Cbs[$m] = sprintf("%08d",decbin(hexdec($Csplit[$i])));
$m++;
}
for($i=0; $i < count($Cas); $i++){
$Cas[$i] = sprintf("%08d",decbin(hexdec($Cas[$i])));
$Cas[$i]=bindec($Cas[$i])>>1;
if($Cas[$i] % 2 <> 0)$Cas[$i]+=128;
$Cas[$i]=sprintf("%08d", decbin($Cas[$i]));
}
foreach($Cas as $cas_item) {
$prev_c = $Cbs[$n-1];
$P[$n] = _xor($cas_item, $k);
$P[$n] = _xor($P[$n], $prev_c);
$Plaintext[$n] = chr(bindec($P[$n]));
$n++;
}
function _xor($text,$key){
for($i=0; $i<strlen($text); $i++){
$text[$i] = intval($text[$i])^intval($key[$i]);
}
return $text;
}
print_r($Csplit);
echo "<br/>";
print_r($Cbs);
echo "<br/>";
print_r($Cas);
echo "<br/>";
print_r($P);
echo "<br/>";
print_r($Plaintext);
?>
Cbs =移位仓之前CAS =移位仓后这样一来,程序代码就可以工作了,但是数组2和数组5是错误的.前面的二进制位代码应为0,而不是1.输出 :
Cbs = before shift binerCas = after shift binerand this comes out, the program code works but array 2 and array 5 are wrong. the binary bit code in front should be 0, not 1.Output :
数组2应该是01110000而不是11110000,数组5应该是01110100但结果是11110100.为什么前面的0是1?
array 2 should be 01110000 instead of 11110000, and array 5 should be 01110100 but result is 11110100. why is 0 in front being 1?
推荐答案
右移时,请注意有符号和无符号移位的区别. (也称为算术或逻辑移位)
When shifting right, beware of the difference of signed and unsigned shift. (also called arithmetic or logical shift)
按符号右移的8位值11101000将为11110100.
8-bit value 11101000 right shifted as signed will be 11110100.
要点是,如果您要向右移动一个有符号的值,那么最高的位将被复制到移入的新位中.如果您要转移无符号的值,那么最高的位将被移为零.
The point is if you are shifting a signed value to the right, the uppermost bit is duplicated into the new bits moving in. If you are shifting unsigned values, the uppermost bits move in zeroes.
缺少无符号整数数据类型的语言还有另一个右移运算符>>>
,表示要进行无符号(或逻辑")移位.在PHP和Java中就是这种情况.
Languages that lack unsigned integer datatypes have another right-shift operator >>>
to indicate that an unsigned (or 'logical') shift is meant. This is the case in PHP and in Java.
这仅适用于右移.永不离开.关键是右移将导致行为被二分频.
This only applies to right-shifts. Never to left. The point is that a right-shift will result in a divide-by-two behaviour.
这篇关于为什么异或结果不同,0变为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!