RC4 PHP decryption fails - php

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);
}

Related

Migration from depricated function create_function

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

PHP Script Runs out of memory

My PHP code is supposed to generate 1000 random words with a random length between 3 and 7 chars. And then it is supposed to compare each word to all the words in my dictionary, engmix.txt, and place the matches into an array and all the nonmatches into another array. I know the code functions correctly, but it runs out of memory on both computers that I have tried running it on. I am using XAMPP as my webserver and even tested removing the memory limit to see if it would run. I would like advice on how to optimize this code.
<?php
ini_set('memory_limit', '-1');
function getRandomWord($len = 10) {
$word = range('a', 'z');
shuffle($word);
return substr(implode($word), 0, $len);
}
$words = array();
for ($i = 0; $i < 300; $i++) {
$words[$i] = getRandomWord(rand(3, 7));
}
$matches = array();
$nonmatches = array();
$k = 0;
$dictionary = file("engmix.txt");
for ($i=0; $i < count($dictionary); $i++) {
for ($j = 0; $j < count($words); $j++) {
if ($dictionary[$i] == $words[$j]) {
$matches[$k] = $words[$j];
$k++;
} else {
$nonmatches[$k] = $words[$j];
$k++;
}
}
}
?>
New fixed code:
<?php
function getRandomWord($len = 10) {
$word = range('a', 'z');
shuffle($word);
return substr(implode($word), 0, $len);
}
$words = array();
for ($i = 0; $i < 300; $i++) {
$words[$i] = getRandomWord(rand(3, 7));
}
$matches = array();
$nonmatches = array();
$file = file("engmix.txt");
$i = 0;
$file_handle = fopen("engmix.txt", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$line = str_replace("\n", "", $line);
$line = str_replace("\r", "", $line);
for ($i = 0; $i < count($words); $i++) {
if ($line == $words[$i]) {
$matches[] = $words[$i];
}
}
}
fclose($file_handle);
$nonmatches = array_diff($words, $file);
print("<pre>");
print_r($matches);
print("<pre>");
print("<pre>");
print_r($nonmatches);
print("<pre>");
?>

RC4 Decodeing Using PHP

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

XOR string in PHP with key

I need to XOR a string/text in PHP the base64 encode it, but something goes wrong:
<?php
$mustget = 'Kw4SCQ==';
$string = 'Josh';
echo("Must get: " . $mustget . "\n");
echo("We got: " . base64_encode(xor_this($string)) . "\n");
function xor_this($text) {
$key = 'frtkj';
$i = 0;
$encrypted = '';
foreach (str_split($text) as $char) {
$encrypted .= chr(ord($char) ^ ord($key{$i++ % strlen($key)}));
}
return $encrypted;
}
?>
I get the following result, but I need to get the "$mustget" one:
Must get: Kw4SCQ==
We got: LB0HAw==
What do I do wrong?
$mustget = 'Kw4SCQ==';
$key = 'frtkj';
$key_length = strlen($key);
$encoded_data = base64_decode($mustget);
$result = '';
$length = strlen($encoded_data);
for ($i = 0; $i < $length; $i++) {
$tmp = $encoded_data[$i];
for ($j = 0; $j < $key_length; $j++) {
$tmp = chr(ord($tmp) ^ ord($key[$j]));
}
$result .= $tmp;
}
echo $result; // Josh
http://ideone.com/NSIe7K
I'm sure you can reverse it and create a function, that "crypts" the data ;-)

PHP Random Number

I want to generate a random number in PHP where the digits itself should not repeat in that number.
Is that possible?
Can you paste sample code here?
Ex: 674930, 145289. [i.e Same digit shouldn't come]
Thanks
Here is a good way of doing it:
$amountOfDigits = 6;
$numbers = range(0,9);
shuffle($numbers);
for($i = 0;$i < $amountOfDigits;$i++)
$digits .= $numbers[$i];
echo $digits; //prints 217356
If you wanted it in a neat function you could create something like this:
function randomDigits($length){
$numbers = range(0,9);
shuffle($numbers);
for($i = 0;$i < $length;$i++)
$digits .= $numbers[$i];
return $digits;
}
function randomize($len = false)
{
$ints = array();
$len = $len ? $len : rand(2,9);
if($len > 9)
{
trigger_error('Maximum length should not exceed 9');
return 0;
}
while(true)
{
$current = rand(0,9);
if(!in_array($current,$ints))
{
$ints[] = $current;
}
if(count($ints) == $len)
{
return implode($ints);
}
}
}
echo randomize(); //Numbers that are all unique with a random length.
echo randomize(7); //Numbers that are all unique with a length of 7
Something along those lines should do it
<?php
function genRandomString() {
$length = 10; // set length of string
$characters = '0123456789'; // for undefined string
$string ="";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
$s = genRandomString(); //this is your random print var
or
function rand_string( $length )
{
$chars = "0123456789";
$size = strlen( $chars );
for( $i = 0; $i < $length; $i++ )
{
$str .= $chars[ rand( 0, $size – 1 ) ];
}
return $str;
}
$rid= rand_string( 6 ); // 6 means length of generate string
?>
$result= "";
$numbers= "0123456789";
$length = 8;
$i = 0;
while ($i < $length)
{
$char = substr($numbers, mt_rand(0, strlen($numbers)-1), 1);
//prevents duplicates
if (!strstr($result, $char))
{
$result .= $char;
$i++;
}
}
This should do the trick. In $numbers you can put any char you want, for example: I have used this to generate random passwords, productcodes etc.
The least amount of code I saw for something like this was:
function random_num($n=5)
{
return rand(0, pow(10, $n));
}
But I'm assuming it requires more processing to do this than these other methods.

Categories