none
criptografia RRS feed

  • Pergunta

  • Bom Dia!
    Pessoal, eu sou novo em VB.NET e gostaria de saber se alguem tem um exemplo de criptografia de descriptografia de texto para me passar, pois eu preciso criptografar a senha do usuário ao gravar no banco sql e descriptografar caso eu precise consultar a senha do mesmo.
    Obrigado!
    VB.NET
    quinta-feira, 5 de novembro de 2009 13:11

Respostas

  • Napsters,

    Segue exemplo nos links abaixo:

    SAMPLE: Symmetric key encryption and decryption using Rijndael algorithm.
    http://www.obviex.com/samples/Code.aspx?Source=EncryptionCS&Title=Symmetric%20Key%20Encryption&Lang=C%23

    Encrypt and Decrypt Passwords using CryptoStream
    http://www.willasrari.com/blog/encrypt-and-decrypt-passwords-using-cryptostream/00049.aspx

    Att.

    Ari C. Raimundo
    quinta-feira, 5 de novembro de 2009 15:52
  • Olá

    Segue um exemplo de código de criptografia

    Imports System
    Imports System.IO
    Imports System.Security
    Imports System.Security.Cryptography
    Imports System.Runtime.InteropServices
    Imports System.Text
    
    
    Module Module1
    
        ' Call this function to remove the key from memory after it is used for security.
        <DllImport("kernel32.dll")> _
        Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
        End Sub
    
        ' Function to generate a 64-bit key.
        Function GenerateKey() As String
            ' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
            Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()
    
            ' Use the automatically generated key for encryption. 
            Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)
    
        End Function
    
        Sub EncryptFile(ByVal sInputFilename As String, _
                       ByVal sOutputFilename As String, _
                       ByVal sKey As String)
    
            Dim fsInput As New FileStream(sInputFilename, _
                                        FileMode.Open, FileAccess.Read)
            Dim fsEncrypted As New FileStream(sOutputFilename, _
                                        FileMode.Create, FileAccess.Write)
    
            Dim DES As New DESCryptoServiceProvider()
    
            'Set secret key for DES algorithm.
            'A 64-bit key and an IV are required for this provider.
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    
            'Set the initialization vector.
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    
            'Create the DES encryptor from this instance.
            Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
            'Create the crypto stream that transforms the file stream by using DES encryption.
            Dim cryptostream As New CryptoStream(fsEncrypted, _
                                                desencrypt, _
                                                CryptoStreamMode.Write)
    
            'Read the file text to the byte array.
            Dim bytearrayinput(fsInput.Length - 1) As Byte
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
            'Write out the DES encrypted file.
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
            cryptostream.Close()
        End Sub
    
        Sub DecryptFile(ByVal sInputFilename As String, _
            ByVal sOutputFilename As String, _
            ByVal sKey As String)
    
            Dim DES As New DESCryptoServiceProvider()
            'A 64-bit key and an IV are required for this provider.
            'Set the secret key for the DES algorithm.
            DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
            'Set the initialization vector.
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    
            'Create the file stream to read the encrypted file back.
            Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
            'Create the DES decryptor from the DES instance.
            Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
            'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
            Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
            'Print out the contents of the decrypted file.
            Dim fsDecrypted As New StreamWriter(sOutputFilename)
            fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
            fsDecrypted.Flush()
            fsDecrypted.Close()
        End Sub
    
        Public Sub Main()
            'Must be 64 bits, 8 bytes.
            Dim sSecretKey As String
    
            ' Get the key for the file to encrypt.
            ' You can distribute this key to the user who will decrypt the file.
            sSecretKey = GenerateKey()
    
            ' For additional security, pin the key.
            Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)
    
    
            ' Encrypt the file.        
            EncryptFile("MyData.txt", _
            "Encrypted.txt", _
            sSecretKey)
    
            ' Decrypt the file.
            DecryptFile("Encrypted.txt", _
                        "Decrypted.txt", _
                        sSecretKey)
    
            ' Remove the key from memory. 
            ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
            gch.Free()
        End Sub
    
    End Module
    

    Se esta resposta solucionou sua dúvida, “Marque como Resposta”
    quinta-feira, 5 de novembro de 2009 13:29

Todas as Respostas

  • Olá

    Segue um exemplo de código de criptografia

    Imports System
    Imports System.IO
    Imports System.Security
    Imports System.Security.Cryptography
    Imports System.Runtime.InteropServices
    Imports System.Text
    
    
    Module Module1
    
        ' Call this function to remove the key from memory after it is used for security.
        <DllImport("kernel32.dll")> _
        Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
        End Sub
    
        ' Function to generate a 64-bit key.
        Function GenerateKey() As String
            ' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
            Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()
    
            ' Use the automatically generated key for encryption. 
            Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)
    
        End Function
    
        Sub EncryptFile(ByVal sInputFilename As String, _
                       ByVal sOutputFilename As String, _
                       ByVal sKey As String)
    
            Dim fsInput As New FileStream(sInputFilename, _
                                        FileMode.Open, FileAccess.Read)
            Dim fsEncrypted As New FileStream(sOutputFilename, _
                                        FileMode.Create, FileAccess.Write)
    
            Dim DES As New DESCryptoServiceProvider()
    
            'Set secret key for DES algorithm.
            'A 64-bit key and an IV are required for this provider.
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    
            'Set the initialization vector.
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    
            'Create the DES encryptor from this instance.
            Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
            'Create the crypto stream that transforms the file stream by using DES encryption.
            Dim cryptostream As New CryptoStream(fsEncrypted, _
                                                desencrypt, _
                                                CryptoStreamMode.Write)
    
            'Read the file text to the byte array.
            Dim bytearrayinput(fsInput.Length - 1) As Byte
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
            'Write out the DES encrypted file.
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
            cryptostream.Close()
        End Sub
    
        Sub DecryptFile(ByVal sInputFilename As String, _
            ByVal sOutputFilename As String, _
            ByVal sKey As String)
    
            Dim DES As New DESCryptoServiceProvider()
            'A 64-bit key and an IV are required for this provider.
            'Set the secret key for the DES algorithm.
            DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
            'Set the initialization vector.
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    
            'Create the file stream to read the encrypted file back.
            Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
            'Create the DES decryptor from the DES instance.
            Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
            'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
            Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
            'Print out the contents of the decrypted file.
            Dim fsDecrypted As New StreamWriter(sOutputFilename)
            fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
            fsDecrypted.Flush()
            fsDecrypted.Close()
        End Sub
    
        Public Sub Main()
            'Must be 64 bits, 8 bytes.
            Dim sSecretKey As String
    
            ' Get the key for the file to encrypt.
            ' You can distribute this key to the user who will decrypt the file.
            sSecretKey = GenerateKey()
    
            ' For additional security, pin the key.
            Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)
    
    
            ' Encrypt the file.        
            EncryptFile("MyData.txt", _
            "Encrypted.txt", _
            sSecretKey)
    
            ' Decrypt the file.
            DecryptFile("Encrypted.txt", _
                        "Decrypted.txt", _
                        sSecretKey)
    
            ' Remove the key from memory. 
            ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
            gch.Free()
        End Sub
    
    End Module
    

    Se esta resposta solucionou sua dúvida, “Marque como Resposta”
    quinta-feira, 5 de novembro de 2009 13:29
  • Napsters,

    Segue exemplo nos links abaixo:

    SAMPLE: Symmetric key encryption and decryption using Rijndael algorithm.
    http://www.obviex.com/samples/Code.aspx?Source=EncryptionCS&Title=Symmetric%20Key%20Encryption&Lang=C%23

    Encrypt and Decrypt Passwords using CryptoStream
    http://www.willasrari.com/blog/encrypt-and-decrypt-passwords-using-cryptostream/00049.aspx

    Att.

    Ari C. Raimundo
    quinta-feira, 5 de novembro de 2009 15:52