slowAES decrypt to another key - php

There was a problem with the old slowAES library.
When trying to decrypt, in js it produces one, and in php it produces another.
There are a lot of errors in the console that I can’t figure out.
Tell me what's wrong? How to get the same keys?
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$a = "cd36b76f96b103402924bd5f76d3c204";
$b = "680eb6a492f48ea1b342aea7b79e18eb";
$c = "f746749b113236227058bd471f5c91dc";
function toHex($args){
if(func_num_args() != 1 || !is_array($args)){
$args = func_get_args();
}
$ret = '';
for($i = 0; $i < count($args) ;$i++)
$ret .= sprintf('%02x', $args[$i]);
return $ret;
}
function toNumbers($s){
$ret = array();
for($i=0; $i<strlen($s); $i+=2){
$ret[] = hexdec(substr($s, $i, 2));
}
return $ret;
}
function getRandom($min,$max){
if($min === null)
$min = 0;
if($max === null)
$max = 1;
return mt_rand($min, $max);
}
function generateSharedKey($len){
if($len === null)
$len = 16;
$key = array();
for($i = 0; $i < $len; $i++)
$key[] = getRandom(0,255);
return $key;
}
function generatePrivateKey($s,$size){
if(function_exists('mhash') && defined('MHASH_SHA256')){
return convertStringToByteArray(substr(mhash(MHASH_SHA256, $s), 0, $size));
}else{
throw new Exception('cryptoHelpers::generatePrivateKey currently requires mhash');
}
}
function convertStringToByteArray($s){
$byteArray = array();
for($i = 0; $i < strlen($s); $i++){
$byteArray[] = ord($s[$i]);
}
return $byteArray;
}
function convertByteArrayToString($byteArray){
$s = '';
for($i = 0; $i < count($byteArray); $i++){
$s .= chr($byteArray[$i]);
}
return $s;
}
include 'cryptovh/aes.php';
$aes = new AES();
$token = $aes->decrypt(toNumbers($c), 16, 2, toNumbers($a), 16, toNumbers($b));
echo toHex($token); // WHAT I HAVE
echo "<br>";
echo "016e9be78dd5130beb5febcd328ff588"; // WHAT I NEED
?>
cryptovh/aes.php
that is copy of: https://github.com/aleaxit/slowaes/blob/master/php/aes_fast.php
use this library:
https://github.com/aleaxit/slowaes
At the output, I get this token:
dd2f6d60b939b390dc19688babc3873d
And console errors:
Notice: Undefined offset: 16 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined offset: 20 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined offset: 24 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/panel/cryptovh/aes.php on line
386

In slowaes/php/aes_fast.php, the inversion of the MixColumns operation in the mixColumns method is implemented incorrectly, the else-block must be:
...
} else {
for ($c = 0; $c < 4; $c++) {
$t[ $c] = self::$GEX[$state[$c]] ^ self::$GBX[$state[4+$c]] ^ self::$GDX[$state[8+$c]] ^ self::$G9X[$state[12+$c]];
$t[ 4+$c] = self::$G9X[$state[$c]] ^ self::$GEX[$state[4+$c]] ^ self::$GBX[$state[8+$c]] ^ self::$GDX[$state[12+$c]];
$t[ 8+$c] = self::$GDX[$state[$c]] ^ self::$G9X[$state[4+$c]] ^ self::$GEX[$state[8+$c]] ^ self::$GBX[$state[12+$c]];
$t[12+$c] = self::$GBX[$state[$c]] ^ self::$GDX[$state[4+$c]] ^ self::$G9X[$state[8+$c]] ^ self::$GEX[$state[12+$c]];
}
}
...
The inverse of the MixColumns operation is required for decryption.
There is also a typo in the method invMain, line 3, where i must be replaced by $i.
With these changes, the expected result is obtained, which can be verified e.g. here. The warnings are also no longer displayed.
I've filed an issue here. Note the ReadMe: The code is intended more for didactic purposes. In practice, openssl_encrypt / openssl_decrypt or similar should be used.

Related

How to rezolve this error in PHPf for sotring probleme

I've tried to make a apply a simple sorting technique like Insertion method, and I've got this annoying error.
I'm using PHP 7.
<br />
<b> Notice: Array to string conversion in <b>[...][...]</b> on line <b>21</b><br /> Array
The line 21 is where I call echo Interschimbare
The code is as following:
<?php
function Interschimbare(array $Vector) : array{
$N = count($Vector);
for($i = 0; $i < $N - 1; $i++)
{
for($j = $i + 1; $j < $N; $j++)
{
if($Vector[$i] > $Vector[$j])
{
$aux = $Vector[$i];
$Vector[$i] = $Vector[$j];
$Vector[$j] = $aux;
}
}
}
return $Vector;
}
$test = [3, 0, 2, 5, -1, 4, 1];
//print_r(Interschimbare($test)); // with this line works just fine
echo Interschimbare($test);
With echo doesn't work but with print_r works just fine
:)
your variable $Vector is an array so you need to convert it to string in order to use echo.
for example:
echo implode(",",Interschimbare($test));
output will be:
-1,0,1,2,3,4,5

PHP Notice: Undefined offset: 0

I receive the following PHP notice
PHP Notice: Undefined offset: 0
Here is a sample of what causes the error.
$file = $_SERVER['DOCUMENT_ROOT']."/savedposts/edited".$post_id.".txt";
$saved_content = file_get_contents($_SERVER['DOCUMENT_ROOT']."/savedposts/".$post_id.".txt");
$cct = 0;
$contexttext = array();
for ($i = 0; $i < strlen($saved_content); $i++) {
if (substr($saved_content, $i, 9) == "[Context]") {
$i = $i + 9;
while (substr($saved_content, $i, 10) !== "[/Context]") {
$contexttext[$cct] .= substr($saved_content, $i, 1);
$i++;
}
$cct++;
}
}
The error is on this line
$contexttext[$cct] .= substr($saved_content, $i, 1);
How to fix the notice.
To replicate this:
$cct = 0;
$contexttext = array();
$contexttext[$cct] .= 'test';
Here 'test' is being appended to $contexttext[$cct], which evaluates to: $contexttext[0]. However there is nothing at [0] yet, because it's an empty array, we can't append to something that doesn't exist
If however you'd done this:
$cct = 0;
$contexttext = array();
$contexttext[$cct] = '';
$contexttext[$cct] .= 'test';
Then the notice would dissapear, because now when we append a string, we have something to append it to

Undefined variable for loop

I have a code in use and it generates a PHP error in the second "for" loop.
PHP Notice: Undefined variable: newmatches
if (empty($result['ERR'])) {
preg_match_all('(<h3><a[^<>]*href="([^<>]*)"[^<>]*>(.*)</a>\s*</h3>)siU', $result['EXE'], $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
$matches[1][$i] = urldecode($matches[1][$i]);
preg_match_all('/\*\*(http:\/\/.*$)/siU', $matches[1][$i], $urls);
$newmatches[1][$i] = $urls[1][0];
}
for ($i = 0; $i < count($newmatches[1]); $i++) { //PHP Notice: Undefined variable: newmatches
if(strstr($newmatches[1][$i], $domain))
return $i+1;
}
} else {
return '0';
}
Thank you in advance!
I don't see anywhere where $newmatches would be set, aside from the first for loop, which won't run if count($matches[1]) is 0.
Not sure what all this hardcoding of your index to 1 is about, but a simple fix is to set $newmatches[1] = array() before the first loop.

Encrypt/decrypt with XOR in PHP

I am studying encryption. And I got a problem like this:
After I XOR plaintext with a key, I get a crypt, "010e010c15061b4117030f54060e54040e0642181b17", as hex type. If I want to get plaintext from this crypt, what should I do in PHP?
I tried convert it to string/int and after that take them to XOR with the key (three letters). But it doesn't work.
This is the code:
function xor_this($string) {
// Let's define our key here
$key = 'fpt';
// Our plaintext/ciphertext
$text = $string;
// Our output text
$outText = '';
// Iterate through each character
for($i=0; $i<strlen($text); )
{
for($j=0; $j<strlen($key); $j++,$i++)
{
$outText .= ($text[$i] ^ $key[$j]);
//echo 'i=' . $i . ', ' . 'j=' . $j . ', ' . $outText{$i} . '<br />'; // For debugging
}
}
return $outText;
}
function strToHex($string)
{
$hex = '';
for ($i=0; $i < strlen($string); $i++)
{
$hex .= dechex(ord($string[$i]));
}
return $hex;
}
function hexToStr($hex)
{
$string = '';
for ($i=0; $i < strlen($hex)-1; $i+=2)
{
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
$a = "This is the test";
$b = xor_this($a);
echo xor_this($b), '-------------';
//
$c = strToHex($b);
$e = xor_this($c);
echo $e, '++++++++';
//
$d = hexToStr($c);
$f = xor_this($d);
echo $f, '=================';
And this is the result:
This is the test-------------
PHP Notice: Uninitialized string offset: 29 in C:\
Users\Administrator\Desktop\test.php on line 210 PHP Stack trace: PHP
1. {main}() C:\Users\Administrator\Desktop\test.php:0 PHP 2. xor_this() C:\Users\Administrator\Desktop\test.php:239
Notice: Uninitialized string offset: 29 in
C:\Users\Administrator\Desktop\test.p hp on line 210
Call Stack:
0.0005 674280 1. {main}() C:\Users\Administrator\Desktop\test.php:0
0.0022 674848 2. xor_this() C:\Users\Administrator\Desktop\test.php:23 9
UBE^A►WEAVA►WEAV#◄WEARAFWECWB++++++++
This is zs$fs☺=================
Why? The "UBE^A►WEAVA►WEAV#◄WEARAFWECWB++++++++" is the result, which I got trouble in my real work.
Try this:
function xor_this($string) {
// Let's define our key here
$key = ('magic_key');
// Our plaintext/ciphertext
$text = $string;
// Our output text
$outText = '';
// Iterate through each character
for($i=0; $i<strlen($text); )
{
for($j=0; ($j<strlen($key) && $i<strlen($text)); $j++,$i++)
{
$outText .= $text{$i} ^ $key{$j};
//echo 'i=' . $i . ', ' . 'j=' . $j . ', ' . $outText{$i} . '<br />'; // For debugging
}
}
return $outText;
}
Basically to revert text back (even numbers are in) you can use the same function:
$textToObfuscate = "Some Text 12345";
$obfuscatedText = xor_this($textToObfuscate);
$restoredText = xor_this($obfuscatedText);
Even easier:
function xor_string($string, $key) {
for($i = 0; $i < strlen($string); $i++)
$string[$i] = ($string[$i] ^ $key[$i % strlen($key)]);
return $string;
}
Based on the code above i created 2 functions to xor encode a JSON string using javascript and then decode it on server side using PHP.
!!! Important: If you will have characters different from ASCII(like Chinese, Cyrillic, Symbols...) in your JSON string, you
must either write some code in PHP or JS to fix how these
characters are encoded/decoded (ord/chr in PHP produce different
results in comparison with JS charCodeAt/String.fromCharCode) or
just base64_encode the JSON string and after that xor encode it.
Personally i use xor_string(base64_encode(JSON.stringify(object)), 'xor_key') in JS and on PHP side:
$json = json_decode(base64_decode(
xor_string(file_get_contents("php://input"), 'xor_key')
),
true);
PHP:
function xor_string($string, $key) {
$str_len = strlen($string);
$key_len = strlen($key);
for($i = 0; $i < $str_len; $i++) {
$string[$i] = $string[$i] ^ $key[$i % $key_len];
}
return $string;
}
Javascript:
function xor_string(string, key) {
string = string.split('');
key = key.split('');
var str_len = string.length;
var key_len = key.length;
var String_fromCharCode = String.fromCharCode;
for(var i = 0; i < str_len; i++) {
string[i] = String_fromCharCode(string[i].charCodeAt(0) ^ key[i % key_len].charCodeAt(0));
}
return string.join('');
}

php recursion - notice: undefined offset: 20 array

I'm working to parse the following string but at the same time trying to get rid of the current undefined offset on the index array. I would appreciate some help w/ the undefined offset issue.
error_reporting(E_ALL);
ini_set('display_errors', '1');
function my_recursion($String, &$Inc) {
$l = strlen($String);
$has_quotes = 0; $array = array();
$x= 0;
for ($Inc; $Inc < $l; $Inc++) {
$my_char = $String[$Inc];
if ($my_char == '(' && !$has_quotes) {
$Inc++;
$array[$x] = my_recursion($String, $Inc);
$x++;
} else if ($my_char == '"') {
$has_quotes = !$has_quotes;
if (!$has_quotes)
$x++;
} else if ($has_quotes) {
$array[$x] .= $my_char;
}
}
print_r($array);
}
$String = '(("HELLO"("BAR")("FOO")()""))';
$Inc = 0;
(my_recursion($String, $Inc));
To get rid of the errors, add this line to the start of your function:
$array = array();
and replace this line:
$array[$x] .= $my_char;
with this:
$array[$x] = isset($array[$x])? $array[$x].$my_char : $my_char;
For help with your recursion, you'll need to describe its desired behavior.

Categories