Load and Save bitmap with encryption

Example requests & Code samples for GdPicture Toolkits.
Post Reply
User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Load and Save bitmap with encryption

Post by Loïc » Thu Feb 25, 2010 3:41 pm

On a customer request we provide an easy vb.net sample to load and save bitmap with RC4 encryption.

Enjoy !

Code: Select all

    Private Function EncodeImage(ByVal PassWord As String, ByVal SrcImagePath As String, ByVal DstPath As String) As Boolean
        Dim bSuccess As Boolean = True
        Dim oGdPictureImaging As New GdPicture.GdPictureImaging
        oGdPictureImaging.SetLicenseNumber("XXX") 'Replace XXX by demo or commercial KEY
        Dim ImageID As Integer = oGdPictureImaging.CreateGdPictureImageFromFile(SrcImagePath)
        If ImageID <> 0 Then
            Dim ImageData(0) As Byte
            Dim BytesRead As Integer = 0
            Dim EncoderParam As Integer

            If oGdPictureImaging.GetPixelFormat(ImageID) = Imaging.PixelFormat.Format1bppIndexed Then
                EncoderParam = 4 'CCITT4 compression
            Else
                EncoderParam = 2 'LZW compression
            End If
            If oGdPictureImaging.SaveAsByteArray(ImageID, ImageData, BytesRead, DocumentFormat.DocumentFormatTIFF, EncoderParam) = GdPictureStatus.OK Then
                Dim EncryptedData() As Byte = RC4(PassWord, ImageData)
                If EncryptedData.Length > 0 Then
                    Dim oFile As New IO.FileInfo(DstPath)
                    Dim oFileStream As IO.FileStream = oFile.OpenWrite()

                    oFileStream.Write(EncryptedData, 0, EncryptedData.Length)
                    oFileStream.Close()
                Else
                    bSuccess = False
                End If
            Else
                bSuccess = False
            End If
            oGdPictureImaging.ReleaseGdPictureImage(ImageID)
        Else
            bSuccess = False
        End If

        Return bSuccess
    End Function


    Private Function DecodeImage(ByVal PassWord As String, ByVal SrcPath As String, ByVal DstImagePath As String) As Boolean
        Dim bSuccess As Boolean = True
        Dim EncryptedData(0) As Byte
       

        If IO.File.Exists(SrcPath) Then
            Dim oFile As New IO.FileInfo(SrcPath)
            Dim oFileStream As IO.FileStream = oFile.OpenRead()
            Dim lBytes As Integer = CInt(oFileStream.Length)

            If (lBytes > 0) Then
                ReDim EncryptedData(lBytes - 1)
                oFileStream.Read(EncryptedData, 0, lBytes)
            Else
                bSuccess = False
            End If
            oFileStream.Close()
        Else
            bSuccess = False
        End If

        If bSuccess Then
            Dim oGdPictureImaging As New GdPicture.GdPictureImaging
            oGdPictureImaging.SetLicenseNumber("XXX") 'Replace XXX by demo or commercial KEY
            Dim UncryptedData() As Byte = RC4(PassWord, EncryptedData)
            If UncryptedData.Length > 0 Then
                Dim ImageID As Integer = oGdPictureImaging.CreateGdPictureImageFromByteArray(UncryptedData)
                If ImageID <> 0 Then
                    Dim TiffCompression As GdPicture.TiffCompression
                    If oGdPictureImaging.GetPixelFormat(ImageID) = Imaging.PixelFormat.Format1bppIndexed Then
                        TiffCompression = GdPicture.TiffCompression.TiffCompressionCCITT4
                    Else
                        TiffCompression = GdPicture.TiffCompression.TiffCompressionLZW
                    End If
                    If oGdPictureImaging.SaveAsTIFF(ImageID, DstImagePath, TiffCompression) <> GdPictureStatus.OK Then
                        bSuccess = False
                    End If
                    oGdPictureImaging.ReleaseGdPictureImage(ImageID)
                Else
                    bSuccess = False
                End If
            Else
                bSuccess = False
            End If
        End If

        Return bSuccess
    End Function


    Private Function RC4(ByVal Password As String, ByRef Data() As Byte) As Byte()
        Dim B(0 To 255) As Integer
        Dim Y As Integer = 0, Z As Integer = 0
        Dim Key(0) As Byte
        Dim Buf As Byte
        Dim Crypt(0) As Byte
        Dim bContinue As Boolean = True

        If Len(Password) > 0 Then
            If Len(Password) > 256 Then Password = Mid(Password, 1, 256)
            Key = System.Text.Encoding.GetEncoding(1252).GetBytes(Password)
        Else
            bContinue = False
        End If

        If bContinue Then
            For i As Integer = 0 To 255
                B(i) = CByte(i)
            Next i


            For i As Integer = 0 To 255
                Y = (Y + B(i) + Key(i Mod Len(Password))) Mod 256
                Buf = CByte(B(i))
                B(i) = B(Y)
                B(Y) = Buf
            Next i

            Y = 0
            Z = 0
            If Data.Length > 0 Then
                Try
                    ReDim Crypt(0 To Data.Length - 1)
                Catch
                    bContinue = False
                End Try
                If bContinue Then
                    For i As Integer = 0 To Data.Length - 1
                        Y = (Y + 1) Mod 256
                        Z = (Z + B(Y)) Mod 256
                        Buf = CByte(B(Y))
                        B(Y) = B(Z)
                        B(Z) = Buf
                        Crypt(i) = CByte(Data(i) Xor (B((B(Y) + B(Z)) Mod 256)))
                    Next i
                End If
            End If
        End If

        Return Crypt
    End Function
Testing this code:

Code: Select all

MsgBox(EncodeImage("mypassword", "c:\test.tif", "c:\test.enc"))
MsgBox(DecodeImage("mypassword", "c:\test.enc", "c:\test2.tif"))

Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests