简体   繁体   中英

Convert Vb.net To Php

Good morning Engineers

This VB.Net code is responsible for encrypting a password

 Public Function Encrypt(ByVal clave As String) As String
        ' Defino variables
        Dim indice As Integer = 1
        Dim largo As Integer = 0
        Dim final As String = ""
        largo = Len(Trim(clave))
        Dim caracteres(largo) As String
        For indice = 1 To largo
            caracteres(indice) = Mid(clave, indice, 1)
            caracteres(indice) = Chr(Asc(caracteres(indice)) + indice)
            final = final & caracteres(indice)
        Next indice
     
        Return final
    End Function

and this would be the code in php that I did but I'm failing somewhere since I don't get the desired result

 public static function encryption($clave){
            $indice = 0;
            $largo = 0;
            $final = '';

            $largo = strlen(trim($clave));
            $caracteres = array();
            for ($indice; $indice < $largo; $indice++) { 
                $caracteres[$indice] = substr($clave, $indice, 1);
                $caracteres[$indice] = chr(ord($caracteres[$indice]) + $indice);
                $final = $final.$caracteres[$indice];
            }
            return $final;
        }

an example would be that in VB.Net the 12345 key is encrypted in 2468: and in PHP the same key is encrypted in 13579

I will be grateful to everyone who can help me find out where I am failing and how I can fix it

Thanks

You are very, very close! You just need to re-add back in that $indice is one-based in VB.Net but zero-based in PHP when you are performing math.

So change this:

$caracteres[$indice] = chr(ord($caracteres[$indice]) + $indice);

To this:

$caracteres[$indice] = chr(ord($caracteres[$indice]) + ($indice + 1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM