CRC Maxim/Dallas 1Wire Generation: translate Visual Basic to PHP - php

i Need a function that generates the Maxim/Dallas 1-Wire CRC 8bit-Code like this page:
http://www.datastat.com/sysadminjournal/maximcrc.cgi
For that problem I found an little Excel Calculator for solution.
It works correctly, but I got the Problem to translate it into PHP language.
Original VB Code:
Private Sub ROMCRC_Click()
Dim InHex, OutBinStr As String
Dim OutBinArr(1 To 56) As Integer
Dim OutDec, i, CRC(1 To 8), CRCTemp As Integer
InHex = Range("ROMByte1").Value & Range("ROMByte2").Value & Range("ROMByte3").Value & Range("ROMByte4").Value & Range("ROMByte5").Value & Range("ROMByte6").Value & Range("ROMByte7").Value
OutBinStr = HexToBin(InHex)
' Convert string to array, LSB = OutBinArr(1)
For i = 1 To 56
OutBinArr(57 - i) = Mid$(OutBinStr, i, 1) ' Split(OutBinStr)
Next i
'Initialize CRC
For i = 1 To 8
CRC(i) = 0
Next i
'Calculate CRC
For i = 1 To 56
CRCTemp = CRC(1) Xor OutBinArr(i)
CRC(1) = CRC(2)
CRC(2) = CRC(3)
CRC(3) = CRC(4) Xor CRCTemp
CRC(4) = CRC(5) Xor CRCTemp
CRC(5) = CRC(6)
CRC(6) = CRC(7)
CRC(7) = CRC(8)
CRC(8) = CRCTemp
Next i
DecCRC = BinToDec(CRC)
Range("ROMCRCValue").Value = DecCRC
End Sub
Private Function HexToBin(hstr)
'convert hex string to binary string
cnvarr = Array("0000", "0001", "0010", "0011", _
"0100", "0101", "0110", "0111", "1000", _
"1001", "1010", "1011", "1100", "1101", _
"1110", "1111")
bstr = ""
For i = 1 To Len(hstr)
hdgt = Mid(hstr, i, 1)
cix = CInt("&H" & hdgt)
bstr = bstr & cnvarr(cix)
Next
HexToBin = bstr
End Function
Function BinToDec(bstr)
'convert 8 bit Binary number to Decimal
Dim j, Out As Integer
Out = 0
For j = 1 To 8
Out = Out + bstr(j) * 2 ^ (j - 1)
Next j
BinToDec = Out
End Function
My PHP test code:
protected function HexToBin($hstr) {
//convert hex string to binary string
$cnvarr = array(
'0000',
'0001',
'0010',
'0011',
'0100',
'0101',
'0110',
'0111',
'1000',
'1001',
'1010',
'1011',
'1100',
'1101',
'1110',
'1111'
);
$bstr = "";
for ($i = 1; $i <= strlen($hstr); $i++) {
$hdgt = substr($hstr, $i, 1);
$cix = intval($hdgt);
echo $cix.'|';
$bstr .= $cnvarr[$cix];
}
return $bstr;
}
protected function createHash($data) {
//$OutBinStr = $this->HexToBin($data);
$OutBinStr = hex2bin($data);
$OutBinArr = array();
$crc = array();
//Convert string to array, LSB = OutBinArr(1)
for ($i = 1; $i <= 56; $i++) {
$OutBinArr[57 - $i] = substr($OutBinStr, $i - 1, 1);
}
//initialize crc
for ($i = 1; $i <= 8; $i++) {
$crc[$i] = 0;
}
// calculate
for ($i = 1; $i <= 56; $i++) {
$CRCTemp = $crc[1] ^ $OutBinArr[$i];
$crc[1] = $crc[2];
$crc[2] = $crc[3];
$crc[3] = $crc[4] ^ $CRCTemp;
$crc[4] = $crc[5] ^ $CRCTemp;
$crc[5] = $crc[6];
$crc[6] = $crc[7];
$crc[7] = $crc[8];
$crc[8] = $CRCTemp;
}
return implode('', $crc);
}
Example call:
$buttonId = '0000145D6E0F01';
echo $this->createHash($buttonId);
I thank you if you have any Ideas why the Script does not generate the correct crc.
Thank you, nice regards
Michael

Your HexToBin() was generating wrong string. The for loop for substr() started with 1 instead of 0. See substr manual.
Imploding $crc resulted an reverse bit order value.
<?PHP
function HexToBin($hstr){
$hstr = str_replace(' ','',$hstr);
$hArr = str_split($hstr);
$bstr = "";
foreach($hArr AS $c){
$cix = sprintf("0x0{$c}");
// echo sprintf("%04b|",hexdec($cix));
$bstr .= sprintf("%04b",hexdec($cix));
}
return $bstr;
}
function createHash($data){
$OutBinStr = HexToBin($data);
$crc = array_fill(0,8,0);
$OutBinArr = array_reverse(str_split($OutBinStr));
// calculate
foreach($OutBinArr AS $i){
$CRCTemp = $crc[0] ^ $i;
$crc[0] = $crc[1];
$crc[1] = $crc[2];
$crc[2] = $crc[3] ^ $CRCTemp;
$crc[3] = $crc[4] ^ $CRCTemp;
$crc[4] = $crc[5];
$crc[5] = $crc[6];
$crc[6] = $crc[7];
$crc[7] = $CRCTemp;
}
$crc = array_reverse($crc);
return implode('', $crc);
}
$buttonId = '00 00 14 5D 6E 0F 01'; // 59 => 3B
// $buttonId = '12 AA 12 5F 14 A2 12 12'; // 109 => 6D
$crc = bindec(createHash($buttonId));
printf("\n%d => %X",$crc,$crc);
?>

Related

Get nearest sequence result from an array and given pattern with PHP

I am trying to get year and month from the letters using established sequence. I know that the sequence is based on the following letters:
$letters = array('B','C','D','F','G','H','J','K','L','M','N','P','R','S','T','V','W','X','Y','Z');
It started with 0000BBB and when it reaches 9999 it becomes BBC, BBD etc. So I don't need the numbers in that case and only letters as I have a list of last registered sequence per year and month like this:
$plates = array(
array('2018','KHF','KHX','KJV','KKN','KLM','KML','KNK','KPD','KPR','KPT','----','----'),
array('2017','JWN','JXF','JYB','JYT','JZP','KBM','KCH','KCV','KDK','KFB','KFV','KGN'),
array('2016','JLN','JMF','JMY','JNR','JPK','JRG','JRZ','JSL','JTB','JTR','JVH','JVZ'),
array('2015','JCK','JCY','JDR','JFG','JFW','JGP','JHJ','JHT','JJH','JJW','JKK','JKZ'),
array('2014','HVN','HVZ','HWM','HXB','HXN','HYD','HTY','HZB','HZL','HZZ','JBL','JBY'),
array('2013','HNT','HPC','HPN','HPY','HRK','HRX','HSK','HSR','HSZ','HTK','HTV','HVF'),
array('2012','HJC','HJM','HKB','HKL','HKX','HLK','HLW','HMD','HML','HMT','HNC','HNK'),
array('2011','HBP','HCB','HCR','HDC','HDR','HFF','HFT','HGC','HGM','HGX','HHH','HHT'),
array('2010','GTC','GTS','GVM','GWC','GWV','GXP','GYD','GYM','GYX','GZJ','GZT','HBG'),
array('2009','GKS','GLC','GLP','GMC','GMN','GNF','GNY','GPJ','GPW','GRM','GSC','GSR'),
array('2008','FZR','GBN','GCK','GDH','GFC','GFY','GGV','GHG','GHT','GJJ','GJV','GKH'),
array('2007','FKY','FLV','FNB','FNZ','FRC','FSJ','FTP','FVJ','FWC','FXB','FXY','FYY'),
array('2006','DVW','DWT','DXZ','DYY','FBC','FCJ','FDP','FFK','FGF','FHD','FJD','FKC'),
array('2005','DFZ','DGX','DHZ','DKB','DLD','DMJ','DNP','DPK','DRG','DSC','DTB','DVB'),
array('2004','CRV','CSS','CTT','CVR','CWR','CXT','CYY','CZP','DBJ','DCH','DDG','DFF'),
array('2003','CDV','CFM','CGJ','CHF','CJC','CKB','CLD','CLV','CMM','CNK','CPF','CRC'),
array('2002','BSL','BTF','BTZ','BVW','BWT','BXP','BYP','BZF','BZV','CBP','CCH','CDC'),
array('2001','BFJ','BGF','BHG','BJC','BKB','BLC','BMF','BMW','BNL','BPG','BRB','BRT'),
array('2000','---','---','---','---','---','---','---','---','BBJ','BCD','BCY','BDR')
);
That means that array index 0 is the year and from 1 to 12 would be month. I am trying to find a match but then realize I can not search exact value and need to look for nearest value based on letters.
I would deeply appreciate if anyone could direct me in right direction what would be the best method of doing this.
This is a test so far but this will just return an exact match, I would have to search any possible letters such as KHW as an example that would have to match as nearest value to KHX
foreach ($plates as $key => $val) {
$search = array_search('KHX', $plates[$key]);
if($search){
echo $search."\n";
echo $plates[$key][0];
break;
}
}
You can solve it with O(log n) with a binary search. But in a more straightforward solution, you can solve it with O(n).
You can calculate the difference between each word with the below algorithm.
‍‍
<?php
function strToInt($str)
{
$result = 0;
for ($i = 0; $i < strlen($str); $i++) {
$result = $result * 100 + ord($str[$i]);
}
return $result;
}
function find($searchStr)
{
$plates = [
['2018','KHF','KHX','KJV','KKN','KLM','KML','KNK','KPD','KPR','KPT','----','----'],
['2017','JWN','JXF','JYB','JYT','JZP','KBM','KCH','KCV','KDK','KFB','KFV','KGN'],
['2016','JLN','JMF','JMY','JNR','JPK','JRG','JRZ','JSL','JTB','JTR','JVH','JVZ'],
['2015','JCK','JCY','JDR','JFG','JFW','JGP','JHJ','JHT','JJH','JJW','JKK','JKZ'],
['2014','HVN','HVZ','HWM','HXB','HXN','HYD','HTY','HZB','HZL','HZZ','JBL','JBY'],
['2013','HNT','HPC','HPN','HPY','HRK','HRX','HSK','HSR','HSZ','HTK','HTV','HVF'],
['2012','HJC','HJM','HKB','HKL','HKX','HLK','HLW','HMD','HML','HMT','HNC','HNK'],
['2011','HBP','HCB','HCR','HDC','HDR','HFF','HFT','HGC','HGM','HGX','HHH','HHT'],
['2010','GTC','GTS','GVM','GWC','GWV','GXP','GYD','GYM','GYX','GZJ','GZT','HBG'],
['2009','GKS','GLC','GLP','GMC','GMN','GNF','GNY','GPJ','GPW','GRM','GSC','GSR'],
['2008','FZR','GBN','GCK','GDH','GFC','GFY','GGV','GHG','GHT','GJJ','GJV','GKH'],
['2007','FKY','FLV','FNB','FNZ','FRC','FSJ','FTP','FVJ','FWC','FXB','FXY','FYY'],
['2006','DVW','DWT','DXZ','DYY','FBC','FCJ','FDP','FFK','FGF','FHD','FJD','FKC'],
['2005','DFZ','DGX','DHZ','DKB','DLD','DMJ','DNP','DPK','DRG','DSC','DTB','DVB'],
['2004','CRV','CSS','CTT','CVR','CWR','CXT','CYY','CZP','DBJ','DCH','DDG','DFF'],
['2003','CDV','CFM','CGJ','CHF','CJC','CKB','CLD','CLV','CMM','CNK','CPF','CRC'],
['2002','BSL','BTF','BTZ','BVW','BWT','BXP','BYP','BZF','BZV','CBP','CCH','CDC'],
['2001','BFJ','BGF','BHG','BJC','BKB','BLC','BMF','BMW','BNL','BPG','BRB','BRT'],
['2000','---','---','---','---','---','---','---','---','BBJ','BCD','BCY','BDR']
];
$minYear = null;
$minKey = null;
$minDiff = strToInt('ZZZ');
$searchInt = strToInt($searchStr);
for ($i = 0; $i < count($plates); $i++) {
for ($j = 1; $j < 13; $j++) {
if(abs($searchInt - strToInt($plates[$i][$j])) < $minDiff) {
$minDiff = abs($searchInt - strToInt($plates[$i][$j]));
$minYear = $plates[$i][0];
$minKey = $plates[$i][$j];
}
}
}
return [$minYear, $minKey];
}
print_r(find('KHW'));
The code down below is by no means optimized, but it's rather a concept of how you might solve your problem.
//Flatten out array (one dimension without years and ----)
$flatten = array();
foreach($plates as $platevalues) {
foreach($platevalues as $pv) {
if ($pv != '---' && $pv != '----' && intval($pv) == 0) {
//Create a string only if valid letters included in the $letters-array
//This string is then added to the new array that is flattened out
$pv2 = '';
for($i=0;$i<strlen($pv);$i++) {
$letter = substr($pv,$i,1);
if (in_array($letter, $letters) !== false) {
$pv2 .= $letter;
}
}
$flatten[] = $pv2;
}
}
}
//Do the search
$search = 'GWN';
$search_result = '';
//Create a new search string based on first found in flattened
//plates array (first G, then GW, then GWN)
for($i=0;$i<strlen($search);$i++) {
foreach($flatten as $key=>$f) {
if (substr($search,0,$i+1) == substr($f,0,$i+1)) {
$search_result .= substr($search,$i,1);
break;
}
}
}
/*
$search_result is: GW
*/
//Create a new array where all items that begins with GW are included
$result = [];
foreach($flatten as $key=>$item) {
if (substr($search_result,0,strlen($search_result)) ==
substr($item,0,strlen($search_result))) {
$result[] = $item;
}
}
/*
$result =
array (size=2)
0 => string 'GWC' (length=3)
1 => string 'GWV' (length=3)
*/
//Create an array with total ASCII-value for each item
//in the $result array above
$result_o = [];
foreach($result as $item) {
$o = 0;
for($i=0;$i<strlen($item);$i++) {
$o += ord(substr($item,$i,1));
}
$result_o[]= $o;
}
/*
$result_o =
array (size=2)
0 => int 225
1 => int 244
*/
//Get the total ASCII-value for the original search string
$search_o = 0;
for($i=0;$i<strlen($search);$i++) {
$search_o += ord(substr($search,$i,1));
}
/*
$search_ o = 236
*/
//Find closest value in the $result_o (ASCII) - array compared (225,244)
//to the original $search_o ASCII value above (236)
$closest = 0;
$use_key = 0;
foreach($result_o as $key=>$item) {
if ($closest == 0 || abs($search_o - $closest) > abs($item - $search_o)) {
$closest = $item;
$use_key = $key;
}
}
/*
$closest = 244 (it's closer to 236 than 225 is)
$use_key = 1
*/
To get the result you have:
/*
$result =
array (size=2)
0 => string 'GWC' (length=3)
1 => string 'GWV' (length=3)
*/
//This should print out GWV
echo 'result=' . $result[$use_key];

Convert VB6 code to PHP

I want to convert Visual Basic 6 Code to PHP Code. I am new to PHP please help me to convert my VB6 Code to PHP. So far I tried to convert this into php code when I tried the code there is an error in the "
CryptRC4 = CryptRC4 & Chr$((pvCryptXor(baS((CLng(baS(li)) + baS(lJ)) Mod 256), Asc(Mid$(sText, lIdx, 1)))));
part and also I don't know how to proceed to the sub functions. Please see the codes below. The vb code is used to encrypt strings. I want to convert it to php format.
PHP Code
<?php
function CryptRC4($sText,$sKey){
$baS = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255);
$baK = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255);
$bytSwap = 0;
$li = 0;
$lJ = 0;
$lIdx = 0;
for( $lIdx = 0; $lIdx < 256; $lIdx++){
$baS[$lIdx] = $lIdx;
$baK[$lIdx] = ord(substr($sKey, 1 + ($lIdx % strlen($sKey)), 1));
}
for($li = 0; $li < 256; $li++){
$lJ = ($baS[$li] + $baK[$li]) % 256;
$bytSwap = $baS[$li];
$baS[$li] = $baS[$lJ];
$baS[$lJ] = $bytSwap;
}
$li = 0;
$lJ = 0;
$data_str = "";
for($lIdx = 0; $lIdx < strlen($sText); $lIdx++){
$li = ($li + 1) % 256;
$lJ = ($lJ + $baS[$li]) % 256;
$bytSwap = $baS[$li];
$baS[$li] = $baS[$lJ];
$baS[$lJ] = $bytSwap;
#echo chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1)))));
$data_str .= chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1)))));
}
echo $data_str;
}
function pvCryptXor($li, $lJ){
if($li = $lJ){
$pcx = $lJ;
}
else {
$pcx = $li Xor $lJ;
}
return $pcx;
}
unction ToHexDump($sText) {
$lIdx;
for($lIdx = 1; $lIdx < strlen($sText); $lIdx++){
$thd .= Right$("0" & Hex(Asc(Mid(sText, lIdx, 1))), 2)
echo $thd;
}
return $thd;
}
FromHexDump("events");
function FromHexDump($sText) {
$fhd = "";
for($lIdx = 0; $lIdx < strlen($sText); $lIdx++){
$fhd .= chr(CLng("&H" & Mid(sText, lIdx, 2)));
}
return $fhd;
}
?>
VB Code:
Public Function CryptRC4(sText As String, sKey As String) As String
On Error Resume Next
Dim baS(0 To 255) As Byte
Dim baK(0 To 255) As Byte
Dim bytSwap As Byte
Dim li As Long
Dim lJ As Long
Dim lIdx As Long
For lIdx = 0 To 255
baS(lIdx) = lIdx
baK(lIdx) = Asc(Mid$(sKey, 1 + (lIdx Mod Len(sKey)), 1))
Next
For li = 0 To 255
lJ = (lJ + baS(li) + baK(li)) Mod 256
bytSwap = baS(li)
baS(li) = baS(lJ)
baS(lJ) = bytSwap
Next
li = 0
lJ = 0
For lIdx = 1 To Len(sText)
li = (li + 1) Mod 256
lJ = (lJ + baS(li)) Mod 256
bytSwap = baS(li)
baS(li) = baS(lJ)
baS(lJ) = bytSwap
CryptRC4 = CryptRC4 & Chr$((pvCryptXor(baS((CLng(baS(li)) + baS(lJ)) Mod 256), Asc(Mid$(sText, lIdx, 1)))))
Next
End Function
Private Function pvCryptXor(ByVal li As Long, ByVal lJ As Long) As Long
On Error Resume Next
If li = lJ Then
pvCryptXor = lJ
Else
pvCryptXor = li Xor lJ
End If
End Function
Public Function ToHexDump(sText As String) As String
On Error Resume Next
Dim lIdx As Long
For lIdx = 1 To Len(sText)
ToHexDump = ToHexDump & Right$("0" & Hex(Asc(Mid(sText, lIdx, 1))), 2)
Next
End Function
Public Function FromHexDump(sText As String) As String
On Error Resume Next
Dim lIdx As Long
For lIdx = 1 To Len(sText) Step 2
FromHexDump = FromHexDump & Chr$(CLng("&H" & Mid(sText, lIdx, 2)))
Next
End Function
I revised your updated code and it seems you only had a few minor errors in it, look and my changes:
I guess you can use the build in PHP function hex2bin and bin2hex instead fo you own hex conversion.
function CryptRC4($sText,$sKey){
$baS = range(0, 255); // you can use range instead of your manual arrays
$baK = range(0, 255);
$bytSwap = 0;
$li = 0;
$lJ = 0;
$lIdx = 0;
for( $lIdx = 0; $lIdx < 256; $lIdx++){
$baS[$lIdx] = $lIdx;
$baK[$lIdx] = ord(substr($sKey, 1 + ($lIdx % strlen($sKey)), 1));
}
for($li = 0; $li < 256; $li++){
$lJ = ($baS[$li] + $baK[$li]) % 256;
$bytSwap = $baS[$li];
$baS[$li] = $baS[$lJ];
$baS[$lJ] = $bytSwap;
}
$li = 0;
$lJ = 0;
$data_str = "";
for($lIdx = 0; $lIdx < strlen($sText); $lIdx++){
$li = ($li + 1) % 256;
$lJ = ($lJ + $baS[$li]) % 256;
$bytSwap = $baS[$li];
$baS[$li] = $baS[$lJ];
$baS[$lJ] = $bytSwap;
#echo chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1)))));
$data_str .= chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1)))));
}
return $data_str; // changed from echo to return
}
function pvCryptXor($li, $lJ){
if($li == $lJ){ // you had an error here, use == to compare instead of a single =
$pcx = $lJ;
}
else {
$pcx = $li ^ $lJ; // XOR function in PHP is the ^ operator
}
return $pcx;
}
$str_hex = bin2hex("events");
$str_enc = CryptRC4($str_hex,"password");
$str_dec = hex2bin(CryptRC4($str_enc,"password"));
echo $str_hex . PHP_EOL . $str_enc . PHP_EOL . $str_dec;
OUTPUT:
6576656e7473
'�����~i��
events
So it seems to me as it's actually encoding and decoding correctly!?
It seems the original VB6 implementation of CryptRC4 function is from my answer to "VB6 encrypt text using password" question on SO.
So let me try answering your Q with this short php implementation of all public functions in the VB6 snippet:
function CryptRC4($text, $key) {
return openssl_encrypt($text, "RC4-40", $key, 1 | 2);
}
function ToHexDump($text) {
return strtoupper(bin2hex($text));
}
function FromHexDump($text) {
return hex2bin($text);
}
You can excercise these one-liners with something like this:
$text = "a message here";
$password = "password";
$encr = ToHexDump(CryptRC4($text, $password));
$decr = CryptRC4(FromHexDump($encr), $password);
echo $text . PHP_EOL . $encr . PHP_EOL . $decr;
As it's intended for passwords, you can save yourself a lot of hassle. PHP has got built in functions (version 5.5 and newer) that are designed for dealing with the hashing of passwords and for verifying hashed passwords against the password submitted by a user. Have a read through the PHP relevant PHP manual pages http://php.net/manual/en/book.password.php

Information integrity gets lost when P and Q receive 4-digit cousins

I have the following problem. When P and Q receive 4-digit prime values, the integrity of the information falls apart at the time of encoding and decoding it. Does anyone know why?
I'm breaking the blocks smaller than N.
Gen Q and P
gen p: 1009 and gen q: 1013
Gen oN and N
gen N: 1022117 and gen oN: 1020096
Gen E
gen E: 983743
Gen D
gen D: 695359
Pré encrypt
Array ( [0] => 116104 [1] => 111109 [2] => 115111 [3] => 110 )
encrypting
116104 ^ 983743 mod 1022117 =244657
111109 ^ 983743 mod 1022117 =623942
115111 ^ 983743 mod 1022117 =619197
110 ^ 983743 mod 1022117 =962437
decrypting
244657 ^ 695359 mod 1022117 =392979
623942 ^ 695359 mod 1022117 =856471
619197 ^ 695359 mod 1022117 =28780
962437 ^ 695359 mod 1022117 =455046
The RSA algorithm is too large to be put here. And even if I put a part of it would not work because there are many dependencies.
The algorithm below works with cousins up to 3
Gen Q and P
gen p: 109 and gen q: 113
Gen oN and N
gen N: 12317 and gen oN: 12096
Gen E
gen E: 3889
Gen D
gen D: 1297
Pré codificando
Array ( [0] => 116 [1] => 104 [2] => 111 [3] => 109 [4] => 115 [5] => 111 [6] => 110 )
Encrypting
116 ^ 3889 mod 12317 =3604
104 ^ 3889 mod 12317 =6862
111 ^ 3889 mod 12317 =11992
109 ^ 3889 mod 12317 =5341
115 ^ 3889 mod 12317 =551
111 ^ 3889 mod 12317 =11992
110 ^ 3889 mod 12317 =8939
Decrypting
3604 ^ 1297 mod 12317 =116
6862 ^ 1297 mod 12317 =104
11992 ^ 1297 mod 12317 =111
5341 ^ 1297 mod 12317 =109
551 ^ 1297 mod 12317 =115
11992 ^ 1297 mod 12317 =111
8939 ^ 1297 mod 12317 =110
class gen keys
/*
* Algoritimo RSA
* 26/11/2016 13:01
* O quinto poder
*/
class keys {
private $p;
private $q;
private $n;
private $oN;
private $e;
private $d;
private $keyPri;
private $keyPub;
function __construct() {
$this->randoPnQ();
$this->genN();
$this->genE();
$this->genD();
$this->applyKeys();
}
private function ifPrime($val) {
$event = 0;
for ($i = 1; $i <= $val; $i ++) {
(($val % $i) == 0) ? $event ++ : $event;
}
($event == 2) ? $answer = true : $answer = false;
return $answer;
}
private function randoPnQ() {
echo '<br><strong>Gerando P</strong><br>';
$loop = true;
for ($i = 0; $loop != false; $i++) {
$this->p = rand(rand(100, 109), rand(110, 119));
($this->ifPrime($this->p)) ? $loop = false : $loop;
}
echo '<br><strong>Gerando Q</strong><br>';
$loop = true;
$this->q = $this->p;
for ($i = 0; $loop != false; $i ++) {
$this->q ++;
($this->ifPrime($this->q)) ? $loop = false : $loop;
}
//$this->p = 17;
//$this->q = 23;
echo "gen p: $this->p and gen q: $this->q<br>";
}
private function genN() {
echo '<br><strong>Gerando N</strong><br>';
$this->n = $this->p * $this->q;
echo '<br><strong>Gerando oN</strong><br>';
$this->oN = ($this->p - 1) * ($this->q - 1);
echo "gen N: $this->n and gen oN: $this->oN<br>";
}
private function fato($val) {
$d = [1];
for ($i = 2; $i <= $val; $i ++) {
(($val % $i) == 0 ) ? array_push($d, $i) : $d;
}
return $d;
}
private function genE() {
echo '<br><strong>Gerando E</strong><br>';
$doN = $this->fato($this->oN);
$voN = count($doN);
$loop = true;
for ($i = 0; $loop != false; $i ++) {
$e = rand(1, $this->oN);
$de = $this->fato($e);
$diff = array_diff_assoc($doN, $de);
if (count($diff) == ($voN - 1)) {
$this->e = $e;
$loop = false;
}
}
//$this->e = 3;
echo "gen E: $this->e<br>";
}
private function genD() {
$loop = true;
for ($i = 1; $loop != false; $i ++) {
$d = ($i * $this->e) % $this->oN;
if ($i >= 1 * 10 ** 100) {
$loop = false;
echo '<h2>Limite excedido!</h2>';
} elseif ($d == 1) {
if (strlen($i) > strlen($this->e)) {
$this->randoPnQ();
$this->genN();
$this->genE();
$i = 1;
} else {
$loop = false;
$this->d = $i;
}
}
}
echo '<br><strong>Gerando D</strong><br>';
echo "gen D: $this->d<br>";
}
private function applyKeys() {
echo '<br><strong>Gerando keys</strong><br>';
$this->keyPri = [$this->e, $this->n];
$this->keyPub = [$this->d, $this->n];
echo 'apply Keys';
}
public function getkeys() {
return [$this->keyPri, $this->keyPub];
}
}
class pre-coded
<?php
class codnDec {
function __costruct() {
}
private function rstr($str, $op) {
switch ($op) {
case 'c':
return htmlentities($str, ENT_QUOTES);
break;
case 'd':
return html_entity_decode($str);
break;
default:
return htmlentities($str, ENT_QUOTES);
break;
}
}
private function rgroup($cod) {
$loop = true;
while ($loop != false) {
if (strlen($cod) == 3) {
$loop = false;
} else {
$cod .= '0';
}
}
return $cod;
}
public function cod($str) {
$str = $this->rstr($str, 'c');
$aStr = str_split($str);
$vArray = count($aStr);
$cAscii = null;
for ($i = 0; $i < $vArray; $i ++) {
$cAscii .= ord($aStr[$i]);
}
$aAscii = str_split($cAscii, 3);
$r = end($aAscii);
$key = array_search($r, $aAscii);
$aAscii[$key] = $this->rgroup($r);
return $aAscii;
}
public function dec($cod) {
$r = null;
$vCod = strlen($cod);
for ($i = 0; $i <= $vCod; $i += 2) {
$temp = intval(substr($cod, $i, 2));
if ($temp < 32) {
$temp = intval(substr($cod, $i, 3));
if ($temp > 128) {
echo "Assumption error";
die;
}
$i++;
}
$r .= chr($temp);
}
return $this->rstr($r, 'd');
}
}
Call script. In this script the calculation is done to code and then to decode. Facilitating the analysis process.
<?php
/*
* Include scripts aux.
*/
include_once 'gen-keys.php';
include_once 'codndec.php';
class rsa {
/* Array d and n */
public $privkey;
/* Array e and n */
public $publkey;
function __construct() {
/* Getting keys, pivate and public */
$keys = new keys();
$akeys = $keys->getkeys();
$this->privkey = $akeys[0];
$this->publkey = $akeys[1];
}
public function encrypt($msg) {
$codndec = new codndec();
echo '<br><strong>Pré codificando</strong><br>';
$msgCod = $codndec->cod($msg);
print_r($msgCod);
echo '<br><strong>Obtendo chaves</strong><br>';
$e = $this->privkey[0];
$d = $this->publkey[0];
$n = $this->privkey[1];
echo '<br><strong>codificando 1/2</strong><br>';
$x = count($msgCod);
for ($i = 0; $i < $x; $i ++) {
echo "<br> $msgCod[$i] ^ $e mod $n =" . $msgCod[$i] = bcpowmod($msgCod[$i], $e, $n);
}
echo '<br>';
for ($i = 0; $i < $x; $i ++) {
echo "<br> $msgCod[$i] ^ $d mod $n =" . $msgCod[$i] = bcpowmod($msgCod[$i], $d, $n);
}
echo '<br>' . $c = implode('', $msgCod);
echo '<br><strong>codificando 2/2</strong><br>';
return $cod = $codndec->dec($c);
}
}
$str = 'thomson';
echo '<strong>Mensagem: </strong>' . $str;
echo '<br>';
echo '<br>';
$rsa = new rsa();
echo '<strong>Mensagem criptografada: </strong>' . $rsa->encrypt($str);
echo '<br>';
echo '<br>';
echo '<strong>Private key is: </strong>' . implode('*', $rsa->privkey);
echo '<br>';
echo '<br>';
echo '<strong>Public key is: </strong>' . implode('*', $rsa->publkey);
/*
$rsa = new rsa();
$keys = $rsa->getkeys();
echo 'Private key: ' . $keys[0][0] . '%' . $keys[0][1];
echo 'Public key: ' . $keys[1][0] . '%' . $keys[1][1];
*/

Vigenere in PHP

could anyone help me fix this Vigenere cypher in PHP?
Sorry for the ripped up code, that's from where I have been dissecting it for hours - trying to fix!
Anyhow, the code outputs 'Ace' when it should output 'Abc'.
There is some weird double offset which I don't have the maths brain to fix! Thanks for reading.
The code originates from here in AutoHotkey script - I have attempted to transcribe it. There are PHP Vigenere examples on the web (although not on Rosetta Code, weirdly!).. but anyhow, this one is modified to accept lower case as well as the standard capitals. Thanks.
$key = "AAA";
$keyLength = 3;
$keyIndex = 0;
$messageAsArray[0] = "A";
$messageAsArray[1] = "b";
$messageAsArray[2] = "c";
foreach ($messageAsArray as $value) //Loop through input string array
{
$thisValueASCII = ord($value);
if ($thisValueASCII >= 65 && $thisValueASCII <= 90) //if is uppercase
{
$thisValueASCIIOffset = 65;
}
else //if is lowercase
{
$thisValueASCIIOffset = 97;
}
$thisA = $thisValueASCII - $thisValueASCIIOffset;
$thisB = fmod($keyIndex,$keyLength);
$thisC = substr($key, $thisB, 1);
$thisD = ord($thisC) - 65;
$thisE = $thisA + $thisD;
$thisF = fmod($thisE,26);
$thisG = $thisF + $thisValueASCII ;
$thisOutput = chr($thisG);
$output = $output . $thisOutput ;
$keyIndex++;
}
echo $output
Ok, I read your code.
You're encoding, and your error is quite simple :
$thisG = $thisF + $thisValueASCII ;
In this step, $thisF is your encrypted letter, which value is between 0 and 25. You want to print it as an ascii char and, instead of adding the offset, you're adding the uncrypted ascii value, which makes no sense.
You should have :
$thisG = $thisF + $thisValueASCIIOffset;
A few tips.
You don't need to have your text or key as an array, you can use it as if it was one.
You can use the % operator instead of fmod. Makes the code easier to read, but it is just a personnal preference.
For instance :
$key = "AAA";
$keyLength = strlen($key);
$keyIndex = 0;
$message = str_split("Abc");
$output = '';
foreach($message as $value) // Loop through input string array
{
$thisValueASCII = ord($value);
if($thisValueASCII >= 65 && $thisValueASCII <= 90) // if is uppercase
{
$thisValueASCIIOffset = 65;
} else // if is lowercase
{
$thisValueASCIIOffset = 97;
}
$letter_value_corrected = $thisValueASCII - $thisValueASCIIOffset;
$key_index_corrected = $keyIndex % $keyLength; // This is the same as fmod but I prefer this notation.
$key_ascii_value = ord($key[$key_index_corrected]);
if($key_ascii_value >= 65 && $key_ascii_value <= 90) // if is uppercase
{
$key_offset = 65;
} else // if is lowercase
{
$key_offset = 97;
}
$final_key = $key_ascii_value - $key_offset;
$letter_value_encrypted = ($letter_value_corrected + $final_key)%26;
$output = $output . chr($letter_value_encrypted + $thisValueASCIIOffset);
$keyIndex++;
}
echo $output;
Have fun and good luck for your implementation !

PHP How can I get HEX Color code from array of R + G + B?

I have an array:
Array
(
[red] => 252
[green] => 168
[blue] => 166
[alpha] => 0
)
It's an output of function imagecolorsforindex.
How can I get a HTML code from these elements? For example: #99CCFF
Strictly speaking you can't, since alpha is not supported. But since the alpha is 0, we can assume that it won't matter. As such, pass each value into sprintf() with a format specifier of %02x for each element.
c = sprintf('#%02x%02x%02x', val['red'], val['green'], val['blue']);
PHP Convert RGB from/to HTML hex color
rgb2html($array[0], $array[1], $array[2])
There's a function contributed in the comments of this page of the PHP manual.
<?PHP
function rgb2hex2rgb($c){
if(!$c) return false;
$c = trim($c);
$out = false;
if(preg_match("/^[0-9ABCDEFabcdef\#]+$/i", $c)){
$c = str_replace('#','', $c);
$l = strlen($c) == 3 ? 1 : (strlen($c) == 6 ? 2 : false);
if($l){
unset($out);
$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,1*$l));
$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 1*$l,1*$l));
$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 2*$l,1*$l));
}else $out = false;
}elseif (preg_match("/^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$/i", $c)){
$spr = str_replace(array(',',' ','.'), ':', $c);
$e = explode(":", $spr);
if(count($e) != 3) return false;
$out = '#';
for($i = 0; $i<3; $i++)
$e[$i] = dechex(($e[$i] <= 0)?0:(($e[$i] >= 255)?255:$e[$i]));
for($i = 0; $i<3; $i++)
$out .= ((strlen($e[$i]) < 2)?'0':'').$e[$i];
$out = strtoupper($out);
}else $out = false;
return $out;
}
?>
Output
#FFFFFF =>
Array{
red=>255,
green=>255,
blue=>255,
r=>255,
g=>255,
b=>255,
0=>255,
1=>255,
2=>255
}
#FFCCEE =>
Array{
red=>255,
green=>204,
blue=>238,
r=>255,
g=>204,
b=>238,
0=>255,
1=>204,
2=>238
}
CC22FF =>
Array{
red=>204,
green=>34,
blue=>255,
r=>204,
g=>34,
b=>255,
0=>204,
1=>34,
2=>255
}
0 65 255 => #0041FF
255.150.3 => #FF9603
100,100,250 => #6464FA
You can try this simple piece of code below.
$rgb = (123,222,132);
$rgbarr = explode(",",$rgb,3);
echo sprintf("#%02x%02x%02x", $rgbarr[0], $rgbarr[1], $rgbarr[2]);
This will return #7bde84

Categories