Reputation: 123
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
Upvotes: 2
Views: 783
Reputation: 55417
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));
Upvotes: 1