Convert PHP to FreeBasic code | Help

New to FreeBASIC? Post your questions here.
Post Reply
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Convert PHP to FreeBasic code | Help

Post by gerry »

Help!

Code: Select all

<?php
	function CryptAlg($dString,$dKey,$dType,$dIntense,$dCalcNum)
	{
		if ($dIntense > 254)
			$dIntense = 254;
		$dNewString = $dString;
		for ($y = 0; $y <= $dIntense; $y++)
		{
			$strOutput = "";
			$x = 0;
			for ($i = 0; $i < strlen($dNewString); $i++)
			{
				if ($x >= strlen($dKey))
					$x = 0;
				if ($dType == 0)
					$strOutput .= Chr((Ord($dNewString[$i]) + $dCalcNum) - Ord($dKey[$x]));
				if ($dType == 1)
					$strOutput .= Chr((Ord($dNewString[$i]) - $dCalcNum) + Ord($dKey[$x]));
				$x++;
			}
			$dNewString = $strOutput;
		}
		return $dNewString;
	}
	$test = CryptAlg("Crypt Algorithm", "testkey1234567890", 0, 110, 1234567890);
	echo "Encrypted text: $test<br>";
	$test = CryptAlg($test, "testkey1234567890", 1, 110, 1234567890);
	echo "Decrypted text: $test<br>";
?>
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Convert PHP to FreeBasic code | Help

Post by D.J.Peters »

NOTE: choosing for dCalcNum a number > 255 makes no sence becouse in FreeBASIC a char from 'normal' string are in range of 0-255 (UBYTE) !

Maybe in PHP a text var is a colection of UNICODES or UTF8 ... !

Joshy

Code: Select all

enum codingType
  encode = 0
  decode = 1
end enum  
function CryptAlg(dString as string, dKey as string, dType as codingType, dIntense as ubyte, dCalcNum as integer) as string
	dim as string dNewString = dString
 	if (dIntense > 254) then dIntense = 254
	for y as integer = 0 to dIntense
		dim as string strOutput = ""
		dim as integer x = 0
		for i as integer = 0 to len(dNewString)-1
		  if x >= len(dKey) then x = 0
      dim as ubyte char = dNewString[i]
      if (dType = encode) then char += dCalcNum : char -= dKey[x]
      if (dType = decode) then char -= dCalcNum : char += dKey[x]
      strOutput &= Chr(char)
      x+=1
    next
    dNewString = strOutput
  next
  return dNewString
end function
dim as string test
test = CryptAlg("Crypt Algorithm", "testkey1234567890", encode, 110, 1234567890)
print "Encrypted text: " & test
test = CryptAlg(test, "testkey1234567890", decode, 110, 1234567890)
print "Decrypted text: " & test
sleep
Last edited by D.J.Peters on Aug 11, 2022 14:42, edited 2 times in total.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Convert PHP to FreeBasic code | Help

Post by gerry »

D.J.Peters wrote: Aug 11, 2022 14:19

Code: Select all

function CryptAlg(dString as string, dKey as string, dType as integer, dIntense as integer, dCalcNum as integer) as string
	dim as string dNewString = dString
 	if (dIntense > 254) then dIntense = 254
	for y as integer = 0 to dIntense
		dim as string strOutput = ""
		dim as integer x = 0
		for i as integer = 0 to len(dNewString)-1
		  if x >= len(dKey) then x = 0
      dim as ubyte char = dNewString[i]
      if (dType = 0) then char+=dCalcNum : char-=dKey[x]
      if (dType = 1) then char-=dCalcNum : char+=dKey[x]
      strOutput &= Chr(char)
      x+=1
    next
    dNewString = strOutput
  next
  return dNewString
end function
dim as string test = CryptAlg("Crypt Algorithm", "testkey1234567890", 0, 110, 1234567890)
print "Encrypted text: " & test
test = CryptAlg(test, "testkey1234567890", 1, 110, 1234567890)
print "Decrypted text: " & test
sleep

Code: Select all

enum encodingType
  encode = 0
  decode = 1
end enum  
function CryptAlg(dString as string, dKey as string, dType as encodingType, dIntense as ubyte, dCalcNum as integer) as string
	dim as string dNewString = dString
 	if (dIntense > 254) then dIntense = 254
	for y as integer = 0 to dIntense
		dim as string strOutput = ""
		dim as integer x = 0
		for i as integer = 0 to len(dNewString)-1
		  if x >= len(dKey) then x = 0
      dim as ubyte char = dNewString[i]
      if (dType = encode) then char+=dCalcNum : char-=dKey[x]
      if (dType = decode) then char-=dCalcNum : char+=dKey[x]
      strOutput &= Chr(char)
      x+=1
    next
    dNewString = strOutput
  next
  return dNewString
end function
dim as string test
test = CryptAlg("Crypt Algorithm", "testkey1234567890", encode, 110, 1234567890)
print "Encrypted text: " & test
test = CryptAlg(test, "testkey1234567890", decode, 110, 1234567890)
print "Decrypted text: " & test
sleep
Thanks! :wink:
Post Reply