Advertisement
Guest User

IBENC

a guest
Dec 14th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. Public Function EncryptData(ByVal fake As Image, ByVal Data As Byte()) As Image
  2. ' Make a new data array that appends the length of the array in front
  3. Dim StoredData(Data.Length + 4 - 1) As Byte
  4. Dim lengthBytes() As Byte = BitConverter.GetBytes(CType(Data.Length, Int32))
  5. System.Array.ConstrainedCopy(lengthBytes, 0, StoredData, 0, 4)
  6. System.Array.ConstrainedCopy(Data, 0, StoredData, 4, Data.Length)
  7.  
  8. ' Now generate an image using the fake that can fit all this data in the pixels
  9. Dim ratio As Double = CDbl(fake.Width) / CDbl(fake.Height)
  10. Dim pixelCount As Integer = StoredData.Length
  11. Dim imgWidth As Integer = Math.Ceiling(Math.Sqrt(CDbl(pixelCount) * ratio))
  12. Dim imgHeight As Integer = Math.Ceiling(Math.Sqrt(CDbl(pixelCount) / ratio))
  13. Dim result As New Bitmap(imgWidth, imgHeight)
  14. Using g As Graphics = Graphics.FromImage(result)
  15. g.DrawImage(fake, 0, 0, imgWidth, imgHeight)
  16. End Using
  17.  
  18. ' Use a lockbitmap to store the data into the image
  19. Dim locked As New Commons.Imaging.LockBitmap(result)
  20. locked.LockBits()
  21. If locked.Pixels.Count <> (imgWidth * imgHeight * 4) Then
  22. Throw New ArgumentException("Dest Pixel format is invalid")
  23. End If
  24. Dim i As Integer = 0
  25. For Each b As Byte In StoredData
  26. locked.Pixels(i + 0) = (locked.Pixels(i + 0) And Not &H7) Or (b And &H7)
  27. locked.Pixels(i + 1) = (locked.Pixels(i + 1) And Not &H3) Or ((b >> 3) And &H3)
  28. locked.Pixels(i + 2) = (locked.Pixels(i + 2) And Not &H7) Or ((b >> 5) And &H7)
  29. i += 4
  30. Next
  31. locked.UnlockBits()
  32. Return result
  33. End Function
  34.  
  35. Public Function DecryptData(ByVal image As Image) As Byte()
  36. ' Convert to ARGB bitmap
  37. Dim bitmap As Bitmap
  38. If TypeOf image Is Bitmap Then
  39. bitmap = CType(image.Clone(), Bitmap)
  40. Else
  41. bitmap = New Bitmap(image)
  42. End If
  43.  
  44. ' Read all the pixels using lockbitmap
  45. Dim pixelCount As Integer = image.Width * image.Height
  46. Dim storedData(pixelCount - 1) As Byte
  47. Dim i As Integer = 0
  48. Dim locked As New Commons.Imaging.LockBitmap(bitmap)
  49. locked.LockBits()
  50. For byte_index As Integer = 0 To pixelCount - 1
  51. Dim b As Byte = 0
  52. b = b Or (locked.Pixels(i + 0) And &H7)
  53. b = b Or ((locked.Pixels(i + 1) And &H3) << 3)
  54. b = b Or ((locked.Pixels(i + 2) And &H7) << 5)
  55. storedData(byte_index) = b
  56. i += 4
  57. Next
  58. locked.UnlockBits()
  59. bitmap.Dispose()
  60.  
  61. ' Extract the data
  62. Dim dataLength As Integer = BitConverter.ToInt32(storedData, 0)
  63. If dataLength > 100 * 1024 * 1024 Then
  64. Throw New ArgumentException("Payload data exceeds 100MB")
  65. End If
  66. Dim result(dataLength - 1) As Byte
  67. System.Array.ConstrainedCopy(storedData, 4, result, 0, dataLength)
  68. Return result
  69. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement