So usually the Diffie-Hellman key is 2048 bits as I understand but my computer can barely calculate a 10 digit number. What are some common numbers in Diffie-Hellman?
Here is my code that's suuuper slow:
$gen = 77;
$mod = 517165;
$saltA = 1233217;
$saltB = 5173123;
$calculatedSecretKeyA = gmp_mod(gmp_pow($gen, $saltA), $mod);
$calculatedSecretKeyB = gmp_mod(gmp_pow($gen, $saltB), $mod);
$calcKeyA = gmp_mod(gmp_pow($calculatedSecretKeyB, $saltA), $mod);
echo $calculatedSecretKeyB . "^" . $saltA . "" . " mod " . $mod . " = " . $calcKeyA;
$calcKeyB = gmp_mod(gmp_pow($calculatedSecretKeyA, $saltB), $mod);
echo $calculatedSecretKeyA . "^" . $saltB . "" . " mod " . $mod . " = " . $calcKeyB;
Use gmp_powm
gmp_powm ( GMP|int|string $num , GMP|int|string $exponent , GMP|int|string $modulus ) : GMP
for the below lines.
$calculatedSecretKeyA = gmp_powm($gen, $saltA, $mod);
$calculatedSecretKeyB = gmp_powm($gen, $saltB, $mod);
$calcKeyA = gmp_powm($calculatedSecretKeyB, $saltA, $mod);
$calcKeyB = gmp_powm($calculatedSecretKeyA, $saltB, $mod);
It uses the modular form of square-and-multiply technique. The intermediate values will never exceed mod^2. Also, it has O(log n) complexity.
Related
I am trying to do some Diffie-Hellman which works fine with smaller generator, mod and private keys. But when the numbers get to large it just outputs zero. I've looked at other posts with no real solution. Could it be configured in php.ini?
$gen = 877;
$mod = 80182923;
$saltA = 517;
$saltB = 1517;
echo "calculation of Ga og Gb<br>";
echo "<br>user A shareable value<br>";
$calculatedSecretKeyA = ($gen**$saltA) % $mod;
echo $gen . "^" . $saltA . " mod " . $mod . " = " . $calculatedSecretKeyA;
echo "<br>user B shareable value<br>";
$calculatedSecretKeyB = ($gen**$saltB) % $mod;
echo $gen . "^" . $saltB . " mod " . $mod . " = " . $calculatedSecretKeyB;
echo "<br>user As calc of secret key <br>";
$calcKeyA = ($calculatedSecretKeyB**$saltA) % $mod;
echo $calculatedSecretKeyB . "^" . $saltA . "" . " mod " . $mod . " = " . $calcKeyA;
echo "<br><br>user Bs calc of secret key <br>";
$calcKeyB = ($calculatedSecretKeyA**$saltB) % $mod;
echo $calculatedSecretKeyA . "^" . $saltB . "" . " mod " . $mod . " = " . $calcKeyB;
echo "<br><br>bruteforcer A calc of secret key <br>";
$calcKeyB = ($calculatedSecretKeyA**7) % $mod;
echo $calculatedSecretKeyA . "^" . 7 . "" . " mod " . $mod . " = " . $calcKeyB;
You can use GMP library for this (https://www.php.net/manual/en/ref.gmp.php):
echo "calculation of Ga og Gb<br>";
echo "<br>user A shareable value<br>";
$calculatedSecretKeyA = gmp_mod ( gmp_pow ($gen, $saltA), $mod);
echo $gen . "^" . $saltA . " mod " . $mod . " = " . $calculatedSecretKeyA;
Output:
calculation of Ga og Gbuser A shareable value877^517 mod 80182923 = 79127908
I am fairly new to ML and have no idea how things work, i have a webpage with search bar. and i need to use word2vec to complete user input in search bar.
i am using this code from https://github.com/donlinglok/C-word2vec-php
<?php
$txtfilepath = fopen("options.txt", "r") or die("Unable to open file!");
echo fread($txtfilepath, filesize("options.txt"));
//fclose($txtfilepath);
function train($txtfilepath) {
$cbow = 1;
$size = 500;
$window = 10;
$negative = 10;
$hs = 0;
$sample = "1e-5";
$threads = 40;
$binary = 1;
$iter = 3;
$min_count = 10;
exec ( "cd " . dirname ( __FILE__ ) . "/google-word2vec-trunk && ./word2vec " . " -train " . $txtfilepath . " -output " . dirname ( __FILE__ ) . "/vectors.bin " . " -cbow " . $cbow . " -size " . $size . " -window " . $window . " -negative " . $negative . " -hs " . $hs . " -sample " . $sample . " -threads " . $threads . " -binary " . $binary . " -iter " . $iter . " -min-count " . $min_count, $outputArray );
}
function distance($keyword) {
exec ( "cd " . dirname ( __FILE__ ) . " && ./distancecli " . dirname ( __FILE__ ) . "/vectors.bin " . $keyword, $outputArray );
if (isset ( $outputArray[0] )) {
return $outputArray;
} else {
return distance ( $keyword );
}
}
//train ( dirname ( __FILE__ ) . "/google-word2vec-trunk/questions-words.txt" );
echo json_encode ( distance ( "facebook" ), true );
?>
what should i do next ? how to run this code and get result in search bar? i ran it from the web folder http://localhost/trainmodel/word2vec.php and recieved this error Fatal error:
Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\trainmodel\word2vec.php on line 23
You can overcome this problem by increasing the max_execution_time value in the php settings.
I took the cookbook example and modified it to use add_textflow() instead of create_textflow() but I can't get it to work. Instead of interpreting the macros it just writes it as plain text. What am I doing wrong?
From documentation I got the understanding that I should define the macros inline - or did I misunderstand it? Have tried everything I could think of...
$tf = 0;
$optlist = "";
$llx = 100; $lly = 50; $urx = 450; $ury = 800;
$t_fontsize = 16; // font size of the text
$t_leading = 20; // leading of the text
$c_num = 3; // no. of lines for the drop cap to cover
$c_textrise = -(($c_num - 1) * $t_leading); // text rise of the drop cap
$c_fontsize = -($c_textrise * 1.8); // font size of the drop cap
try {
$p = new pdflib();
$p->set_option("errorpolicy=exception");
$p->set_option("stringformat=utf8");
$p->begin_document("", "");
$optlist = "fontname=Helvetica fontsize=" . $t_fontsize . " encoding=unicode ";
$text = "<macro " .
"{cap_start {fontsize=" . $c_fontsize . " leading=" . $t_leading .
" textrise=" . $c_textrise .
" matchbox={createwrapbox boxheight={leading textrise}}} " .
"cap_end {matchbox=end fontsize=" . $t_fontsize . " textrise=0}}>";
$text .= "<&cap_start>O<&cap_end>ver the mountains... ";
$tf = $p->add_textflow($tf, $text, $optlist);
$text =
"Paper Planes are the ideal way of " .
"passing the time. We offer revolutionary " .
"new developĀments of the traditional comĀmon " .
"paper planes. If your lesson, conference, or lecture " .
"turn out to be deadly boring, you can have a wonderful time " .
"with our planes. ";
$tf = $p->add_textflow($tf, $text, $optlist);
do {
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
$result = $p->fit_textflow($tf, $llx, $lly, $urx, $ury, "");
$p->end_page_ext("");
} while ($result == "_boxfull" || $result == "_nextpage");
/* Check for errors */
if (!$result == "_stop") {
if ($result == "_boxempty")
throw new Exception ("Error: Textflow box too small");
else {
throw new Exception ("User return '" . $result .
"' found in Textflow");
}
}
$p->delete_textflow($tf);
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=drop_caps.pdf");
print $buf;
} catch (PDFlibException $e) {
die("PDFlib exception occurred:\n".
"[" . $e->get_errnum() . "] " . $e->get_apiname() .
": " . $e->get_errmsg() . "\n");
} catch (Exception $e) {
die($e->getMessage());
}
$p = 0;
this is simple: only create_texflow() support inline options (because you must specify the data in a single chunk). With add_textflow() you have the possibility to "build" the textflow handle in multiple steps, for each text fragment with the related options.
In your case, it might look like the following way:
$optlistmacro = " macro " .
"{cap_start {fontsize=" . $c_fontsize . " leading=" . $t_leading .
" textrise=" . $c_textrise .
" matchbox={createwrapbox boxheight={leading textrise}}} " .
"cap_end {matchbox=end fontsize=" . $t_fontsize . " textrise=0}}";
$tf = $p->add_textflow($tf, "", $optlist . $optlistmacro);
$tf = $p->add_textflow($tf, "O", "&cap_start");
$tf = $p->add_textflow($tf, "ver the mountains... ", "&cap_end");
In this code fragment, I do the first add_textflow() without text but all the
macros settings. Then it place the "O" with the macro (&cap_start) in
the option list. The following text "ver the mountains... " is done
in the next add_textflow() and as option I place the &cap_end.
I am trying to display numbers retrieved from the database, in a specific format in a text box.
There are two ways in which the numbers can be displayed.
When the total numbers are 10
in database (4608061019) Expected output 46-0806-1019
When the total numbers are 13
in database (4608061019100) Expected output 46-0806-1019-100
My progress so far:
While saving the value into the database I am using
preg_replace("/[^0-9]/","",$string); // to make sure all hardcoded "-" are removed while storing.
One possible (regex-using) approach:
$str = '4608061019';
$formatted = preg_replace(
'/(^\d{2}|\d{4})(?!$)/', '$1-', $str);
// 46-0806-1019
Demo. This function doesn't check the string's length - it just adds a hyphen after each relevant sequence of symbols (2 right after the beginning, 4 afterwards).
Easy! If you are sure that there are only these two options, then you can do this way:
Convert the number to an array of numbers:
$number = str_split($number);
Check the length:
if (count($number) == 10)
$number = $number[0] . $number[1] . "-" . $number[2] . $number[3] . $number[4] . $number[5] . "-" . $number[6] . $number[7] . $number[8] . $number[9];
else if (count($number) == 13)
$number = $number[0] . $number[1] . "-" . $number[2] . $number[3] . $number[4] . $number[5] . "-" . $number[6] . $number[7] . $number[8] . $number[9] . "-" . $number[10] . $number[11] . $number[12];
Return the number:
return $number;
The full function here:
function tokenize($number)
{
$number = str_split($number);
if (count($number) == 10)
$number = $number[0] . $number[1] . "-" . $number[2] . $number[3] . $number[4] . $number[5] . "-" . $number[6] . $number[7] . $number[8] . $number[9];
else if (count($number) == 13)
$number = $number[0] . $number[1] . "-" . $number[2] . $number[3] . $number[4] . $number[5] . "-" . $number[6] . $number[7] . $number[8] . $number[9] . "-" . $number[10] . $number[11] . $number[12];
return $number;
}
Output
echo tokenize(4608061019);
echo tokenize(4608061019100);
Output
46-0806-1019
46-0806-1019-100
Fiddle: http://codepad.viper-7.com/EVWeFR
Another alternative solution:
$parts = array(2,4,4,3);
$string = "4608061019100";
$i = 0;
$newString = '';
foreach ($parts as $part) {
$newString.=substr($string, $i, $part) ."-";
$i = $i+$part;
}
$newString = rtrim($newString, "-");
Output is:
string '46-0806-1019-100' (length=16)
Works for 46-0806-1019 too.
try it:
//$number = '4608061019';
$number = '4608061019100';
function formatImportantNumber($theNumber){
$formatedNumber = '';
if(strlen($theNumber)==10){
//46-0806-1019
$formatedNumber = substr($theNumber, 0, 2).'-'.substr($theNumber, 2, 4).'-'.substr($theNumber, 6, 4);
}elseif(strlen($theNumber)==13){
//46-0806-1019-100
$formatedNumber = substr($theNumber, 0, 2).'-'.substr($theNumber, 2, 4).'-'.substr($theNumber, 6, 4).'-'.substr($theNumber, 10, 3);
}else{
die('Invalid number formated')
}
return $formatedNumber;
}
echo formatImportantNumber($number);
I am working on my first program written in PHP. This is a basic question. I am trying to print a string stored in an array. I have two arrays. One array, $content, stores a number at $content[$i][15] (i am using arrays in arrays, and looping through it with a for loop). Lets say this number is 3. I now want to access a string in another array, called $type. This string is at position $type[3]. Logically, I think, $type[3] should print the same thing as $type[$content[$i][15]]. But, when I call $type[$content[$i][15]], it doesn't print anything, but when i print $type[3] it works fine. Is this type of call not allowed in PHP?
$type = array();
$typetemp = readfile("type.csv");
foreach ($typetemp as $sa){
$type[$sa[0]] = $sa[1];
}
$content = readfile("content.csv");
for($i=0; $i<sizeof($content); $i++){
if($content[$i][15] == 3){
if($content[$i][4] == 1){
$temp = $content[$i][15];
echo "id is " . $content[$i][0] . "title is " . $content[$i][1] . "introtext is " . $content[$i][2] . "restoftext is " . $content[$i][3] . "visible is " . $content[$i][4] . "category is " . $content[$i][5] . "creation_date is " . $content[$i][6] . "author is " . $content[$i][7] . "img is " . $content[$i][8] . "ordering is " . $content[$i][9] . "rank is " . $content[$i][10] . "vid is " . $content[$i][11] . "start_date is " . $content[$i][12] . "stop_date is " . $content[$i][13] . "event_date is " . $content[$i][14] . "type is " . $content[$i][15] . "thumb is " . $content[$i][16] . "type name is " . $type[$temp] . "<br/>";
}
}
}
function readfile($filename) {
$line_of_text = array();
$file_handle = fopen($filename, "r");
while (!feof($file_handle) ) {
array_push($line_of_text, fgetcsv($file_handle, 1024));
}
fclose($file_handle);
return $line_of_text;
}
You are missing a $ sign before content:
$type[$content[$i][15]]
This accesses the value in $type with index $content[$i][15]