RE:Encriptar-Desencriptar
Bueno pues amigo aqui esta un buen ejemplo y + que ejemplo yo lo utilizo en mis aplicaciones y ha funcionado muy bien !!!
guardo la cadena de conexion en un archivo de texto encriptado y cuando la voy a utilizar hago la desencriptacion de una forma como veras simple y sencilla pero lo importante funcional.
Private Sub cmdGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGrabar.Click
Dim sPath As String
If ProbarConexion() Then
Try
sPath = ENCRIPTAR(Trim(txtServer.Text)) & "\" & _
ENCRIPTAR(Trim(TxtUsuario.Text)) & "\" & _
ENCRIPTAR(Trim(TxtPassword.Text)) & "\" & _
ENCRIPTAR(Trim(Txtbasedatos.Text)) & "\" & _
ENCRIPTAR(Trim(txtpuerto.Text))
FileOpen(1, AppPath() & "rtdbMySQL.fch", OpenMode.Output)
Print(1, sPath)
FileClose(1)
Mensaje("RUTA ACTUALIZADA CON EXITO")
StringCn = StringConexionMySQL()
Catch ex As Exception
MensajeError(Err.Description)
End Try
End If
End Sub
Function ProbarConexion() As Boolean
Dim cn As New MySql.Data.MySqlClient.MySqlConnection
Dim StringCn As String
StringCn = "server=" & Trim(txtServer.Text) & ";" & _
"user id=" & Trim(TxtUsuario.Text) & ";" & _
"password=" & Trim(TxtPassword.Text) & ";" & _
"database=" & Trim(Txtbasedatos.Text) & ";" & _
"port=" & Trim(txtpuerto.Text) & ""
Try
cn.ConnectionString = StringCn
cn.Open()
cn.Close()
Mensaje("Conexión Exitosa")
ProbarConexion = True
Catch ex As Exception
MensajeError(Err.Description)
ProbarConexion = False
End Try
End Function
Public Function StringConexionMySQL() As String
Dim sPath As String
Dim datos() As String
sPath = DBPathMySQL()
datos = Split(sPath, "\")
Try
StringConexionMySQL = "server=" & DESENCRIPTAR(Trim(datos(0))) & ";" & _
"user id=" & DESENCRIPTAR(Trim(datos(1))) & ";" & _
"password=" & DESENCRIPTAR(Trim(datos(2))) & ";" & _
"database=" & DESENCRIPTAR(Trim(datos(3))) & ";" & _
"port=" & DESENCRIPTAR(Trim(datos(4))) & ""
Catch ex As Exception
StringConexionMySQL = ""
End Try
End Function
'-------------- aqui estan las funciones que se necesitan ----------
Function ENCRIPTAR(ByVal string_encriptar As String) As String
Dim R As Integer
Dim I As Integer
R = Len(Trim(string_encriptar))
For I = 1 To R
Mid(string_encriptar, I, 1) = Chr(Asc(Mid(string_encriptar, I, 1)) - 1)
Next I
Return string_encriptar
End Function
Function DESENCRIPTAR(ByVal string_desencriptar As String) As String
Dim R As Integer
Dim i As Integer
R = Len(Trim(string_desencriptar))
For i = 1 To R
Mid(string_desencriptar, i, 1) = Chr(Asc(Mid(string_desencriptar, i, 1)) + 1)
Next i
Return string_desencriptar
End Function