MaximusDebois Ответов: 0

Как скрыть файл в изображении с помощью стеганографии VB.NET


Я пишу стеганографию в vb.net чтобы скрыть файл в изображении. у меня есть этот код vb.net это скрывает файл в изображении, когда вы шифруете и извлекаете зашифрованный файл из изображения, когда вы расшифровываете, но проблема в том, что когда я расшифровываю, я получаю поврежденный файл. Это код:

[edit]добавлен блок кода-OriginalGriff [/edit]

Что я уже пробовал:

Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO
Public Class FileEncryption
    Private loadedTrueImagePath As String, loadedFilePath As String, saveToImage As String, DLoadImagePath As String, DSaveFilePath As String
		Private height As Integer, width As Integer
		Private fileSize As Long, fileNameSize As Long

    Private Sub browseimagebtn_Click(sender As Object, e As EventArgs) Handles browseimagebtn.Click
                If openFileDialog1.ShowDialog() = DialogResult.OK Then
                loadedTrueImagePath = openFileDialog1.FileName
				TextBox1.Text = loadedTrueImagePath
				loadedTrueImage = Image.FromFile(loadedTrueImagePath)
				height = loadedTrueImage.Height
				width = loadedTrueImage.Width
				loadedTrueBitmap = New Bitmap(loadedTrueImage)

				Dim imginf As New FileInfo(loadedTrueImagePath)
				Dim fs As Single = CSng(imginf.Length) / 1024
				ImageSize_lbl.Text = smalldecimal(fs.ToString(), 2) & " KB"
				ImageHeight_lbl.Text = loadedTrueImage.Height.ToString() & " Pixel"
				ImageWidth_lbl.Text = loadedTrueImage.Width.ToString() & " Pixel"
				Dim cansave As Double = (8.0 * ((height * (width \ 3) * 3) \ 3 - 1)) / 1024
				CanSave_lbl.Text = smalldecimal(cansave.ToString(), 2) & " KB"
                canPaint = True
				Me.Invalidate()
			End If
    End Sub

    Private Sub browsefilebtn_Click(sender As Object, e As EventArgs) Handles browsefilebtn.Click
        If openFileDialog2.ShowDialog() = DialogResult.OK Then
				loadedFilePath = openFileDialog2.FileName
				TextBox2.Text = loadedFilePath
				Dim finfo As New FileInfo(loadedFilePath)
				fileSize = finfo.Length
				fileNameSize = justFName(loadedFilePath).Length
			End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
				saveToImage = saveFileDialog1.FileName
			Else
				Return
			End If
			If TextBox1.Text = [String].Empty OrElse TextBox2.Text = [String].Empty Then
				MessageBox.Show("Encrypton information is incomplete!" & vbLf & "Please complete them frist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End If
			If 8 * ((height * (width \ 3) * 3) \ 3 - 1) < fileSize + fileNameSize Then
				MessageBox.Show("File size is too large!" & vbLf & "Please use a larger image to hide this file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
				Return
			End If
			fileContainer = File.ReadAllBytes(loadedFilePath)
			EncryptLayer()
    End Sub
    Private Sub EncryptLayer()
			toolStripStatusLabel1.Text = "Encrypting... Please wait"
			Application.DoEvents()
			Dim FSize As Long = fileSize
			Dim changedBitmap As Bitmap = EncryptLayer(8, loadedTrueBitmap, 0, (height * (width \ 3) * 3) \ 3 - fileNameSize - 1, True)
			FSize -= (height * (width \ 3) * 3) \ 3 - fileNameSize - 1
			If FSize > 0 Then
				Dim i As Integer = 7
				While i >= 0 AndAlso FSize > 0
					changedBitmap = EncryptLayer(i, changedBitmap, (((8 - i) * height * (width \ 3) * 3) \ 3 - fileNameSize - (8 - i)), (((9 - i) * height * (width \ 3) * 3) \ 3 - fileNameSize - (9 - i)), False)
					FSize -= (height * (width \ 3) * 3) \ 3 - 1
					i -= 1
				End While
			End If
			changedBitmap.Save(saveToImage)
			toolStripStatusLabel1.Text = "Encrypted image has been successfully saved."
			EncriptionDone = True
			AfterEncryption = Image.FromFile(saveToImage)
			Me.Invalidate()
		End Sub
    Private Function EncryptLayer(layer As Integer, inputBitmap As Bitmap, startPosition As Long, endPosition As Long, writeFileName As Boolean) As Bitmap
			Dim outputBitmap As Bitmap = inputBitmap
			layer -= 1
			Dim i As Integer = 0, j As Integer = 0
			Dim FNSize As Long = 0
			Dim t As Boolean() = New Boolean(7) {}
			Dim rb As Boolean() = New Boolean(7) {}
			Dim gb As Boolean() = New Boolean(7) {}
			Dim bb As Boolean() = New Boolean(7) {}
			Dim pixel As New Color()
			Dim r As Byte, g As Byte, b As Byte

			If writeFileName Then
				FNSize = fileNameSize
				Dim fileName As String = justFName(loadedFilePath)

				'write fileName:
				i = 0
				While i < height AndAlso i * (height \ 3) < fileNameSize
					j = 0
					While j < (width \ 3) * 3 AndAlso i * (height \ 3) + (j \ 3) < fileNameSize
						byte2bool(CByte(AscW(fileName(i * (height \ 3) + j \ 3))), t)
						pixel = inputBitmap.GetPixel(j, i)
						r = pixel.R
						g = pixel.G
						b = pixel.B
						byte2bool(r, rb)
						byte2bool(g, gb)
						byte2bool(b, bb)
						If j Mod 3 = 0 Then
							rb(7) = t(0)
							gb(7) = t(1)
							bb(7) = t(2)
						ElseIf j Mod 3 = 1 Then
							rb(7) = t(3)
							gb(7) = t(4)
							bb(7) = t(5)
						Else
							rb(7) = t(6)
							gb(7) = t(7)
						End If
						Dim result As Color = Color.FromArgb(CInt(bool2byte(rb)), CInt(bool2byte(gb)), CInt(bool2byte(bb)))
						outputBitmap.SetPixel(j, i, result)
						j += 1
					End While
					i += 1
				End While
				i -= 1
			End If
			'write file (after file name):
			Dim tempj As Integer = j

			While i < height AndAlso i * (height \ 3) < endPosition - startPosition + FNSize AndAlso startPosition + i * (height \ 3) < fileSize + FNSize
				j = 0
				While j < (width \ 3) * 3 AndAlso i * (height \ 3) + (j \ 3) < endPosition - startPosition + FNSize AndAlso startPosition + i * (height \ 3) + (j \ 3) < fileSize + FNSize
					If tempj <> 0 Then
						j = tempj
						tempj = 0
					End If
					byte2bool(CByte(fileContainer(startPosition + i * (height \ 3) + j \ 3 - FNSize)), t)
					pixel = inputBitmap.GetPixel(j, i)
					r = pixel.R
					g = pixel.G
					b = pixel.B
					byte2bool(r, rb)
					byte2bool(g, gb)
					byte2bool(b, bb)
					If j Mod 3 = 0 Then
						rb(layer) = t(0)
						gb(layer) = t(1)
						bb(layer) = t(2)
					ElseIf j Mod 3 = 1 Then
						rb(layer) = t(3)
						gb(layer) = t(4)
						bb(layer) = t(5)
					Else
						rb(layer) = t(6)
						gb(layer) = t(7)
					End If
					Dim result As Color = Color.FromArgb(CInt(bool2byte(rb)), CInt(bool2byte(gb)), CInt(bool2byte(bb)))

					outputBitmap.SetPixel(j, i, result)
					j += 1
				End While
				i += 1
			End While
			Dim tempFS As Long = fileSize, tempFNS As Long = fileNameSize
			r = CByte(tempFS Mod 100)
			tempFS /= 100
			g = CByte(tempFS Mod 100)
			tempFS /= 100
			b = CByte(tempFS Mod 100)
			Dim flenColor As Color = Color.FromArgb(r, g, b)
			outputBitmap.SetPixel(width - 1, height - 1, flenColor)

			r = CByte(tempFNS Mod 100)
			tempFNS /= 100
			g = CByte(tempFNS Mod 100)
			tempFNS /= 100
			b = CByte(tempFNS Mod 100)
			Dim fnlenColor As Color = Color.FromArgb(r, g, b)
			outputBitmap.SetPixel(width - 2, height - 1, fnlenColor)

			Return outputBitmap
		End Function
Private Sub DecryptLayer()
			toolStripStatusLabel1.Text = "Decrypting... Please wait"
			Application.DoEvents()
			Dim i As Integer, j As Integer = 0
			Dim t As Boolean() = New Boolean(7) {}
			Dim rb As Boolean() = New Boolean(7) {}
			Dim gb As Boolean() = New Boolean(7) {}
			Dim bb As Boolean() = New Boolean(7) {}
			Dim pixel As New Color()
			Dim r As Byte, g As Byte, b As Byte
			pixel = DecryptedBitmap.GetPixel(width - 1, height - 1)
			Dim fSize As Long = pixel.R + pixel.G * 100 + pixel.B * 10000
			pixel = DecryptedBitmap.GetPixel(width - 2, height - 1)
			Dim fNameSize As Long = pixel.R + pixel.G * 100 + pixel.B * 10000
			Dim res As Byte() = New Byte(fSize - 1) {}
			Dim resFName As String = ""
			Dim temp As Byte

			'Read file name:
			i = 0
			While i < height AndAlso i * (height \ 3) < fNameSize
				j = 0
				While j < (width \ 3) * 3 AndAlso i * (height \ 3) + (j \ 3) < fNameSize
					pixel = DecryptedBitmap.GetPixel(j, i)
					r = pixel.R
					g = pixel.G
					b = pixel.B
					byte2bool(r, rb)
					byte2bool(g, gb)
					byte2bool(b, bb)
					If j Mod 3 = 0 Then
						t(0) = rb(7)
						t(1) = gb(7)
						t(2) = bb(7)
					ElseIf j Mod 3 = 1 Then
						t(3) = rb(7)
						t(4) = gb(7)
						t(5) = bb(7)
					Else
						t(6) = rb(7)
						t(7) = gb(7)
						temp = bool2byte(t)
						resFName += ChrW(temp)
					End If
					j += 1
				End While
				i += 1
			End While

			'Read file on layer 8 (after file name):
			Dim tempj As Integer = j
			i -= 1

			While i < height AndAlso i * (height \ 3) < fSize + fNameSize
				j = 0
				While j < (width \ 3) * 3 AndAlso i * (height \ 3) + (j \ 3) < (height * (width \ 3) * 3) \ 3 - 1 AndAlso i * (height \ 3) + (j \ 3) < fSize + fNameSize
					If tempj <> 0 Then
						j = tempj
						tempj = 0
					End If
					pixel = DecryptedBitmap.GetPixel(j, i)
					r = pixel.R
					g = pixel.G
					b = pixel.B
					byte2bool(r, rb)
					byte2bool(g, gb)
					byte2bool(b, bb)
					If j Mod 3 = 0 Then
						t(0) = rb(7)
						t(1) = gb(7)
						t(2) = bb(7)
					ElseIf j Mod 3 = 1 Then
						t(3) = rb(7)
						t(4) = gb(7)
						t(5) = bb(7)
					Else
						t(6) = rb(7)
						t(7) = gb(7)
						temp = bool2byte(t)
						res(i * (height \ 3) + j \ 3 - fNameSize) = temp
					End If
					j += 1
				End While
				i += 1
			End While

			'Read file on other layers:
			Dim readedOnL8 As Long = (height * (width \ 3) * 3) \ 3 - fNameSize - 1

			Dim layer As Integer = 6
			While layer >= 0 AndAlso readedOnL8 + (6 - layer) * ((height * (width \ 3) * 3) \ 3 - 1) < fSize
				i = 0
				While i < height AndAlso i * (height \ 3) + readedOnL8 + (6 - layer) * ((height * (width \ 3) * 3) \ 3 - 1) < fSize
					j = 0
					While j < (width \ 3) * 3 AndAlso i * (height \ 3) + (j \ 3) + readedOnL8 + (6 - layer) * ((height * (width \ 3) * 3) \ 3 - 1) < fSize
						pixel = DecryptedBitmap.GetPixel(j, i)
						r = pixel.R
						g = pixel.G
						b = pixel.B
						byte2bool(r, rb)
						byte2bool(g, gb)
						byte2bool(b, bb)
						If j Mod 3 = 0 Then
							t(0) = rb(layer)
							t(1) = gb(layer)
							t(2) = bb(layer)
						ElseIf j Mod 3 = 1 Then
							t(3) = rb(layer)
							t(4) = gb(layer)
							t(5) = bb(layer)
						Else
							t(6) = rb(layer)
							t(7) = gb(layer)
							temp = bool2byte(t)
							res(i * (height \ 3) + j \ 3 + (6 - layer) * ((height * (width \ 3) * 3) \ 3 - 1) + readedOnL8) = temp
						End If
						j += 1
					End While
					i += 1
				End While
				layer -= 1
			End While

			If File.Exists(DSaveFilePath & "\" & resFName) Then
				MessageBox.Show("File """ & resFName & """ already exist please choose another path to save file", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
				Return
			Else
				File.WriteAllBytes(DSaveFilePath & "\" & resFName, res)
			End If
			toolStripStatusLabel1.Text = "Decrypted file has been successfully saved."
			Application.DoEvents()
		End Sub
    Private Sub byte2bool(inp As Byte, ByRef outp As Boolean())
            If inp >= 0 AndAlso inp <= 255 Then
                For i As Short = 7 To 0 Step -1
                    If inp Mod 2 = 1 Then
                        outp(i) = True
                    Else
                        outp(i) = False
                    End If
                    inp /= 2
                Next
            Else
                Throw New Exception("Input number is illegal.")
            End If
        End Sub
     Private Function bool2byte(inp As Boolean()) As Byte
			Dim outp As Byte = 0
			For i As Short = 7 To 0 Step -1
				If inp(i) Then
					outp += CByte(Math.Truncate(Math.Pow(2.0, CDbl(7 - i))))
				End If
			Next
			Return outp
		End Function

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TextBox3.Text = [String].Empty OrElse TextBox4.Text = [String].Empty Then
				MessageBox.Show("Text boxes must not be empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])

				Return
			End If

			If System.IO.File.Exists(TextBox4.Text) = False Then
				MessageBox.Show("Select image file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
				TextBox4.Focus()
				Return
			End If



			DecryptLayer()
    End Sub

    Private Sub decryptbrowsefilebtn_Click(sender As Object, e As EventArgs) Handles decryptbrowsefilebtn.Click
        If openFileDialog3.ShowDialog() = DialogResult.OK Then
				DLoadImagePath = openFileDialog3.FileName
				TextBox4.Text = DLoadImagePath
				DecryptedImage = Image.FromFile(DLoadImagePath)
				height = DecryptedImage.Height
				width = DecryptedImage.Width
				DecryptedBitmap = New Bitmap(DecryptedImage)

				Dim imginf As New FileInfo(DLoadImagePath)
				Dim fs As Single = CSng(imginf.Length) / 1024
				ImageSize_lbl.Text = smalldecimal(fs.ToString(), 2) & " KB"
				ImageHeight_lbl.Text = DecryptedImage.Height.ToString() & " Pixel"
				ImageWidth_lbl.Text = DecryptedImage.Width.ToString() & " Pixel"
				Dim cansave As Double = (8.0 * ((height * (width \ 3) * 3) \ 3 - 1)) / 1024
				CanSave_lbl.Text = smalldecimal(cansave.ToString(), 2) & " KB"

				canPaint = True
				Me.Invalidate()
			End If
    End Sub

    Private Sub decryptsavebtn_Click(sender As Object, e As EventArgs) Handles decryptsavebtn.Click
        If folderBrowserDialog1.ShowDialog() = DialogResult.OK Then
				DSaveFilePath = folderBrowserDialog1.SelectedPath
				TextBox3.Text = DSaveFilePath
			End If
		End Sub

		
    Private Function justFName(path As String) As String
            Dim output As String
            Dim i As Integer
            If path.Length = 3 Then
                ' i.e: "C:\\"
                Return path.Substring(0, 1)
            End If
            For i = path.Length - 1 To 1 Step -1
                If path(i) = "\"c Then
                    Exit For
                End If
            Next
            output = path.Substring(i + 1)
            Return output
        End Function
Private Function justEx(fName As String) As String
			Dim output As String
			Dim i As Integer
			For i = fName.Length - 1 To 1 Step -1
				If fName(i) = "."C Then
					Exit For
				End If
			Next
			output = fName.Substring(i + 1)
			Return output
		End Function

    Private Sub decryptbtn_Paint(sender As Object, e As PaintEventArgs) Handles decryptbtn.Paint

    End Sub

    Private Sub encryptbtn_Paint(sender As Object, e As PaintEventArgs) Handles encryptbtn.Paint
If canPaint Then
				Try
					If Not EncriptionDone Then
						e.Graphics.DrawImage(loadedTrueImage, previewImage)
					Else
						e.Graphics.DrawImage(AfterEncryption, previewImage)
					End If
				Catch
					e.Graphics.DrawImage(DecryptedImage, previewImage)
				End Try
			End If

    End Sub

    Private Function smalldecimal(inp As String, dec As Integer) As String
			Dim i As Integer
			For i = inp.Length - 1 To 1 Step -1
				If inp(i) = "."C Then
					Exit For
				End If
			Next
			Try
				Return inp.Substring(0, i + dec + 1)
			Catch
				Return inp
			End Try
		End Function


    Private loadedTrueImage As Image, DecryptedImage As Image, AfterEncryption As Image
		Private loadedTrueBitmap As Bitmap, DecryptedBitmap As Bitmap
		Private previewImage As New Rectangle(20, 160, 490, 470)
		Private canPaint As Boolean = False, EncriptionDone As Boolean = False
		Private fileContainer As Byte()
End Class

OriginalGriff

И что?
Что вы пытались сделать, чтобы понять, в чем проблема?
Какую отладку вы сделали?

0 Ответов