Jump to content

Encriptação


GpL

Recommended Posts

Pessoal, fiz este codigo juntamente com um colega, para encriptar ficheiros.. com um .txt ele resulta perfeitamente.. lê o ficheiro indicado, cria um 2º encriptado e cria um 3º ficheiro desencriptado, igual ao 1º ..

Mas o problema é que só funciona mesmo com .txt .. se puser .jpg ou .mp3 ou outro tipo qualquer de ficheiro ele não encripta nem desencripta depois..

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim fleitura As FileStream
    Dim bleitura As BinaryReader

    Dim fnum As Integer = FreeFile()

    Dim b, c As Byte
    Dim nomefich, x, y As String


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.TextBox3 = New System.Windows.Forms.TextBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(40, 264)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(64, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "abrir"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(8, 24)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(160, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(8, 56)
        Me.TextBox2.Multiline = True
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.ReadOnly = True
        Me.TextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TextBox2.Size = New System.Drawing.Size(160, 200)
        Me.TextBox2.TabIndex = 2
        Me.TextBox2.Text = ""
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(192, 56)
        Me.TextBox3.Multiline = True
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.ReadOnly = True
        Me.TextBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TextBox3.Size = New System.Drawing.Size(160, 200)
        Me.TextBox3.TabIndex = 2
        Me.TextBox3.Text = ""
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(232, 264)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(80, 24)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Button2"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(360, 302)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox3)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        nomefich = TextBox1.Text
        fleitura = New FileStream(nomefich, FileMode.Open)

        FileOpen(fnum, "2" & nomefich, OpenMode.Binary)

        bleitura = New BinaryReader(fleitura)
        x = ""
        y = ""
        Do While True
            Try
                b = bleitura.Read
            Catch
                Exit Do
            End Try

            x = x & Format(b, "000") & " "
            y = y & Format((b Xor 18), "000") & " "
            c = (b Xor 18)
            FilePut(fnum, c)
        Loop
        bleitura.Close()
        FileClose(fnum)
        TextBox2.Text = x
        TextBox3.Text = y
    End Sub



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        fleitura = New FileStream("2" & nomefich, FileMode.Open)
        FileOpen(fnum, "3" & nomefich, OpenMode.Binary)
        bleitura = New BinaryReader(fleitura)

        Do While True
            Try
                b = bleitura.Read
            Catch
                Exit Do
            End Try
            c = (b Xor 18)
            FilePut(fnum, c)
        Loop
        bleitura.Close()
        FileClose(fnum)
    End Sub

End Class

este é o codigo .. agradecia imenso algum tipo de ajuda visto o prazo para entrega deste projecto estar a ficar apertado já.. cumps!

Link to comment
Share on other sites

Ja esprimentaste a fazer a espreencia com uma imagem pequena tipo um icon ou algu do genero, ou entao um txt grande. Digo istu porque podes estar a esceder a capacidade de alguma variavel quando abres uma imagem ou outro ficheiro, sinseramente n estou a ver qual a rasão desse problema 😄

Só mais uma coisa, quando dizes que ele não encripta nem desencripta os outros ficheiros queres dizer que ele da ero a meio?

Link to comment
Share on other sites

  😛 Gostava de te poder ajudar mas não sei mm o que pode ser, eu estou mais abituado ao VB6.... Podia ser do modo que estas a utilizar para abrir os ficheiros, mas se não me engano estas a abrilo em modo Binario que axu que é o mais correcto...

Não desistas 🙂 e se consseguires resolver o prob diz qualquer coisa  😁

cumps

Link to comment
Share on other sites

Imports System.io
Public Class Form1
   Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

   Public Sub New()
       MyBase.New()

       'This call is required by the Windows Form Designer.
       InitializeComponent()

       'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.IContainer

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer. 
   'Do not modify it using the code editor.
   Friend WithEvents abrir As System.Windows.Forms.OpenFileDialog
   Friend WithEvents Button1 As System.Windows.Forms.Button
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   Friend WithEvents Button2 As System.Windows.Forms.Button
   Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.abrir = New System.Windows.Forms.OpenFileDialog
       Me.Button1 = New System.Windows.Forms.Button
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.Button2 = New System.Windows.Forms.Button
       Me.TextBox2 = New System.Windows.Forms.TextBox
       Me.SuspendLayout()
       '
       'Button1
       '
       Me.Button1.Location = New System.Drawing.Point(200, 32)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(96, 24)
       Me.Button1.TabIndex = 0
       Me.Button1.Text = "Abrir"
       '
       'TextBox1
       '
       Me.TextBox1.Location = New System.Drawing.Point(24, 32)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(160, 20)
       Me.TextBox1.TabIndex = 1
       Me.TextBox1.Text = ""
       '
       'Button2
       '
       Me.Button2.Location = New System.Drawing.Point(16, 264)
       Me.Button2.Name = "Button2"
       Me.Button2.Size = New System.Drawing.Size(288, 24)
       Me.Button2.TabIndex = 2
       Me.Button2.Text = "Copiar fixeiro"
       '
       'TextBox2
       '
       Me.TextBox2.Location = New System.Drawing.Point(24, 64)
       Me.TextBox2.Multiline = True
       Me.TextBox2.Name = "TextBox2"
       Me.TextBox2.ReadOnly = True
       Me.TextBox2.Size = New System.Drawing.Size(272, 192)
       Me.TextBox2.TabIndex = 3
       Me.TextBox2.Text = ""
       '
       'Form1
       '
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(320, 302)
       Me.Controls.Add(Me.TextBox2)
       Me.Controls.Add(Me.Button2)
       Me.Controls.Add(Me.TextBox1)
       Me.Controls.Add(Me.Button1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)

   End Sub

#End Region

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       With abrir
           If .ShowDialog() <> DialogResult.Cancel Then
               Me.TextBox1.Text = .FileName
           End If
       End With
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim fescrita, fleitura As filestream

       Dim bleitura As BinaryReader
       Dim bescrita As BinaryWriter
       Dim b As Byte
       Dim bts As String

       fleitura = New FileStream(Me.TextBox1.Text, FileMode.Open)
       fescrita = New FileStream("fcopiado.jpg", FileMode.CreateNew)

       bleitura = New BinaryReader(fleitura)
       bescrita = New BinaryWriter(fescrita)

       bleitura.BaseStream.Seek(0, SeekOrigin.Begin)
       bts = ""
       Do While True
           Try
               b = bleitura.ReadByte
               bts = bts & b
           Catch
               Exit Do
           End Try
           bescrita.Write(b)
       Loop
       TextBox2.Text = bts
       fleitura.Close()
       bleitura.Close()
       bescrita.Close()
   End Sub
End Class

Ja funca 🙂

Link to comment
Share on other sites

  • 4 weeks later...

Ola gpl, o teu problema é usares um simples XOR para encryptar ficheiros. Existe um namepace System.Security.Cryptography so para encryptacao, procura o seguinte site http://msdn2.microsoft.com/en-us/default.aspx por TripleDESCryptoServiceProvider class. Podias ter tambem usado o Code Snippets Manager do VS 2005.

Jorge

"The Dark Side Clouds Everthing. Impossible To See The Future Is."My rig: Intel Core 2 Quad Q9450 | abit IP35P | 4G Kingston 800 Mhz | XFX GeForce 9800 GX2 1G ddr3 | 2X WD5000AAJS 500Gb Sata 2 | PSU 600W || Caixa El-Diablo | Creative XMod

Link to comment
Share on other sites

  • 7 months later...

como diz o colega :  Asgorath 

"um simples XOR para encryptar ficheiros"  [ ás vezes a instrução Xor dá problemas.. ( esta é a minha opinião ) ]

Caso tenhas alguma dúvida me contacta: h2so4-programadorvb6@hotmail.com

cump.

até+

programadorvb6

______________________________________________________________________________

Que minha coragem seja maior que meu medo e que minha força seja tão grande quanto minha fé.
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.