Loading...

Encrypting portion of an image

Example requests & Code samples for GdPicture.NET Toolkits.

Encrypting portion of an image

Postby rangasamy » Mon Aug 02, 2010 10:14 am

Hi All,

How to hide and encrypt confidential parts of image Using GDPicture.net?.

for example some Key field values (Social Security Number, license Number) from the image and add redaction marks

User can able to,
setup Auto-Redaction zones - that do not require user intervention to redact,
Manual Redaction Zones that allow users to manually check common zones,
Enable/Disable Redaction Zones
rangasamy
 
Posts: 8
Joined: Wed Jul 14, 2010 11:14 am

Re: Image Redaction

Postby mp285 » Mon Aug 02, 2010 1:18 pm

Hi rangasamy,

I've been doing something very simillar - I've given a brief overview of how I've done it below. I'm assuming you're using a standard scanned form, so the fields being redacted will be in a constant position. I'm not sure it would be possible if the details could be anywhere on the image (OCR maybe?)

The simplest way seems to be using the GdViewer to draw a region on a loaded image, then storing the settings for future use (database, xml, text file, etc...).

Drawing Regions:
Code: Select all
// set the viewer to allow a region to be selected by clicking and dragging
gdViewer1.MouseMode = ViewerMouseMode.MouseModeAreaSelection;
...

// Wait for the mouse up event - indicates that a region has been drawn
private void gdViewer1_MouseUp(object sender, MouseEventArgs e) {
     // check a rectangle has been drawn
    if (gdViewer1.IsRect()) {
        int t_left = 0;
        int t_top = 0;
        int t_width = 0;
        int t_height = 0;
                   
        // get the co-ordinates / size of the drawn area
        gdViewer1.GetRectCoordinatesOnDocument(ref t_left, ref t_top, ref t_width, ref t_height);
    }
}


With the area set, we can then draw the rectangle onto the viewer. This displays a rectangle, without actually altering the underlying image.
Code: Select all
gdViewer1.AddRegion("Redaction", t_left, t_top, t_width, t_height, ForegroundMixMode.ForegroundMixModeBLACK, Color.Black);
gdViewer1.Redraw();


To Clear the rectangle:
Code: Select all
gdViewer1.ClearRect();
gdViewer1.RemoveAllRegions();


So far none of this has altered the underlying image. If the user is happy with the redactions, they can be drawn onto the image:
Code: Select all
// draw rectangle onto image
ogdPictureImaging.DrawFilledRectangle(image1, t_left, t_top, t_width, t_height, Color.Black)


This is now part of the image, and is basically unremovable.

Obviously you can extend this to allow as many redactions as you wish. I work on a sample image to get the settings, then save them for future use. These can then be added automatically (using drawFilledRectangle(...), or displayed for user confirmation / editing (gdViewer.AddRegion(...)).

You'll probably need at least some sort of manual validation for automatic validation. Theres nothing worse than displaying the functionality to a client and discovering someones ignored the appropriate box and written their credit-card number across the top of the form (true story!). I tend to display the redactions on screen, then allow the user to either accept them, or edit/remove them if needed.

Also, as this is all location based off a 'perfect' sample image, it will throw off the redaction if the images vary in size. I use a very basic modifier that checks the size of the image against the sample, and resizes/relocates the redaction areas accordingly. This works for small variations, but will fail if the image is a very different size.

I hope this helps, but I'd be very interested to know if anyone can come up with better ways of doing this!
mp285
 
Posts: 5
Joined: Fri Jul 03, 2009 4:26 pm

Re: Encrypting portion of an image

Postby Loïc » Mon Aug 02, 2010 1:48 pm

Hi there,

Here a way to encrypt/decrypt portion of an image using the RC4 algorithm. More information about this algorithm can be found here: http://en.wikipedia.org/wiki/RC4

Let me know if you have any comment !

Code: Select all
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      'Create a GdPciture Image from screencapture of the windows desktop
      Dim ImageID As Integer = oGdPictureImaging.CreateGdPictureImageFromHwnd(oGdPictureImaging.GetDesktopHwnd)

      'Convert to image to 24 BPP RGB
      oGdPictureImaging.ConvertTo24BppRGB(ImageID)

      ' Get ROI 0,0,300,300 of pixels to an array of byte
      Dim arPixels() As Byte = Nothing
      oGdPictureImaging.GetPixelArrayByte(ImageID, arPixels, 0, 0, 300, 300)

      'Now we encrypt the array using RC4 algorythm and the "I Love GdPicture" Password
      arPixels = RC4("I Love GdPicture", arPixels)

      'Now we replace the extracted area by encrypted pixels !
      oGdPictureImaging.SetPixelArrayByte(ImageID, arPixels, 0, 0, 300, 300)

      'Now saving the image with encrpyted portion. Warning: do not use lossly encryption scheme such as JPEG !!
      oGdPictureImaging.SaveAsTIFF(ImageID, "encrypted.tif", TiffCompression.TiffCompressionAUTO)
      oGdPictureImaging.ReleaseGdPictureImage(ImageID)


      'Now we will try to open and decrypt image !
      ImageID = oGdPictureImaging.CreateGdPictureImageFromFile("encrypted.tif")

      'Getting encoded area to byte array
      oGdPictureImaging.GetPixelArrayByte(ImageID, arPixels, 0, 0, 300, 300)

      'Unencrypting
      arPixels = RC4("I Love GdPicture", arPixels)

      'Restoring pixel data
      oGdPictureImaging.SetPixelArrayByte(ImageID, arPixels, 0, 0, 300, 300)



      gdviewer1.DisplayFromGdPictureImage(ImageID)
   End Sub


   Public 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
Attachments
encryption.png
Before with encryption - After with decryption
Loïc Carrère, support team.
www.orpalis.com
User avatar
Loïc
Site Admin
 
Posts: 4430
Joined: Tue Oct 17, 2006 10:48 pm
Location: France

Re: Encrypting portion of an image

Postby rangasamy » Tue Aug 03, 2010 9:11 am

Hi,

Thank you for your response.

I have added rectangular box with your guidance.

I need to be able to edit the rectangular box after the permanent save and I need to be able to enable/disable option for selected rectangular box


How can i do this?
rangasamy
 
Posts: 8
Joined: Wed Jul 14, 2010 11:14 am


Return to Example requests & Code samples For GdPicture.NET

Who is online

Users browsing this forum: No registered users and 0 guests