how to convert php codes to vb.net for loop - php

how do i convert this php code to vb
im trying to do this in .net but the value is only 1 value
what is the code of .net using my php codes
this is my code in php
$ctr = 7
for ($i = 0;$i < $ctr;$i++)
{
$prenum .= '0';
}
output = 0000000
Dim prenum as string = "0"
Dim ctr as Integer = 7
For i = 0 To ctr
output = prenum
Next
output = 0

the operator '.' in php concatenates strings, the operator for vb to concatenate is '&'...
Dim prenum as string = "0"
Dim ctr as Integer = 7
For i = 0 To ctr
output = output & prenum
Next

Related

PHP Converting Hex string to Byte Array - converting VB.Net to PHP

I'm writing a PHP website that is accessing an encrypted MySQL database. The database is currently a back end to a VB.Net Windows forms program. This is all working fine, but I want to the PHP website to access some of the data and be able to decrypt/encrypt it. The fields are encrypted using Blowfish code originally written by David Ireland in VB6 and converted by Todd Acheson with a few tweaks from myself.
With the PHP Blowfish examples I've seen, the $iv is set at random, but I need it to be set to the same as that created in VB, so I'm attempting to convert the VB code to PHP.
I've started converting the code line by line, and from a technical perspective, it seems OK, but testing the first part of it doesn't provide the same results as with VB
Setting the key:
Dim aKey() as Byte = cv_BytesFromHex(MySecretKey)
Public Function cv_BytesFromHex(ByVal sInputHex As String) As Object
' Returns array of bytes from hex string in big-endian order
' E.g. sHex="FEDC80" will return array {&HFE, &HDC, &H80}
Dim i As Long
Dim M As Long
Dim aBytes() As Byte
If Len(sInputHex) Mod 2 <> 0 Then
sInputHex = "0" & sInputHex
End If
M = Len(sInputHex) \ 2
ReDim aBytes(M - 1)
For i = 0 To M - 1
Dim x = "&H" & Mid(sInputHex, i * 2 + 1, 2)
Debug.Print(x + " " + Val(x).ToString)
aBytes(i) = Val(x)
Next
cv_BytesFromHex = aBytes 'CopyArray(aBytes)
Return cv_BytesFromHex
End Function
Converting this function to PHP5.
public function cv_BytesFromHex($inputstring)
{
// Returns array of bytes from hex string in big-endian order
// e.g. shex="fedc80" will return array {&hfe, &hdc, &h80}
$i=0;
$m=0;
if (strlen($inputstring)/2 <> (int)(strlen($inputstring)/2)) {
$inputstring = "0".$inputstring;
}
$m = strlen($inputstring)/2;
echo 'Length '.strlen($inputstring).' = '.$m." elements</br>";
$abytes=array_fill(0,$m-1,0) ;
for ($i=0; $i<=$m-1;$i++) {
$raw=substr($inputstring, $i * 2 , 2);
$hexed=hexdec($raw);
echo 'Raw ='.$raw.' = '.$hexed.'</br>';
$abytes[$i]=$hexed;
}
return $abytes;
}
Testing with the key "1check".
VB output:
&H1C 28
&Hhe 0
&Hck 12
PHP output:
Length 6 = 3 elements
Raw =1c = 28
Raw =he = 14
Raw =ck = 12
So.. in this example, "1C" and "ck" give me the same values, but "he" doesn't
another example:
key =10stack
vb
&H01 1
&H0s 0
&Hta 0
&Hck 12
php
Length 8 = 4 elements
Raw =01 = 1
Raw =0s = 0
Raw =ta = 10
Raw =ck = 12
This one works:
key =1234wxyz
vb
&H12 18
&H34 52
&Hwx 0
&Hyz 0
php
Raw =12 = 18
Raw =34 = 52
Raw =wx = 0
Raw =yz = 0
Does anyone know why?
so, there is no errors here. h is ignored by hexdec and only e is decoded. cause...
https://en.wikipedia.org/wiki/Hexadecimal there is no h :)
and in VBA Val function returns 0, cause he is not valid hex-combination
<?php
function myHex($str)
{
if ($str === dechex(hexdec($str))) {
return hexdec($str);
}
return 0;
}
var_dump(myHex("he")); // returns 0

How to convert decimal format to number format

When I convert this binary to decimal:
010000010110110001101001001000000100000101101000011011010110010101100100001000000100111101110011011011010110000101101110001000000110001001101001011100100111010001101000011001000110000101110100011001010011101000110001001011010011001100101101001100010011100100111000001101110010000001100011011011110111010101101110011101000111001001111001001110100111001101110101011001000110000101101110
using VB.Net, I get this double number: 1.00695950340148E+115.
I need to format it to be normal number like this:
10069595034014783469636931351646363690636553221072125712116008033168565762469882090200055592025448896479090348155246
How can I do that using PHP or VB.Net?
Public Class Test
Public Shared Sub Main()
Dim Bin as String = "010000010110110001101001001000000100000101101000011011010110010101100100001000000100111101110011011011010110000101101110001000000110001001101001011100100111010001101000011001000110000101110100011001010011101000110001001011010011001100101101001100010011100100111000001101110010000001100011011011110111010101101110011101000111001001111001001110100111001101110101011001000110000101101110"
Dim dec As Double = Nothing
Dim length As Integer = Len(Bin)
Dim temp As Integer = Nothing
Dim x As Integer = Nothing
For x = 1 To length
temp = Val(Mid(Bin, length, 1))
length = length - 1
If temp <> "0" Then
dec += (2 ^ (x - 1))
End If
Next
System.Console.WriteLine("Sum of x and y = " & dec)
End Sub
End Class
The output is:
Visual Basic.Net Compiler version 0.0.0.5943 (Mono 4.7 - tarball)
Copyright (C) 2004-2010 Rolf Bjarne Kvinge. All rights reserved.
Assembly 'jdoodle, Version=0.0, Culture=neutral, PublicKeyToken=null'
saved successfully to '/home/jdoodle.exe'. Compilation successful
Compilation took 00:00:01.1970230
Sum of x and y = 1.00695950340148E+115
please try with my this method, but I have not verified it whether it correct or not:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myBin = "110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111110101010100001111101010101010101000000110110101100000010111"
Debug.Print(ConvBinToDec(myBin))
End Sub
Function ConvBinToDec(ByVal theVal As String)
ConvBinToDec = ""
Dim BinVal(Len(theVal)) As String
BinVal(1) = "1"
Dim Total = "0"
If Mid(theVal, Len(theVal), 1) = "1" Then Total = "1"
Dim myLast As String = ""
For a = 2 To Len(theVal)
BinVal(a) = Multi(BinVal(a - 1))
myLast = BinVal(a)
If Mid(theVal, Len(theVal) - a + 1, 1) = "1" Then
Total = Adding(Total, myLast)
End If
Next
ConvBinToDec = Total
End Function
Function Multi(ByVal TheVal As String) As String
Dim simpan As Byte = 0
Multi = ""
Dim myLen As Byte = 10
Dim myTimes = -Int(-Len(TheVal) / myLen)
For a = 1 To myTimes
Dim myStr As String = Strings.Right(TheVal, a * myLen)
Dim myRemain As String = Strings.Left(myStr, Len(myStr) - (a - 1) * myLen)
Multi = Strings.Right("0000000000" & CLng(myRemain) * 2 + simpan, myLen) & Multi
If Len(CStr(CLng(myRemain) * 2 + simpan)) > myLen Then simpan = 1 Else simpan = 0
If a = myTimes And simpan = 1 Then Multi = "1" & Multi
Next
End Function
Function Adding(ByVal TheVal1 As String, ByVal TheVal2 As String) As String
Dim simpan As Byte = 0
Adding = ""
Dim myLen As Byte = 10
Dim myTimes = -Int(-Len(TheVal2) / myLen)
TheVal1 = Strings.Right("00000000000" & TheVal1, Len(TheVal2))
For a = 1 To myTimes
Dim myStr1 As String = Strings.Right(TheVal1, a * myLen)
Dim myStr2 As String = Strings.Right(TheVal2, a * myLen)
Dim myRemain1 As String = Strings.Left(myStr1, Len(myStr1) - (a - 1) * myLen)
Dim myRemain2 As String = Strings.Left(myStr2, Len(myStr2) - (a - 1) * myLen)
Adding = Strings.Right("0000000000" & CLng(myRemain1) + CLng(myRemain2) + simpan, myLen) & Adding
If Len(CStr(CLng(myRemain1) + CLng(myRemain2) + simpan)) > myLen Then simpan = 1 Else simpan = 0
If a = myTimes And simpan = 1 Then Adding = "1" & Adding
Next
End Function
and when I tried to use your binary,
"010000010110110001101001001000000100000101101000011011010110010101100100001000000100111101110011011011010110000101101110001000000110001001101001011100100111010001101000011001000110000101110100011001010011101000110001001011010011001100101101001100010011100100111000001101110010000001100011011011110111010101101110011101000111001001111001001110100111001101110101011001000110000101101110"
The Result is:
"000010069595034014783469636931351646363690636553221072125712116008033168565762469882090200055592025448896479090348155246"

Various HMAC_SHA256 functions in classic ASP gives different results

Somehow I need to generate a hash in Classic ASP which is equivalent to PHP's following function's output:
$hash = hash_hmac('SHA256', $message, pack('H*', $secret));
where $message = 'stackoverflow'; $secret = '1234567890ABCDEF';. I tried quite a lot approaches online, but none matches the PHP result:
bcb3452cd48c0f9048e64258ca24d0f3399563971d4a5dcdc531a7806b059e36
Method 1: Using dvim_brix_crypto-js-master_VB.asp online (using CrytoJS)
Function mac256(ent, key)
Dim encWA
Set encWA = ConvertUtf8StrToWordArray(ent)
Dim keyWA
Set keyWA = ConvertUtf8StrToWordArray(key)
Dim resWA
Set resWA = CryptoJS.HmacSHA256(encWA, key)
Set mac256 = resWA
End Function
Function ConvertUtf8StrToWordArray(data)
If (typename(data) = "String") Then
Set ConvertUtf8StrToWordArray = CryptoJS.enc.Utf8.parse(data)
Elseif (typename(data) = "JScriptTypeInfo") Then
On error resume next
'Set ConvertUtf8StrToWordArray = CryptoJS.enc.Utf8.parse(data.toString(CryptoJS.enc.Utf8))
Set ConvertUtf8StrToWordArray = CryptoJS.lib.WordArray.create().concat(data) 'Just assert that data is WordArray
If Err.number>0 Then
Set ConvertUtf8StrToWordArray = Nothing
End if
On error goto 0
Else
Set ConvertUtf8StrToWordArray = Nothing
End if
End Function
The script can be found here. This method gives:
c8375cf0c0db721ecc9c9b3a034284117d778ee8594285196c41d5020917f78c
Method 2: Pure Classic ASP Approach
Public Function HMAC_SHA256(prmKey, prmData)
Dim theKey : theKey = prmKey
Dim Block_Size, O_Pad, I_Pad
Block_Size = 64
O_Pad = 92 'HEX: 5c'
I_Pad = 54 'HEX: 36'
Dim iter, iter2
If Len(theKey) < Block_Size Then
For iter = 1 to Block_Size - Len(theKey)
theKey = theKey & chr(0)
Next
ElseIf Len(theKey) > Block_Size Then
theKey = SHA256(theKey)
End If
Dim o_key_pad : o_key_pad = ""
Dim i_key_pad : i_key_pad = ""
For iter = 1 to Block_Size
o_key_pad = o_key_pad & Chr(Asc(Mid(theKey,iter,1)) xor O_Pad)
i_key_pad = i_key_pad & Chr(Asc(Mid(theKey,iter,1)) xor I_Pad)
Next
HMAC_SHA256 = SHA256(o_key_pad & SHA256(i_key_pad & prmData))
End Function
result = HMAC_SHA256(secret, message)
This method gives:
bc0511316791176484c7d80bc8faaecd8388b75fb97516181ba6b361fd032531
Method 3: Using Amazon AWS's sha256.wsc (using CrytoJS)
Dim sha
Set sha = GetObject( "script:" & Server.MapPath("sha256.wsc") )
sha.hexcase = 0
result = sha.b64_hmac_sha256(secret, message)
The WSC can be found here. This method gives (same result as Method 1):
c8375cf0c0db721ecc9c9b3a034284117d778ee8594285196c41d5020917f78c
I think the problem is the pack() part, which changes the Hex string to binary. Therefore, I found a way to reproduce the pack() function in ASP:
Dim key2, hexarr, binstr
key2 = "12 34 56 78 90 AB CD EF"
hexarr = Split(key2)
ReDim binarr(UBound(hexarr))
For i = 0 To UBound(hexarr)
binarr(i) = Chr(CInt("&h" & hexarr(i)))
Next
binstr = Join(binarr, "")
where the key2 is the original secret with space added in every 2 characters. By replacing the secret with binstr, the methods now produce:
Method 1: 8ab9e595eab259acb10aa18df7fdf0ecc5ec593f97572d3a4e09f05fdd3aeb8f
Method 2: d23fcafb41d7b581fdae8c2a4a65bc3b19276a4bd367eda9e8e3de43b6a4d355
Method 3: 8ab9e595eab259acb10aa18df7fdf0ecc5ec593f97572d3a4e09f05fdd3aeb8f
None of the above results is identical to PHP's one. What did I miss now?
Check out the following example.
The only requirement with this approach is Microsoft .Net Framework 2.0 (preinstalled starting from Windows Server 2003 R2) to use Com Interops.
I tried to be descriptive in the comments but feel free to ask questions about it.
'Returns Byte(), UTF-8 bytes of unicode string
Function Utf8Bytes(text)
With Server.CreateObject("System.Text.UTF8Encoding")
Utf8Bytes = .GetBytes_4(text)
End With
End Function
'Returns String, sequential hexadecimal digits per byte
'data As Byte()
Function BinToHex(data)
With Server.CreateObject("MSXML2.DomDocument").CreateElement("b64")
.dataType = "bin.hex"
.nodeTypedValue = data
BinToHex = .text
End With
End Function
'Returns Byte(), a keyed hash generated using SHA256 method
'data As String, key As Byte()
Function HashHmacSha256(data, key)
With Server.CreateObject("System.Security.Cryptography.HMACSHA256")
.Key = key
HashHmacSha256 = .ComputeHash_2(UTF8Bytes(data))
End With
End Function
'Returns Byte(), of a packed hexadecimal string
'instead of PHP's pack('H*'
Function HexToBin(data)
With Server.CreateObject("MSXML2.DomDocument").CreateElement("b64")
.dataType = "bin.hex"
.text = data
HexToBin = .nodeTypedValue
End With
End Function
packed_secret = HexToBin("1234567890ABCDEF")
message = "stackoverflow"
binary_hash = HashHmacSha256(message, packed_secret)
string_hash = BinToHex(binary_hash)
Response.Write string_hash

PHP ansi to utf8 and vice versa

I just want to accomplish something where I want to convert strings from ansi to utf8 and vice versa.
example: to_ansi(1234)
expected result: NLKJ
example: to_utf8(NLKJ)
expected result: 1234
functions I currently have are:
function to_ansi($str)
{
$newString = "";
$reversedString = strrev($str);
for($i=0; $i < strlen($reversedString); $i++ ) {
$newString .= iconv(mb_detect_encoding(), 'UTF-8', chr(ord($reversedString[$i]) * 1.5));
}
return $newString;
}
function to_utf8($str)
{
$newString = "";
$reversedString = strrev($str);
for($i=0; $i < strlen($reversedString); $i++ ) {
$newString .= iconv(mb_detect_encoding(), 'UTF-8', chr(ord($reversedString[$i]) / 1.5));
}
return $newString;
}
usnig those functions above I get
example: to_ansi(1234)
result: NLKI
example: to_utf8(NLKJ)
result: 1224
actually I'm just interpreting vbs to PHP and the original functions are:
Function ToAnsi(ByVal strPassword As String) As String
Dim strLetter As String
Dim strRevPass As String
Dim strNewPass As String
strRevPass = strReverse(strPassword)
strNewPass = ""
For a = 1 To Len(strRevPass)
strLetter = Mid$(strRevPass, a, 1)
strNewPass = strNewPass & Chr((Asc(strLetter) * 1.5))
Next a
Text2.Text = strNewPass
End Function
Function ToUTF8(ByVal strPassword As String)
Dim strLetter As String
Dim strRevPass As String
Dim strNewPass As String
strRevPass = strReverse(strPassword)
strNewPass = ""
For a = 1 To Len(strRevPass)
strLetter = Mid$(strRevPass, a, 1)
strNewPass = strNewPass & Chr(Asc(strLetter) / 1.5)
Next a
txtText3.Text = strNewPass
End Function
Why do you think that 1234 in ANSI should NLKJ in UTF-8?
The reason of your problem might be a rounding error. You're multiplying and dividing by 1.5. For instance the letter 'y' (ASCII 122) divided by 1.5 is 80 2/3, which is treated as 81 (there are no fractions in character codes). Then back: 81 * 1.5 = 121.5 which is treated as 122, resulting in a 'z'.
So it's hard to grasp what the meaning of this code is. It certainly isn't a regular ANSI to UTF-8 conversion.
It seems to do some password hashing/encoding, but in a way that is very insecure. It's just a very simple encoding algorithm that can be very easily decoded as well, apart from the fact that it is inherently broken, and mangles your data beyond repair.

send PHP string to C++

I am trying to pass over from php a string into C++, i managed to figure out how to pass numbers, but it doesn't work for letters. Here's what i have that works for PHP
<?php
$r = 5;
$s = 12;
$x= 3;
$y= 4;
$q= "Hello World";
$c_output=`project1.exe $r $s $x $y $q`; // pass in the value to the c++ prog
echo "<pre>$c_output</pre>"; //received the sum
//modify the value in php and output
echo "output from C++ programm is" . ($c_output + 1);
?>
This sends the variables r,s,x,y, and q to the C++ programm project1.exe and IT WORKS, but the problem is that it doesn't work for the string variable $q.
Here's the code that I have in my C++ programm, it's simple:
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main(int in, char* argv[]) {
int val[2];
for(int i = 1; i < in; i++) { // retrieve the value from php
val[i-1] = atoi(argv[i]);
}
double r = val[0];
double s = val[1];
double x = val[2];
double y = val[3];
double q = val[4]; // here's the problem, as soon as i try to define val[4] as a string or char, it screws up
cout << r;
cout <<s;
cout << x;
cout << y;
cout << q;
// will output to php
return 0;
}
It works, but for the string "Hello world" which i pass through $q from PHP doesn't give me the string back (i know it's defined as a double, but as soon as i try to change it to a string or a char variable the code just doesn't compile).
Please explain to me how i have to go around this problem so that $q can be processed as a string. FYI, I am a newbie to programming (6 months in).
Try not converting the final argument using atoi(argv[i]). Just keep it as argv[i].
for(int i = 1; i < in-1; i++)
{
val[i-1] = atoi(argv[i]);
}
q = argv[i];
It doesn't work for letters because you are doing atoi(..)(which converts char-string to integer) in the C++ program.
Have some means of letting the program know what to expect -- whether a number or a string. May be the first argument can help the program differentiate, like may be the following:
$c_output = `project1.exe nnsnns 1 2 string1 3 4 string2`
Then you could do:
for(int i = 0/*NOTE*/,len=strlen(argv[1]); i < len; i++) { // retrieve the value from php
if (argv[1][i] == 'n'){
//argv[2+i] must be an integer
}else if (argv[1][i] == 's'){
//argv[2+i] is a string
}
}
Of course you should check if (strlen(argv[1]) == in-2).
BTW, in the C++ code above, val is a array holding 2 ints; and you are trying to access much beyond index 1.
To pass one single string to the C++ you would do something like the following:
$output = `project1.exe $q`; //Read below.
NOTE: $q must be a single word. No spaces, no extra characters like '|', '&', or any other character which the shell might interpret differently. $q must be clean before you pass that on to C++ Program. If $q is more than one word, use quotes.
C++ Part (Just try the following, then you can modify as you go along)
cout<<argv[1]<<endl;

Categories