Implementasi Algoritma Kriptografi RC5 dan Metode Steganografi Least Significant Bit (LSB) Dalam Pengamanan File Teks

A-1

LISTING PROGRAM

1.

Form1.vb
Imports
Imports
Imports
Imports
Imports
Imports
Imports
Imports

System.Runtime.InteropServices
Microsoft.Office.Interop.Word
System.Text
System.IO
System

System.Xml
Microsoft.Office.Interop
System.Numerics

Public Class Form1
Dim w As Integer = 32 'block size dalam bit
Dim Pw As BigInteger = hextodec("B7E15163"), Qw As BigInteger
hextodec("9E3779B9") 'nilai magic untuk block size 32 bit
Dim round As Integer = 12 'jumlah round
Dim b_kecil As Integer = 16 'panjang password dalam byte/16 karakter
Dim u As Integer = w / 8
Dim c As Integer = b_kecil / u
Dim t As Integer = 2 * round + 2
Dim S As BigInteger() = New BigInteger(t) {}
Dim L As BigInteger() = New BigInteger(c) {}

=

Function bit_to_string(ByVal a As String) As String
Dim

Dim
Dim
For

total As Integer = 0
sb, sb1 As New StringBuilder
temp As String = ""
i As Integer = 1 To Len(a)
sb.Append(Mid(a, i, 1))
If (Len(sb.ToString) = 8) Then
temp += Chr(Bin_To_Dec(sb.ToString))
sb.Clear()
End If
Next
Return temp
End Function
Function string_to_bit(ByVal input As String) As String
Dim Result As String = ""
For Each C As Char In input
Dim s As String = System.Convert.ToString(AscW(C), 2).PadLeft(8,

"0")
Result &= s
Next
Return Result
End Function
Function desimal2biner(ByVal input As Long, ByVal bit As Integer) As String
Dim biner_1 As String = ""
Dim tempbits As Long = Math.Pow(2, bit - 1)
Dim inputcopy As Long = input
For i As Integer = 0 To bit - 1
If (inputcopy >= tempbits) Then
inputcopy -= tempbits
biner_1 = biner_1 + "1"
Else
biner_1 = biner_1 + "0"
End If
If (tempbits > 1) Then
tempbits = tempbits / 2
End If
Next

Return biner_1
End Function
Function biner2desimal(ByVal input As String, ByVal bit As Integer) As
Integer
Return Convert.ToInt16(input, 2)
End Function

Universitas Sumatera Utara

A-2

Function rotasikiri(ByVal input As Long, ByVal count As Integer, ByVal bit
As Integer) As Long
Dim biner As String = desimal2biner(input, bit)
Dim binerrotasikiri As String = biner.Substring(count, biner.Length count) + biner.Substring(0, count)
Return Convert.ToUInt64(binerrotasikiri, 2)
End Function
Function rotasikanan(ByVal input As Long, ByVal count As Integer, ByVal bit
As Integer) As Long
Dim biner As String = desimal2biner(input, bit)

Dim binerrotasikanan As String = biner.Substring(biner.Length - count,
count) + biner.Substring(0, biner.Length - count)
Return Convert.ToUInt64(binerrotasikanan, 2)
End Function
Function dectohex(ByVal dec As Long) As String
Dim sHexValue As String = Hex$(dec)
If (sHexValue.Length < 8) Then
For i As Integer = 1 To 8 - sHexValue.Length
sHexValue = "0" + sHexValue
Next
End If
Return sHexValue
End Function
Function hextodec(ByVal hex As String) As BigInteger
Return Convert.ToInt64(hex, 16)
End Function
Function hextostring(ByVal hex As String) As String
Dim sb As New StringBuilder
For x = 0 To hex.Length - 1 Step 2
Dim k As String = hex.Substring(x, 2)

sb.Append(System.Convert.ToChar(System.Convert.ToUInt32(k, 16)))
Next
Return sb.ToString
End Function
Function stringtohex(ByVal input As String) As String
Dim a As String = input
Dim temp As String = ""
Dim temp2 As String = ""
Dim sb As New StringBuilder
Dim hex_string As String = ""
For x As Integer = 1 To Len(a)
temp = Mid(a, x, 1)
temp2 = System.Convert.ToString(Asc(temp), 2) 'mengkonversi string
langsung ke bitnya
hex_string = Hex(Convert.ToInt32(temp2, 2))
If (hex_string.Length < 2) Then
hex_string = "0" + hex_string
End If
sb.Append(hex_string)
Next

Return sb.ToString
End Function
Function Bin_To_Dec(ByVal Bin As String) As String 'function to convert a
binary number to decimal
Dim dec As Double = Nothing
Dim length As Integer = Len(Bin)
Dim temp As Integer = Nothing
Dim x As Integer = Nothing
For x = 1 To length
temp = Val(Mid(Bin, length, 1))
length = length - 1
If temp "0" Then
dec += (2 ^ (x - 1))
End If
Next
Return dec
End Function
Function index2x(ByVal index As BigInteger, ByVal width As BigInteger)
'mencari pixel x
Return index Mod width

End Function

Universitas Sumatera Utara

A-3

Function index2y(ByVal index As BigInteger, ByVal width As BigInteger)
'mencari pixel y
Return (index - (index Mod width)) / width
End Function
Function dec_to_bin(ByVal dec As Integer) As String ' function to convert
decimal to binary
Dim temp As String = Convert.ToString(dec, 2)
If (Len(temp) < 8) Then ' jika pjng bitnya kurang dari 8
While (Len(temp) < 8) ' selama pnjg bit kurang dr 8 tambahkan 0 ex:
1010 -> 00001010
temp = "0" + temp
End While
End If
Return temp

End Function

'tab encrypt and embed
'==================================================
Private Sub Button1_Click(ByVal sender As System.Object,
System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter
=
"Text
(*.txt,*.docx,*.doc,*.rtf)|*.txt;*.docx;*.doc;*.rtf"

ByVal

e

As

Files

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

If Not String.IsNullOrEmpty(OpenFileDialog1.FileName) Then
Dim pathfile, ext, filename As String
pathfile
=
System.IO.Path.GetDirectoryName(OpenFileDialog1.FileName)
filename = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
ext = System.IO.Path.GetExtension(OpenFileDialog1.FileName)
If (ext = ".docx" Or ext = ".doc" Or ext = ".rtf") Then
Dim wdApp As New Word.Application
Dim wdDoc As New Word.Document
wdDoc = wdApp.Documents.Open(OpenFileDialog1.FileName)
Dim myText As String = wdDoc.Range.Text
RichTextBox1.Text = myText
Label13.Text = RichTextBox1.Text.Length
wdApp.Quit()
Else
RichTextBox1.Text
=
System.IO.File.ReadAllText(OpenFileDialog1.FileName)
End If

End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
OpenFileDialog1.Filter
=
"Image
Files
(*.jpeg,*.bmp,*.png,*.jpg)|*.jpeg;*.bmp;*.png;*.jpg"
OpenFileDialog1.FilterIndex = 1
Dim filepath, filename As String
filepath = System.IO.Path.GetDirectoryName(OpenFileDialog1.FileName)
filename = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
If Not String.IsNullOrEmpty(OpenFileDialog1.FileName) Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
Dim data As Bitmap = New Bitmap(PictureBox1.Image)
panjang_gambar.Text = data.Width
tinggi_gambar.Text = data.Height
Else
End If
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click

Universitas Sumatera Utara

A-4

If PictureBox1.Image IsNot Nothing Then
SaveFileDialog1.Filter = "Image Files (*.jpeg)|*.jpeg"
If
SaveFileDialog1.ShowDialog
System.Windows.Forms.DialogResult.OK Then
PictureBox1.Image.Save(SaveFileDialog1.FileName)
End If
End If
' save the image to the desktop
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e
System.EventArgs) Handles Button3.Click
If String.IsNullOrEmpty(password.Text) Then
MsgBox("Password Harus Diisi!")
ElseIf PictureBox1.Image Is Nothing Or RichTextBox1.Text = "" Then
MsgBox("teks atau gambar belum ada")

=

As

Else
Try
' RC5 32/12/16
Dim plaintext As String = stringtohex(RichTextBox1.Text)
Dim pass As String = stringtohex(password.Text)
Dim x As Integer
Dim watch As Stopwatch = Stopwatch.StartNew()
'Padding Time
'==================================================
'Padding password
If (pass.Length Mod 32 0) Then
Dim sisa = 32 - pass.Length
For x = 0 To sisa - 1
pass = pass + "0"
Next
End If
'==================================================
'Padding plaintext
'==================================================
If (plaintext.Length < 16) Then
Dim sisa = 16 - plaintext.Length
For x = 0 To sisa - 1
plaintext = plaintext + "0"
Next
Else
If (plaintext.Length Mod 8 0) Then
Dim sisa = 8 - plaintext.Length Mod 8
For x = 0 To sisa - 1
plaintext = plaintext + "0"
Next
End If
End If
'Pembuatan Array S
'==================================================
S(0) = Pw
Dim i As Integer
For i = 1 To t - 1
S(i) = (S(i - 1) + Qw) And 4294967295
Next
'==================================================
'Pembuatan array L
'==================================================
Dim j As Integer
For i = 0 To 3 Step 1
Dim backward As String = ""
For j = 0 To 3 Step 1
backward &= pass.Substring((i * 8) + (6 - (j * 2)), 2)
Next
L(i) = hextodec(backward)
Next
'==================================================
'Pencampuran Array L dan S

Universitas Sumatera Utara

A-5

'==================================================
j = 0
i = 0
Dim A As BigInteger = 0
Dim B As BigInteger = 0
For y As Integer = 1 To 3 * Math.Max(t, c)
S(i) = rotasikiri((S(i) + A + B) And 4294967295, 3, 32)
A = S(i)
L(j) = rotasikiri((L(j) + A + B) And 4294967295, (A + B)
Mod 32, 32)
B = L(j)
i = (i + 1) Mod t
j = (j + 1) Mod c
Next
'==================================================
'ENCRYPTION
'==================================================
Dim ciphertext As String = ""
A = (hextodec(plaintext.Substring(0, 8)) + S(0)) And 4294967295
B = (hextodec(plaintext.Substring(8, 8)) + S(1)) And 4294967295
For i = 1 To round
A = (rotasikiri((A Xor B), B Mod 32, 32) + S(2 * i)) And
4294967295
B = (rotasikiri((B Xor A), A Mod 32, 32) + S(2 * i + 1))
And 4294967295
Next
ciphertext += dectohex(A)
'jika lebih dari 32 bit
If plaintext.Length > 16 Then
For y As Integer = 1 To (plaintext.Length / 8 - 2)
A = (B + S(0)) And 4294967295
B = (hextodec(plaintext.Substring(16 + (8 * (y - 1)),
8)) + S(1)) And 4294967295
For i = 1 To round
A = (rotasikiri((A Xor B), B Mod 32, 32) + S(2 *
i)) And 4294967295
B = (rotasikiri((B Xor A), A Mod 32, 32) + S(2 * i
+ 1)) And 4294967295
Next
ciphertext += dectohex(A)
Next
End If
ciphertext += dectohex(B)
'==================================================
' Embedding Time
'==================================================
Dim plainbit As String = string_to_bit(ciphertext)
Dim img As Bitmap = New Bitmap(PictureBox1.Image)
Dim Color As Color
Dim panjang, lebar, pixelx, pixely As BigInteger
Dim temp As String = ""
panjang = Val(panjang_gambar.Text)
lebar = Val(tinggi_gambar.Text)
Dim listAwal As New ArrayList
Dim listRandom As New ArrayList
For i = 0 To plainbit.Length - 1 '(panjang * (lebar - 1)) - 1 '
merandom sebanyak pixel kemudian dimasukkan ke array
listAwal.Add(i)
Next
Dim
Dim
Dim
Dim

z1
m1
a1
c1

As
As
As
As

BigInteger
BigInteger
BigInteger
BigInteger

=
=
=
=

1
Math.Pow(2, 32)
1664525
1013904223

For i = 0 To ((plainbit.Length - (plainbit.Length Mod 3) + 3) /
3) - 1 'Ini sudah N layer(plainbit.length) -> M pixel
z1 = (a1 * z1 + c1) Mod m1
Dim
indexnow
As
Integer
=
Math.Floor((Double.Parse(z1.ToString())
/
Double.Parse(m1.ToString()))
*
listAwal.Count)
listRandom.Add(listAwal(indexnow))
listAwal.RemoveAt(indexnow)

Universitas Sumatera Utara

A-6

Next
Dim RGB(3) As Byte
For i = 0 To ((plainbit.Length - (plainbit.Length Mod 3) +
(Math.Min(1, (plainbit.Length Mod 3)) * 3)) / 3) - 1
pixelx
=
index2x(BigInteger.Parse(listRandom(i).ToString()), panjang)
pixely
=
index2y(BigInteger.Parse(listRandom(i).ToString()), panjang)
Color = img.GetPixel(pixelx, pixely)
Dim LayerValue As Byte
Dim posisi_layer As Byte = 0
RGB(0) = Color.R
RGB(1) = Color.G
RGB(2) = Color.B
While posisi_layer + i * 3 < plainbit.Length And
posisi_layer < 3
temp
=
Integer.Parse(plainbit.Substring(i
*
3
+
posisi_layer, 1))
If (posisi_layer = 0) Then 'R
LayerValue = Color.R
ElseIf (posisi_layer = 1) Then 'G
LayerValue = Color.G
ElseIf (posisi_layer = 2) Then 'B
LayerValue = Color.B
End If
'LSB
If (LayerValue Mod 2 = 1) Then
'1
If (temp = 1) Then
'1
Else
'0
LayerValue = LayerValue - 1
End If
Else
'0
If (temp = 1) Then
'1
LayerValue = LayerValue + 1
Else
'0
End If
End If
RGB(posisi_layer) = LayerValue
posisi_layer = posisi_layer + 1
End While
img.SetPixel(pixelx, pixely, Color.FromArgb(RGB(0), RGB(1),
RGB(2)))
Next
'Memasukkan Panjang ciphertext ke dalam pixel-pixel di layer B
'==================================================
x = 0
Dim penanda As String = dec_to_bin(ciphertext.Length) + "0"
Do
penanda = penanda + "1"
x += 1
Loop Until x = Len(dec_to_bin(ciphertext.Length))
For i = 1 To Len(penanda)
Color = img.GetPixel(panjang - i, lebar - 1)
Dim pixbit4 As String = dec_to_bin(Val(Color.B))
Dim change4 As String = pixbit4.Substring(0, pixbit4.Length
- 1) & penanda.Substring(penanda.Length - i, 1)
img.SetPixel(panjang
i,
lebar
1,
Color.FromArgb(Color.R, Color.G, Bin_To_Dec(change4)))
Next
PictureBox1.Image = img
Button4.Visible = True
watch.Stop()

Universitas Sumatera Utara

A-7

Label9.Text = watch.Elapsed.TotalMilliseconds
MsgBox("Proses Selesai")
Catch ex As Exception
MsgBox("Cover Image Kurang Besar")
End Try
End If
End Sub
Private Sub Button8_Click_1(ByVal sender As System.Object,
System.EventArgs) Handles Button8.Click
If RichTextBox3.Text = RichTextBox5.Text Then
MsgBox("sama")
Else
Dim s1 As String
Dim s2 As String
Dim s3 As String

ByVal

e

As

s1 = RichTextBox3.Text
s2 = RichTextBox5.Text
s3 = ""
For i As Integer = 0 To s1.Length - 1
If (s1.Substring(i, 1) s2.Substring(i, 1)) Then
s3
=
s3
&
s1.Substring(i,
1)
&
"
"
&
string_to_bit(s1.Substring(i, 1)) & "->" & s2.Substring(i, 1) & " " &
string_to_bit(s2.Substring(i, 1)) & vbNewLine
End If
Next
RichTextBox5.Text = ""
RichTextBox5.Text = s3
End If
End Sub
'==================================================
'tab extract and decrypt
'==================================================
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button6.Click
OpenFileDialog1.Filter
=
"Image
Files
(*.jpeg,*.bmp,*.png,*.jpg)|*.jpeg;*.bmp;*.png;*.jpg"
OpenFileDialog1.FilterIndex = 1
Dim filepath, filename As String
filepath = System.IO.Path.GetDirectoryName(OpenFileDialog1.FileName)
filename = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
If Not String.IsNullOrEmpty(OpenFileDialog1.FileName) Then
PictureBox2.Image = Image.FromFile(OpenFileDialog1.FileName)
Dim data As Bitmap = New Bitmap(PictureBox2.Image)
Else
End If
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
If RichTextBox1.Text IsNot Nothing Then
SaveFileDialog1.DefaultExt = "*.txt"
SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt"
If
SaveFileDialog1.ShowDialog()
=
System.Windows.Forms.DialogResult.OK And (SaveFileDialog1.FileName.Length) > 0
Then
RichTextBox2.SaveFile(SaveFileDialog1.FileName,
RichTextBoxStreamType.UnicodePlainText)
End If
End If
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button7.Click
Dim ciphertext As String = ""
If String.IsNullOrEmpty(password2.Text) Then
MsgBox("Password Belum Diisi")
ElseIf PictureBox2.Image Is Nothing Then
MsgBox("stego image belum ada")
Else
'Proses Ekstraksi ciphertext

Universitas Sumatera Utara

A-8

'==================================================
Dim img As Bitmap = New Bitmap(PictureBox2.Image)
Dim pixbit5 As String
Dim batas As Integer
Dim penanda As String = ""
Dim Color As Color
Dim panjang, lebar As BigInteger
panjang = img.Width
lebar = img.Height
Dim watch As Stopwatch = Stopwatch.StartNew()
'Pengambilan Panjang ciphertext dari pixel2 di layer B
'==================================================
Dim r = 0
Do
r += 1
Color = img.GetPixel(panjang - r, lebar - 1)
pixbit5 = dec_to_bin(Val(Color.B))
Loop While pixbit5.Substring(pixbit5.Length - 1, 1) 0
Dim y = 0
batas = r
Do
r += 1
Color = img.GetPixel(panjang - r, lebar - 1)
pixbit5 = dec_to_bin(Val(Color.B))
penanda = pixbit5.Substring(pixbit5.Length - 1, 1) + penanda
y += 1
Loop Until y = batas - 1
'==================================================
Dim len_in_bit As Integer = Val(Bin_To_Dec(penanda)) * 8
Dim pixelx, pixely As BigInteger
Dim i As Integer
Dim listAwal As New ArrayList
Dim listRandom As New ArrayList
For i = 0 To len_in_bit - 1 ' merandom sebanyak pixel kemudian
dimasukkan ke array
listAwal.Add(i)
Next
Dim
Dim
Dim
Dim

z1
m1
a1
c1

As
As
As
As

BigInteger
BigInteger
BigInteger
BigInteger

=
=
=
=

1
Math.Pow(2, 32)
1664525
1013904223

For i = 0 To ((len_in_bit - (len_in_bit Mod 3) + 3) / 3) - 1 'Ini
sudah N layer(plainbit.length) -> M pixel
z1 = (a1 * z1 + c1) Mod m1
Dim
indexnow
As
Integer
=
Math.Floor((Double.Parse(z1.ToString())
/
Double.Parse(m1.ToString()))
*
listAwal.Count)
listRandom.Add(listAwal(indexnow))
listAwal.RemoveAt(indexnow)
Next
Dim RGB(3) As Byte
Dim kumpulanBit As String = ""
For i = 0 To ((len_in_bit - (len_in_bit Mod 3) + 3) / 3) - 1
pixelx
=
index2x(BigInteger.Parse(listRandom(i).ToString()),
panjang)
pixely

=

index2y(BigInteger.Parse(listRandom(i).ToString()),

panjang)
Color = img.GetPixel(pixelx, pixely)
Dim posisi_layer As Byte = 0
RGB(0) = Color.R
RGB(1) = Color.G
RGB(2) = Color.B
While posisi_layer + i * 3