Hi DW peps.

I've used the following codes to crypt and save files now I want to decrypt it when I'm reading it so that let say I want to check if the saved password matchs the entered password which the saved password is saved on pc drive e.g. Drive D:.

I wont put all of my code but will put the code I used to crypt and save the crypted text in a file.

' ontop you need to import these files.
Imports System
Imports System.Security.Cryptography
Imports System.Collections
Imports System.Text

Private sub BtnRegist_Click()
Dim name As String

        name = CStr(TextBox1.Text)
        Dim sourceBytes1() As Byte = Encoding.ASCII.GetBytes(name)

        ' saving the crypted text.

        Dim encodedBytes1() As Byte
        ' ne will be for name
        Dim ne As String

        encodedBytes1 = EncodeBytes(sourceBytes1)
        ne = (Encoding.ASCII.GetString(encodedBytes1))
        ' writing to a file.
        My.Computer.FileSystem.WriteAllText("D:\Test\Name.txt", ne, True)

End sub

Private Function EncodeBytes(ByVal sourceBytes() As Byte) As Byte()
        Dim currentPosition As Int16 = 0
        Dim targetBytes(1024) As Byte
        Dim sourceByteLength As Integer = sourceBytes.Length

        ' Create a DES encryptor from this instance to perform encryption.
        Dim cryptoTransform As CryptoAPITransform
        cryptoTransform = CType(desCSP.CreateEncryptor(), CryptoAPITransform)

        ' Retrieve the block size to read the bytes.
        Dim inputBlockSize As Integer = cryptoTransform.InputBlockSize

        ' Retrieve the key handle.
        Dim keyHandle As IntPtr = cryptoTransform.KeyHandle

        ' Retrieve the block size to write the bytes.
        Dim outputBlockSize As Integer = cryptoTransform.OutputBlockSize

            ' Determine if multiple blocks can be transformed.
            If (cryptoTransform.CanTransformMultipleBlocks) Then
                Dim numBytesRead As Int16 = 0
                While (sourceByteLength - currentPosition >= inputBlockSize)
                    ' Transform the bytes from currentposition in the 
                    ' sourceBytes array, writing the bytes to the targetBytes
                    ' array.
                    numBytesRead = cryptoTransform.TransformBlock( _
                        sourceBytes, _
                        currentPosition, _
                        inputBlockSize, _
                        targetBytes, _

                    ' Advance the current position in the sourceBytes array.
                    currentPosition += numBytesRead
                End While

                ' Transform the final block of bytes.
                Dim finalBytes() As Byte
                finalBytes = cryptoTransform.TransformFinalBlock( _
                    sourceBytes, _
                    currentPosition, _
                    sourceByteLength - currentPosition)

                ' Copy the contents of the finalBytes array to the targetBytes
                ' array.
                finalBytes.CopyTo(targetBytes, currentPosition)
            End If

        Catch ex As Exception
            MsgBox("Caught unexpected exception:" + _
                ex.ToString() + vbCrLf)

        End Try

        ' Determine if the current transform can be reused.
        If (Not cryptoTransform.CanReuseTransform) Then

            ' Free up any used resources.
        End If

        ' Trim the extra bytes in the array that were not used.
        Return TrimArray(targetBytes)
    End Function
    ' Resize the dimensions of the array to a size that contains only valid
    ' bytes.
    Private Function TrimArray(ByVal targetArray() As Byte) As Byte()

        Dim enum1 As IEnumerator = targetArray.GetEnumerator()
        Dim i As Int16 = 0

        While (enum1.MoveNext())
            If (enum1.Current.ToString().Equals("0")) Then
                Exit While
            End If

            i += 1
        End While

        ' Create a new array with the number of valid bytes.
        Dim returnedArray(i - 1) As Byte
        For j As Int16 = 0 To i - 1 Step 1
            returnedArray(j) = targetArray(j)

        Return returnedArray
    End Function

so now the main problem for me is to use another form e.g. this will be done on form1 and form2 will do decryption so that the files can be read and be matched and also be read by a human.

4 Years
Discussion Span
Last Post by azareth

i've tried doing this as well, but in the end i changed my approach into encrypting the file instead.. but what you're trying to do is different, anyway
hope this helps

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.