19/08/2026 MANMOG Version 3.0.12 Se añade rutina EncriptarTexto y DesencriptarTexto

This commit is contained in:
2026-08-19 17:45:59 +02:00
parent 9b3b60c528
commit bc9ee18259
3 changed files with 60 additions and 8 deletions

View File

@@ -582,4 +582,55 @@ Public Class crypt
Next
Return sb.ToString()
End Function
Public Shared Function EncriptarTexto(Texto As String, password As String) As String
Dim key As Byte() = GetKey(password)
Dim aes As Aes = Aes.Create()
aes.Key = key
aes.Mode = CipherMode.CBC
aes.Padding = PaddingMode.PKCS7
aes.GenerateIV()
Dim iv As Byte() = aes.IV
Dim encryptor = aes.CreateEncryptor()
Dim plainBytes = Encoding.UTF8.GetBytes(Texto)
Dim ms As New MemoryStream()
ms.Write(iv, 0, iv.Length) ' Guardamos el IV al principio
Using cs As New CryptoStream(ms, encryptor, CryptoStreamMode.Write)
cs.Write(plainBytes, 0, plainBytes.Length)
cs.FlushFinalBlock()
End Using
Return Convert.ToBase64String(ms.ToArray())
End Function
Public Shared Function DesencriptarTexto(Texto As String, password As String) As String
Dim fullCipher = Convert.FromBase64String(Texto)
Dim key As Byte() = GetKey(password)
Dim aes As Aes = Aes.Create()
aes.Key = key
aes.Mode = CipherMode.CBC
aes.Padding = PaddingMode.PKCS7
Dim iv(15) As Byte
Array.Copy(fullCipher, iv, iv.Length)
aes.IV = iv
Dim decryptor = aes.CreateDecryptor()
Dim ms As New MemoryStream()
Using cs As New CryptoStream(ms, decryptor, CryptoStreamMode.Write)
cs.Write(fullCipher, iv.Length, fullCipher.Length - iv.Length)
cs.FlushFinalBlock()
End Using
Return Encoding.UTF8.GetString(ms.ToArray())
End Function
Private Shared Function GetKey(password As String) As Byte()
Using sha256 As SHA256 = SHA256.Create()
Return sha256.ComputeHash(Encoding.UTF8.GetBytes(password))
End Using
End Function
End Class