I am trying to decode the rc4 encoded value using follwong function.
function rc4($key, $data){
// Store the vectors "S" has calculated
static $SC;
// Function to swaps values of the vector "S"
$swap = create_function('&$v1, &$v2', '
$v1 = $v1 ^ $v2;
$v2 = $v1 ^ $v2;
$v1 = $v1 ^ $v2;
');
$ikey = crc32($key);
if (!isset($SC[$ikey])) {
// Make the vector "S", basead in the key
$S = range(0, 255);
$j = 0;
$n = strlen($key);
for ($i = 0; $i < 255; $i++) {
$char = ord($key{$i % $n});
$j = ($j + $S[$i] + $char) % 256;
$swap($S[$i], $S[$j]);
}
$SC[$ikey] = $S;
} else {
$S = $SC[$ikey];
}
// Crypt/decrypt the data
$n = strlen($data);
$data = str_split($data, 1);
$i = $j = 0;
for ($m = 0; $m < $n; $m++) {
$i = ($i + 1) % 256;
$j = ($j + $S[$i]) % 256;
$swap($S[$i], $S[$j]);
$char = ord($data[$m]);
$char = $S[($S[$i] + $S[$j]) % 256] ^ $char;
$data[$m] = chr($char);
}
return $data; implode('', $data);
}
If i use this function to decode simple text it works fine, but when i go for bigger key, it gives something like this
Œù©>Ç ¾¾óÅ,ŒŒ£f®ãápXŽ×{
Which i am not getting what it is???
Can you please explain whats wrong with it?
My Key is f033b52440607260e131d4f4a0f55cae and data is: 4522261326835a46d78099e0
Related
I have function in my script but after updating my php version to 7.3 i receive an notice that function create_function is deprecated. How can i migrate and what can i use instead of create_function?
function rc4($key, $data){
// Store the vectors "S" has calculated
static $SC;
// Function to swaps values of the vector "S"
$swap = create_function('&$v1, &$v2', '
$v1 = $v1 ^ $v2;
$v2 = $v1 ^ $v2;
$v1 = $v1 ^ $v2;
');
$ikey = crc32($key);
if (!isset($SC[$ikey])) {
// Make the vector "S", basead in the key
$S = range(0, 255);
$j = 0;
$n = strlen($key);
for ($i = 0; $i < 255; $i++) {
$char = ord($key{$i % $n});
$j = ($j + $S[$i] + $char) % 256;
$swap($S[$i], $S[$j]);
}
$SC[$ikey] = $S;
} else {
$S = $SC[$ikey];
}
// Crypt/decrypt the data
$n = strlen($data);
$data = str_split($data, 1);
$i = $j = 0;
for ($m = 0; $m < $n; $m++) {
$i = ($i + 1) % 256;
$j = ($j + $S[$i]) % 256;
$swap($S[$i], $S[$j]);
$char = ord($data[$m]);
$char = $S[($S[$i] + $S[$j]) % 256] ^ $char;
$data[$m] = chr($char);
}
return $data; implode('', $data);
}
enter code here
I'm trying to understand the algorithm Bitap for string matching
The following code related to bitap algorithm that write with PHP language :
function SearchString($text, $pattern)
{
$m = strlen($pattern);
$textLen = strlen($text);
$patternMask = array();
$i;
if (empty($pattern)) return 0;
if ($m > 31) return -1; //Error: The pattern is too long!
$R = ~1;
for ($i = 0; $i <= 127; ++$i)
$patternMask[$i] = ~0;
for ($i = 0; $i < $m; ++$i)
$patternMask[ord($pattern[$i])] &= ~(1 << $i);
for ($i = 0; $i < $textLen; ++$i)
{
$R |= $patternMask[ord($text[$i])];
$R <<= 1;
if (0 == ($R & (1 << $m)))
return ($i - $m) + 1;
}
return -1;
}
Please explain how this code works?
i am not understand this part :
for ($i = 0; $i < $m; ++$i)
$patternMask[ord($pattern[$i])] &= ~(1 << $i);
for ($i = 0; $i < $textLen; ++$i)
{
$R |= $patternMask[ord($text[$i])];
$R <<= 1;
if (0 == ($R & (1 << $m)))
return ($i - $m) + 1;
}
return -1;
thanks
I have encrypted strings using rc4 encryption. I could decrypt 1st string I have encrypted. But I couldn't successfully decrypt 2nd string onwards. Any idea?
My code
$dpl = 256;
$file = fopen( 'log', "w");
$key = 'sjhdjhd';
$content1 = rc4($key,str_repeat(" ",$dpl).'ABC');
$content1 = substr($content1,$dpl);
$content2 = rc4($key,str_repeat(" ",$dpl).'DEFG');
$content2 = substr($content2,$dpl);
fwrite($file, $content1);
fwrite($file, $content2);
fclose( $file );
$file = fopen( 'log', "r");
while ( $buff = fread( $file, 256 ) ) {
$plaintext = rc4($key,str_repeat(" ",$dpl).$buff);
echo substr($plaintext,$dpl).PHP_EOL;
}
fclose( $file );
function rc4($key, $data)
{
// Store the vectors "S" has calculated
static $SC;
// Function to swaps values of the vector "S"
$swap = create_function('&$v1, &$v2', '
$v1 = $v1 ^ $v2;
$v2 = $v1 ^ $v2;
$v1 = $v1 ^ $v2;
');
$ikey = crc32($key);
if (!isset($SC[$ikey])) {
// Make the vector "S", basead in the key
$S = range(0, 255);
$j = 0;
$n = strlen($key);
for ($i = 0; $i < 255; $i++) {
$char = ord($key{$i % $n});
$j = ($j + $S[$i] + $char) % 256;
$swap($S[$i], $S[$j]);
}
$SC[$ikey] = $S;
} else {
$S = $SC[$ikey];
}
// Crypt/decrypt the data
$n = strlen($data);
$data = str_split($data, 1);
$i = $j = 0;
for ($m = 0; $m < $n; $m++) {
$i = ($i + 1) % 256;
$j = ($j + $S[$i]) % 256;
$swap($S[$i], $S[$j]);
$char = ord($data[$m]);
$char = $S[($S[$i] + $S[$j]) % 256] ^ $char;
$data[$m] = chr($char);
}
return implode('', $data);
}
I'm currently implement left shift using int[] arrays in php and need to get back the decimal after operation. So I have written the following snippet to attempt conversion of binary array to decimal.
function bin2dec($bin)
{
$length = count($bin) - 1;
$sum = 0;
//convert using doubling
for($i = 0; $i < $length; $i++)
{
//use string_add if doubling bigger than int32
if($i >= 16)
{
$double = $this->string_add("$sum", "$sum");
$cr = $bin[$i];
if($cr == 0)
{
$sum = $this->string_add($sum, $double);
}
else{
return $i;//WHAT's UP??!
$add = $this->string_add($double, "$cr");
$sum = $this->string_add($sum, $add);
}
}
else{
$sum += ($sum * 2) + $bin[$i];
}
}
return $sum;
}
Now the weird problem is in the loop where $cr != 0, $i returns an unbelievable value already not satisfying the loop condition but I can't figure out why this is happening. Here's the rest of the relevant code.
function string_add($a, $b)
{
$lena = strlen($a); $lenb = strlen($b);
if($lena == $lenb)
{
$len = $lena - 1;//any
}
else if($lena > $lenb)
{
$b = str_pad($b, $lena, "0", STR_PAD_LEFT);
$len = $lena - 1;
}
else if($lenb > $lena){
$a = str_pad($a, $lenb, "0", STR_PAD_RIGHT);
$len = $lenb - 1;
}
$result = "";
for ($i = $len, $carry = 0; $i >= 0 || $carry != 0; $i--)
{
$add1 = $i < 0 ? 0 : $a[$i];
$add2 = $i < 0 ? 0 : $b[$i];
$add = $add1 + $add2 + $carry;
if ($add > 9) {
$carry = 1;
$add -= 10;
}
else {
$carry = 0;
}
$result .= $add;
}
return strrev($result);
}
$arr = array_pad(array(1), 62, 0);
$dec = bin2dec($arr);
return $dec;//test
I have also implemented a working version on ideone for testing. Does anyone understand why this is happening?
Thanks.
Ok, so apparently the problem was adding more than needed and unnecessarily subtracting 1 from length in bin2dec. Here's the final working version:
<?php
class MyClass{
function bin2dec($bin)
{
$length = count($bin);
$sum = 0;
//convert using doubling
for($i = 0; $i < $length; $i++)
{
//use string_add if doubling bigger than int32
if($i >= 16)
{
$sum = $this->string_add("$sum", "$sum");
$cr = $bin[$i];
if($cr != 0){
$sum = $this->string_add($sum, "$cr");
}
}
else{
$sum += $sum + $bin[$i];
}
}
return $sum;
}
function string_add($a, $b)
{
$lena = strlen($a); $lenb = strlen($b);
if($lena == $lenb)
{
$len = $lena - 1;//any
}
else if($lena > $lenb)
{
$b = str_pad($b, $lena, "0", STR_PAD_LEFT);
$len = $lena - 1;
}
else if($lenb > $lena){
$a = str_pad($a, $lenb, "0", STR_PAD_RIGHT);
$len = $lenb - 1;
}
$result = "";
for ($i = $len, $carry = 0; $i >= 0 || $carry != 0; $i--)
{
$add1 = $i < 0 ? 0 : $a[$i];
$add2 = $i < 0 ? 0 : $b[$i];
$add = $add1 + $add2 + $carry;
if ($add > 9) {
$carry = 1;
$add -= 10;
}
else {
$carry = 0;
}
$result .= $add;
}
return strrev($result);
}
}
$man = new MyClass();
$arr = array_pad(array(1), 62, 0);
$dec = $man->bin2dec($arr);
echo $dec;
Using strings...
function bin2dec($bin)
{
$length = count($bin);
$sum = "";
for ( $i = 0; $i < $length; $i++)
{
$sum = $sum . ( $bin[$i} == 0 ? '0' : '1');
}
return $sum;
}
function string_add($a, $b)
{
// not efficient, but obvious
$maxlen = ( strlen($a) > strlen($b) ? strlen($a) : strlen($b);
// make them both same length as longest
$a = str_pad($a, $maxlen, "0", STR_PAD_LEFT);
$b = str_pad($b, $maxlen, "0", STR_PAD_LEFT);
$result = "";
$carry = 0;
// start from the right end
for ($i = $maxlen - 1; $i >= 0; $i--)
{
$val = $a[$i] + $b[$i] + $carry;
$result = ( $val % 10 ) . $result;
$carry = $val / 10;
}
// handle final carry if present
if ( $carry > 0 )
{
$result = $carry . $result;
}
return $result;
}
$arr = array_pad(array(1), 62, 0);
$dec = bin2dec($arr);
return $dec;//test
Logfiles are full of this error:
Undefined offset: 255 in /var/www/html/site2/functions.inc.php on line 764"
The line 764 is the following:
$counter[$i] = $counter[$x];
The whole function see below.
Can somebody help me to fix it? Thanks.
function Encode($data,$pwd) {
$pwd_length = strlen($pwd);
for ($i = 0; $i < 255; $i++) {
$key[$i] = ord(substr($pwd, ($i % $pwd_length)+1, 1));
$counter[$i] = $i;
}
$x = '';
for ($i = 0; $i < 255; $i++) {
$x = ($x + $counter[$i] + $key[$i]) % 256;
$temp_swap = $counter[$i];
$counter[$i] = $counter[$x];
$counter[$x] = $temp_swap;
}
$a = '';
$j = '';
$Zcrypt = '';
for ($i = 0; $i < strlen($data); $i++) {
$a = ($a + 1) % 256;
$j = ($j + $counter[$a]) % 256;
$temp = $counter[$a];
$counter[$a] = $counter[$j];
$counter[$j] = $temp;
$k = $counter[(($counter[$a] + $counter[$j]) % 256)];
$Zcipher = ord(substr($data, $i, 1)) ^ $k;
$Zcrypt .= chr($Zcipher);
}
return $Zcrypt;
}
Your for-loop stops when $i >= 255. You don't want that. I think you want the for-loop to stop at $i>255.
So change
for ($i = 0; $i < 255; $i++) {
into
for ($i = 0; $i <= 255; $i++) {
And you're good to go!