- 2025-07-22 1.0.6 Nuevas funciones de encriptartexto y desencriptartexto
This commit is contained in:
58
crypt.vb
58
crypt.vb
@@ -1,5 +1,6 @@
|
||||
Imports System.IO
|
||||
Imports System.Security.Cryptography
|
||||
Imports System.Text
|
||||
|
||||
Public Class crypt
|
||||
Public Shared Function FEncS$(ByVal X$, ByVal Jco0$, ByVal Jcd0$, ByVal Xs0 As Long)
|
||||
@@ -502,4 +503,61 @@ Public Class crypt
|
||||
Return res
|
||||
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
|
||||
End Class
|
||||
Reference in New Issue
Block a user