Cryprtography (Text - Picture)
Posted: Thu Jan 20, 2011 1:49 pm
Hello to my tutorial on how to make program that will do what title says. ;)
Let's begin!
Source code may be downloaded here:
viewtopic.php?f=71&t=4516&#p32971
ADD:
2 Buttons:
- Button1 //Load image
- Button2 //Preform action
2 Radiobuttons:
-RadioButton1 //Encrypt Text
-RadioButton2 //Decrypt Text
3 Textboxes:
-TextBox1 //Text that's going to "go" in picture
-TextBox2 //This one will show picture's location
-TextBox3 //Text etrieved from picture
1 PictureBox
-PictureBox1 //Preview of picture
1 Openfiledialog
-openpic //Opens picture
1 Savefiledialog
-buildpic //Saves picture
CODE(s):
Bellow "Public class form1":
Button1:
RadioButton1:
openPic_fileOK:
Sub ResizeFileName(ByVal LongFileName As String, ByVal ShortFileName As String):
Result:
1:


Source code is comming soon, link will be Here!
Let's begin!
Source code may be downloaded here:
viewtopic.php?f=71&t=4516&#p32971
ADD:
2 Buttons:
- Button1 //Load image
- Button2 //Preform action
2 Radiobuttons:
-RadioButton1 //Encrypt Text
-RadioButton2 //Decrypt Text
3 Textboxes:
-TextBox1 //Text that's going to "go" in picture
-TextBox2 //This one will show picture's location
-TextBox3 //Text etrieved from picture
1 PictureBox
-PictureBox1 //Preview of picture
1 Openfiledialog
-openpic //Opens picture
1 Savefiledialog
-buildpic //Saves picture
CODE(s):
Bellow "Public class form1":
Code: Select all
Dim PicBuffer As System.IO.FileInfo
Button1:
Code: Select all
Button2:
openPic.Title = "Open Picture Files"
openPic.ShowDialog()
Code: Select all
Dim Ready As Boolean = True
Dim PicFileStream As System.IO.FileStream
Try
PicFileStream = PicBuffer.OpenRead
Catch ex As Exception
Ready = False
MsgBox("Please load a picture before clicking this button", MsgBoxStyle.Critical, "Error")
End Try
If Ready = True Then
Dim PicBytes As Long = PicFileStream.Length
Dim PicExt As String = PicBuffer.Extension
Dim PicByteArray(PicBytes) As Byte
PicFileStream.Read(PicByteArray, 0, PicBytes)
Dim SentinelString() As Byte = {73, 116, 83, 116, 97, 114, 116, 115, 72, 101, 114, 101}
If RadioButton1.Checked = True Then
Dim PlainText As String = TextBox1.Text
Dim PlainTextByteArray(PlainText.Length) As Byte
For i As Integer = 0 To (PlainText.Length - 1)
PlainTextByteArray(i) = CByte(AscW(PlainText.Chars(i)))
Application.DoEvents()
Next
Dim PicAndText(PicBytes + PlainText.Length + SentinelString.Length) As Byte
For t As Long = 0 To (PicBytes - 1)
PicAndText(t) = PicByteArray(t)
Next
Dim count As Integer = 0
For r As Long = PicBytes To (PicBytes + (SentinelString.Length) - 1)
PicAndText(r) = SentinelString(count)
count += 1
Next
count = 0
For q As Long = (PicBytes + SentinelString.Length) To (PicBytes + SentinelString.Length + PlainText.Length - 1)
PicAndText(q) = PlainTextByteArray(count)
count += 1
Next
buildPic.ShowDialog()
Dim NewFileName As String = buildPic.FileName
My.Computer.FileSystem.WriteAllBytes(NewFileName, PicAndText, False)
ElseIf RadioButton2.Checked Then
TextBox3.Clear()
Dim OutterSearch, InnerSearch, StopSearch As Boolean
OutterSearch = True
InnerSearch = True
StopSearch = False
Dim count As Long = 0
Dim leftCounter As Long
Dim rightCounter As Integer
leftCounter = 0
rightCounter = 0
Do While (count < (PicBytes - SentinelString.Length) And StopSearch = False)
If (PicByteArray(count) = SentinelString(0)) Then
leftCounter = count + 1
rightCounter = 1
InnerSearch = True
Do While (InnerSearch = True) And (rightCounter < SentinelString.Length) _
And (leftCounter < PicByteArray.Length)
If (PicByteArray(leftCounter) = SentinelString(rightCounter)) Then
rightCounter += 1
leftCounter += 1
If (rightCounter = (SentinelString.Length - 1)) Then
StopSearch = True
End If
Else
InnerSearch = False
count += 1
End If
Loop
Else
count += 1
End If
Loop
If StopSearch = True Then
'leftCounter contains the starting string that is being retrieved
Do While (leftCounter < PicBytes)
'Bytes need to be converted to an integer
'then to an unicode character which will be the plaintext
TextBox3.AppendText(ChrW(CInt(PicByteArray(leftCounter))))
leftCounter += 1
Loop
Else
TextBox3.Text = "The Picture does not contain any text"
End If
End If
End If
RadioButton1:
Code: Select all
RadioButton2:
TextBox1.Enabled = True
Code: Select all
TextBox1.Enabled = False
openPic_fileOK:
Code: Select all
PictureBox1.Image = Image.FromFile(openPic.FileName)
PicBuffer = New System.IO.FileInfo(openPic.FileName)
ResizeFileName(openPic.FileName, PicBuffer.Name)
Sub ResizeFileName(ByVal LongFileName As String, ByVal ShortFileName As String):
Code: Select all
If LongFileName.Length > 71 Then
Dim LongFileNameSize As Integer = LongFileName.Length
Dim ShortFileNameSize As Integer = ShortFileName.Length
Dim Cut As Integer = 71 - (5 + ShortFileNameSize)
Dim i As Integer
TextBox2.Clear()
For i = 0 To (Cut) - 1
TextBox2.AppendText(LongFileName.Chars(i))
Next
For i = 0 To 4
TextBox2.AppendText(".")
Next
For i = 0 To (ShortFileNameSize - 1)
TextBox2.AppendText(ShortFileName(i))
Next
Else
TextBox2.Text = LongFileName
End If
Result:
1:


Source code is comming soon, link will be Here!