Tengo el siguiente algoritmo en C#:
public static int Encriptar(string cad) {
int[,] mul = { {0,1,2,3,4,5,6,7,8,9},
{1,2,3,4,0,6,7,8,9,5},
{2,3,4,0,1,7,8,9,5,6},
{3,4,0,1,2,8,9,5,6,7},
{4,0,1,2,3,9,5,6,7,8},
{5,9,8,7,6,0,4,3,2,1},
{6,5,9,8,7,1,0,4,3,2},
{7,6,5,9,8,2,1,0,4,3},
{8,7,6,5,9,3,2,1,0,4},
{9,8,7,6,5,4,3,2,1,0}
};
int[,] per= {{0,1,2,3,4,5,6,7,8,9},
{1,5,7,6,2,8,3,0,9,4},
{5,8,0,3,7,9,6,1,4,2},
{8,9,1,6,0,4,3,5,2,7},
{9,4,5,3,1,2,6,8,7,0},
{4,2,8,6,5,7,3,9,0,1},
{2,7,9,3,8,0,6,4,1,5},
{7,0,4,6,9,1,3,2,5,8}};
int[] inv={0,4,3,2,1,5,6,7,8,9};
int check = 0;
int i = 0;
string invertido=invertir(cad);
for (i = 0; i < invertido.Length; i++ )
{
check=mul[check,per[((i+1)%8),int.Parse(invertido[i].ToString())]];
}
return inv[check];
}
Y lo estoy traduciendo a Visual Basic:
Function Encriptar(cad As String) As Integer
Dim mul = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{1, 2, 3, 4, 0, 6, 7, 8, 9, 5},
{2, 3, 4, 0, 1, 7, 8, 9, 5, 6},
{3, 4, 0, 1, 2, 8, 9, 5, 6, 7},
{4, 0, 1, 2, 3, 9, 5, 6, 7, 8},
{5, 9, 8, 7, 6, 0, 4, 3, 2, 1},
{6, 5, 9, 8, 7, 1, 0, 4, 3, 2},
{7, 6, 5, 9, 8, 2, 1, 0, 4, 3},
{8, 7, 6, 5, 9, 3, 2, 1, 0, 4},
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
}
Dim per = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{1, 5, 7, 6, 2, 8, 3, 0, 9, 4},
{5, 8, 0, 3, 7, 9, 6, 1, 4, 2},
{8, 9, 1, 6, 0, 4, 3, 5, 2, 7},
{9, 4, 5, 3, 1, 2, 6, 8, 7, 0},
{4, 2, 8, 6, 5, 7, 3, 9, 0, 1},
{2, 7, 9, 3, 8, 0, 6, 4, 1, 5},
{7, 0, 4, 6, 9, 1, 3, 2, 5, 8}}
Dim inv = {0, 4, 3, 2, 1, 5, 6, 7, 8, 9}
Dim check = 0
Dim invertido As String = StrReverse(cad)
For i As Integer = 0 To invertido.Length
check = mul(check, per( [[i]+1]%8 , invertido(i).ToString()))
Next
Return inv(check)
End Function
El problema es que apenas estoy aprendiendo VB y tengo un error en la linea
check = mul(check, per( [[i]+1]%8 , invertido(i).ToString()))
Ayuda a traducir esa linea por favor.
Cabe aclarar que el parametro de entrada es una cadena de solo numeros.
[[i]+1]
? También, el operador de módulo esMod
.check = mul(check, per((i + 1) Mod 8, invertido(i).ToString()))
, y me sale error en invertido(i).ToString() que se sale del rango de la matrizInteger
, puedes hacerInteger.Parse(invertido(i).ToString())
?