PHP to VB.NET convertion problem

Last post 07-28-2008 2:21 PM by ps2goat. 3 replies.

Sort Posts:

  • PHP to VB.NET convertion problem

    07-24-2008, 12:48 PM
    • Member
      point Member
    • nickshawn
    • Member since 07-24-2008, 4:41 PM
    • Posts 2

    Here is my php to vb.net

    PHP code:

    function encrypt($string, $key) {
                            $result = '';
                            for($i=0; $i<strlen($string); $i++) {
                                        $char = substr($string, $i, 1);
                                        $keychar = substr($key, ($i % strlen($key))-1, 1);
                                        $char = chr(ord($char)+ord($keychar));
                                        $result.=$char;
                            }
                            return base64_encode($result);
                } 

     

    VB.NET code:

    Function crypter(ByVal strPass As String, ByVal key As String)
            Dim result As String = Nothing
            Dim i As Integer
            For i = 0 To strPass.Length - 1
                Dim charPass As Char
                charPass = strPass.Substring(i, 1)
                Dim keychar As Char
                keychar = key.Substring((i Mod key.Length) - 1, 1)
                charPass = Chr(Asc(charPass) + Asc(keychar))
                result += charPass
            Next [i]
            Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(result)
            Return System.Convert.FromBase64String(result)

        End Function

    --------------------------------------------

     At line keychar = key.Substring((i Mod key.Length) - 1, 1) i get this error: StartIndex cannot be less than zero.

    my string is 13 characters and my key is about 34, can someone help with this solution? 

     

  • Re: PHP to VB.NET convertion problem

    07-24-2008, 3:17 PM
    • Star
      10,395 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,901

    If i = 0, then 0 MOD anything is still zero.  Zero - one = -1, thus less than zero.

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: PHP to VB.NET convertion problem

    07-24-2008, 3:41 PM
    • Member
      point Member
    • nickshawn
    • Member since 07-24-2008, 4:41 PM
    • Posts 2
    Yes, totally understand that it returns zero, but why does it work in PHP.  The php code works fine when given the zero, i just don't understand why mine is different.  Anyone who can explain this to me will be of much help.
  • Re: PHP to VB.NET convertion problem

    07-28-2008, 2:21 PM
    • Star
      10,395 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,901

    It isn't the 0 that screws up the code, it's the -1.

    As for the PHP question, I'm not totally sure because I'm not a PHP guy (I could be in a day or less if need be, but there is no need). There is another post that can possibly answer your question: http://bytes.com/forum/thread163251.html

    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
Page 1 of 1 (4 items)