CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jan 2006
    Posts
    14

    Arrow Decrypt Problem (Bad Data)

    Hi Guys,

    I need some help here.

    I have a routine/code which encrypts a file based on a key and decrypts the file using the same key. Now I have had this routine for quite sometime now and it has worked flawlessly so far. Today while trying to decrypt a file an error occured. The file is similar to the other files I encrypt. Somehow I am unable to decrypt a particular file no matter what.

    I am attaching two similar files, one which encrypts & decrypts fine and the other which encrypts ok but does not decrypt properly. I am stunned to be honest.

    CODE : ENCRYPT

    Code:
    'create an 8-byte long array to hold the key 
        Private TheKey(7) As Byte ' it was public earlier
        'Stuff some random values into the vector: 
        Private Vector() As Byte = {&H12, &H44, &H16, &HEE, &H88, &H15, &HDD, &H41}
    
        Friend Sub Encrypt(ByVal inName As String, ByVal outName As String, ByVal key As String)
            Try
                CreateKey(key)
                Dim storage(4096) As Byte   'create buffer 
                Dim totalBytesWritten As Long = 8  'Keeps track of bytes written. 
    
                Dim packageSize As Integer   'Specifies the number of bytes written at one time. 
    
                'Declare the file streams. 
                Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
                Dim fout As New FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write)
                fout.SetLength(0)
    
                Dim totalFileLength As Long = fin.Length  'Specifies the size of the source file. 
    
                'create the Crypto object 
                Dim des As New DESCryptoServiceProvider()
    
                Dim crStream As New CryptoStream(fout, des.CreateEncryptor(TheKey, Vector), CryptoStreamMode.Write)
    
                'flow the streams 
                While totalBytesWritten < totalFileLength
                    packageSize = fin.Read(storage, 0, 4096)
                    crStream.Write(storage, 0, packageSize)
                    totalBytesWritten = Convert.ToInt32(totalBytesWritten + packageSize / des.BlockSize * des.BlockSize)
                End While
    
                crStream.Close()
                fin.Close()
                fout.Close()
            Catch e As Exception
                MsgBox(e.Message, MsgBoxStyle.Critical)
            End Try
    
        End Sub
    CODE: DECRYPT

    Code:
    Friend Sub Decrypt(ByVal inName As String, ByVal outName As String, ByVal key As String, ByRef IsErr As Boolean)
            CreateKey(key)
            Dim storage(4096) As Byte
            Dim totalBytesWritten As Long = 8
            Dim packageSize As Integer
            Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
            Dim fout As New FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write)
            fout.SetLength(0)
            Dim totalFileLength As Long = fin.Length
            Dim des As New DESCryptoServiceProvider()
            Dim crStream As New CryptoStream(fout, des.CreateDecryptor(TheKey, Vector), CryptoStreamMode.Write)
    
            Try
                While totalBytesWritten < totalFileLength
                    packageSize = fin.Read(storage, 0, 4096)
                    crStream.Write(storage, 0, packageSize)
                    totalBytesWritten = Convert.ToInt32(totalBytesWritten + packageSize / des.BlockSize * des.BlockSize)
                  End While
    
                crStream.FlushFinalBlock()   ' just added
                crStream.Close()
                fin.Close()
                fout.Close()
            Catch e As Exception
                IsErr = True
                fin.Close()
                fout.Close()
            End Try
    
        End Sub
    COMMON FUNCTION CODE:

    Code:
    Private Sub CreateKey(ByVal strKey As String)
    
            ' Byte array to hold key 
            Dim arrByte(7) As Byte
    
            Dim AscEncod As New System.Text.ASCIIEncoding()
            Dim i As Integer = 0
            AscEncod.GetBytes(strKey, i, strKey.Length, arrByte, i)
    
            'Get the hash value of the password 
            Dim hashSha As New SHA1CryptoServiceProvider()
            Dim arrHash() As Byte = hashSha.ComputeHash(arrByte)
    
            'put the hash value into the key 
            For i = 0 To 7
                TheKey(i) = arrHash(i)
            Next i
    
        End Sub
    You can call the functions to encrypt & decrypt a file like this:

    Code:
     Call Encrypt("C:\fileOk.txt", "C:\FileOkEnc.dat", "key")
     Call Decrypt("C:\FileOKEnc.dat", "C:\fileOKdec.txt", "key", IsErr)
    Try to encrypt and decrypt two very similar files. One is okay and in second one an error occurs while decrypting. I am unable to figure out why. This code has works well so far.

    FileOK.txt & FileProblem.txt have been attached.

    Thanks a lot,

    Cheers,
    GR
    Attached Files Attached Files
    Last edited by greatchap; October 21st, 2010 at 05:13 AM. Reason: attachment

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured