Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Function EncryptData(ByVal fake As Image, ByVal Data As Byte()) As Image
- ' Make a new data array that appends the length of the array in front
- Dim StoredData(Data.Length + 4 - 1) As Byte
- Dim lengthBytes() As Byte = BitConverter.GetBytes(CType(Data.Length, Int32))
- System.Array.ConstrainedCopy(lengthBytes, 0, StoredData, 0, 4)
- System.Array.ConstrainedCopy(Data, 0, StoredData, 4, Data.Length)
- ' Now generate an image using the fake that can fit all this data in the pixels
- Dim ratio As Double = CDbl(fake.Width) / CDbl(fake.Height)
- Dim pixelCount As Integer = StoredData.Length
- Dim imgWidth As Integer = Math.Ceiling(Math.Sqrt(CDbl(pixelCount) * ratio))
- Dim imgHeight As Integer = Math.Ceiling(Math.Sqrt(CDbl(pixelCount) / ratio))
- Dim result As New Bitmap(imgWidth, imgHeight)
- Using g As Graphics = Graphics.FromImage(result)
- g.DrawImage(fake, 0, 0, imgWidth, imgHeight)
- End Using
- ' Use a lockbitmap to store the data into the image
- Dim locked As New Commons.Imaging.LockBitmap(result)
- locked.LockBits()
- If locked.Pixels.Count <> (imgWidth * imgHeight * 4) Then
- Throw New ArgumentException("Dest Pixel format is invalid")
- End If
- Dim i As Integer = 0
- For Each b As Byte In StoredData
- locked.Pixels(i + 0) = (locked.Pixels(i + 0) And Not &H7) Or (b And &H7)
- locked.Pixels(i + 1) = (locked.Pixels(i + 1) And Not &H3) Or ((b >> 3) And &H3)
- locked.Pixels(i + 2) = (locked.Pixels(i + 2) And Not &H7) Or ((b >> 5) And &H7)
- i += 4
- Next
- locked.UnlockBits()
- Return result
- End Function
- Public Function DecryptData(ByVal image As Image) As Byte()
- ' Convert to ARGB bitmap
- Dim bitmap As Bitmap
- If TypeOf image Is Bitmap Then
- bitmap = CType(image.Clone(), Bitmap)
- Else
- bitmap = New Bitmap(image)
- End If
- ' Read all the pixels using lockbitmap
- Dim pixelCount As Integer = image.Width * image.Height
- Dim storedData(pixelCount - 1) As Byte
- Dim i As Integer = 0
- Dim locked As New Commons.Imaging.LockBitmap(bitmap)
- locked.LockBits()
- For byte_index As Integer = 0 To pixelCount - 1
- Dim b As Byte = 0
- b = b Or (locked.Pixels(i + 0) And &H7)
- b = b Or ((locked.Pixels(i + 1) And &H3) << 3)
- b = b Or ((locked.Pixels(i + 2) And &H7) << 5)
- storedData(byte_index) = b
- i += 4
- Next
- locked.UnlockBits()
- bitmap.Dispose()
- ' Extract the data
- Dim dataLength As Integer = BitConverter.ToInt32(storedData, 0)
- If dataLength > 100 * 1024 * 1024 Then
- Throw New ArgumentException("Payload data exceeds 100MB")
- End If
- Dim result(dataLength - 1) As Byte
- System.Array.ConstrainedCopy(storedData, 4, result, 0, dataLength)
- Return result
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement