Algoritma Serpent 2


Algoritma Serpent adalah salah satu algoritma yang dapat digunakan untuk melakukan enkripsi data sehingga data asli hanya dapat dibaca oleh seseorang yang memiliki kunci enkripsi tersebut. Contoh yang dibahas kali ini adalah mengenai enkripsi dan dekripsi dari sebuah kalimat.
Algoritma ini menggunakan teknik cipher blok dengan key simetris. Serpent memiliki ukuran blok 128 bit dan mendukung ukuran key 128, 192, dan 256 bit. Teknik cipher yang dilakukan menggunakan struktur jaringan substitusi permutasi dengan jumlah round 32 kali pada 4 blok dengan ukuran masing-masing 32 bit. Dalam masing-masing round akan dilakukan penerapan 1 dari 8 SBOX berukuran 4 bit x 4 bit sebanyak 32 kali secara paralel.


Langkah-langkah penggunaan algoritma ini adalah

1. Tentukan kalimat yang akan dienkrip

Console.WriteLine("Masukkan kalimat yang akan dienkrip: ")
Dim input As String = Console.ReadLine

2. Tentukan kata kunci enkripsi yang digunakan

Console.WriteLine("Masukkan kata kunci enkripsi: ")
Dim kataKunci As String = Console.ReadLine

3. Lakukan inisialisasi variabel yang digunakan oleh metode ini
Penjelasan lebih detail tentang fungsi ini dapat dilihat pada penjelasan skrip dibawah ini

Dim s As New Serpent()

* Skrip tersebut akan melakukan inisialisasi pada Class Serpent. Class ini berisi tentang variabel dan fungsi-fungsi yang digunakan untuk melakukan enkripsi dan dekripsi. Deklarasi Class Serpent adalah sebagai berikut:

Public Class Serpent
#Const BLOCK_REVERSE = True

    Private Structure blokData
         Dim Data() As Byte

        Public Sub Initialize()
            ReDim Preserve Data(3)
        End Sub
    End Structure

    Private l_key(139) As blokData
    Private RegX(3, 31) As Byte
    Private TheByte(7) As Byte
    Private byteArray() As Byte
    Private hiByte As Integer
    Private hiBound As Integer
	
	. . .
End Class

4. Lakukan enkripsi kalimat awal menggunakan algoritma ini
Penjelasan lebih detail tentang fungsi ini dapat dilihat pada penjelasan skrip dibawah ini

Dim hasilEnkripsi As String = s.Encrypt(input, kataKunci)

* Skrip tersebut akan menjalankan proses enkripsi yang terdapat dalam Class Serpent.

Public Function Encrypt(ByRef Text As String, Optional ByRef Key As String = "", Optional ByRef OutputInHex As Boolean = False) As String
	On Error Resume Next

	Dim blk1(3) As blokData
	Dim blk2(3) As blokData
	Dim StrBuffer() As Byte
	Dim RetVal() As Byte
	Dim Length, Count As Double
	Length = Len(Text)
	Length = IIf(Length = 0, 16, 16 * ((Length - 1) \ 16 + 1))
	ReDim StrBuffer(Length - 1)
	ReDim RetVal(Length - 1)
	SetKey(Key)
	SetBuffer(StrBuffer, Text, Length)

	For Count = 0 To Length - 1 Step 16
		SetBlock(blk1, StrBuffer, Count, 16)
		blokEncrypt(blk1, blk2)
		GetBlock(blk2, RetVal, Count, 16)
	Next

	Encrypt = System.Text.Encoding.Default.GetString(RetVal)
	If OutputInHex = True Then Encrypt = EnHex(Encrypt)
End Function

* Sedangkan proses enkripsi utama algoritma Serpent adalah sebagai berikut

Private Sub blokEncrypt(ByRef in_blk() As blokData, ByRef out_blk() As blokData)
	Dim g, e, c, a, b, d, f, h As blokData

#If BLOCK_REVERSE Then
	a = bswap(in_blk(3))
	b = bswap(in_blk(2))
	c = bswap(in_blk(1))
	d = bswap(in_blk(0))
#Else
	MCopy (in_blk(0), a)
	MCopy (in_blk(1), b)
	MCopy (in_blk(2), c)
	MCopy (in_blk(3), d)
#End If
	k_xor(0, a, b, c, d) : sb0(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(1, e, f, g, h) : sb1(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(2, a, b, c, d) : sb2(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(3, e, f, g, h) : sb3(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(4, a, b, c, d) : sb4(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(5, e, f, g, h) : sb5(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(6, a, b, c, d) : sb6(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(7, e, f, g, h) : sb7(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(8, a, b, c, d) : sb0(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(9, e, f, g, h) : sb1(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(10, a, b, c, d) : sb2(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(11, e, f, g, h) : sb3(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(12, a, b, c, d) : sb4(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(13, e, f, g, h) : sb5(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(14, a, b, c, d) : sb6(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(15, e, f, g, h) : sb7(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(16, a, b, c, d) : sb0(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(17, e, f, g, h) : sb1(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(18, a, b, c, d) : sb2(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(19, e, f, g, h) : sb3(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(20, a, b, c, d) : sb4(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(21, e, f, g, h) : sb5(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(22, a, b, c, d) : sb6(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(23, e, f, g, h) : sb7(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(24, a, b, c, d) : sb0(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(25, e, f, g, h) : sb1(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(26, a, b, c, d) : sb2(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(27, e, f, g, h) : sb3(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(28, a, b, c, d) : sb4(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(29, e, f, g, h) : sb5(e, f, g, h, a, b, c, d) : rot(a, b, c, d)
	k_xor(30, a, b, c, d) : sb6(a, b, c, d, e, f, g, h) : rot(e, f, g, h)
	k_xor(31, e, f, g, h) : sb7(e, f, g, h, a, b, c, d) : k_xor(32, a, b, c, d)
#If BLOCK_REVERSE Then
	out_blk(3) = bswap(a)
	out_blk(2) = bswap(b)
	out_blk(1) = bswap(c)
	out_blk(0) = bswap(d)
#Else
	MCopy (a, out_blk(0))
	MCopy (b, out_blk(1))
	MCopy (c, out_blk(2))
	MCopy (d, out_blk(3))
#End If
End Sub

5. Lakukan dekripsi dari kalimat yang telah terenkripsi
Penjelasan lebih detail tentang fungsi ini dapat dilihat pada penjelasan skrip dibawah ini

Dim hasilDekripsi As String = s.Decrypt(hasilEnkripsi)

* Skrip tersebut akan menjalankan proses dekripsi yang terdapat dalam Class Serpent.

Public Function Decrypt(ByRef Text As String, Optional ByRef Key As String = "", Optional ByRef IsTextInHex As Boolean = False) As String
	On Error Resume Next

	Dim blk1(3) As blokData
	Dim blk2(3) As blokData
	Dim Code() As Byte
	Dim StrBuffer() As Byte
	Dim RetVal As String
	Dim Count, Length As Double
	If IsTextInHex = True Then Text = DeHex(Text)

	Code = System.Text.Encoding.Default.GetBytes(Text)
	Length = UBound(Code) + 1
	ReDim StrBuffer(Length - 1)
	SetKey(Key)
	For Count = 0 To Length - 1 Step 16
		SetBlock(blk1, Code, Count, 16)
		blokDecrypt(blk1, blk2)
		GetBlock(blk2, StrBuffer, Count, 16)
	Next

	GetBuffer(StrBuffer, RetVal, Length)
	Decrypt = RetVal
End Function

* Sedangkan proses dekripsi utama algoritma Serpent adalah sebagai berikut

Private Sub blokDecrypt(ByRef in_blk() As blokData, ByRef out_blk() As blokData)
	Dim g, e, c, a, b, d, f, h As blokData

#If BLOCK_REVERSE Then
	a = bswap(in_blk(3))
	b = bswap(in_blk(2))
	c = bswap(in_blk(1))
	d = bswap(in_blk(0))
#Else
	MCopy (in_blk(0), a)
	MCopy (in_blk(1), b)
	MCopy (in_blk(2), c)
	MCopy (in_blk(3), d)
#End If
	k_xor(32, a, b, c, d) : ib7(a, b, c, d, e, f, g, h) : k_xor(31, e, f, g, h)
	irot(e, f, g, h) : ib6(e, f, g, h, a, b, c, d) : k_xor(30, a, b, c, d)
	irot(a, b, c, d) : ib5(a, b, c, d, e, f, g, h) : k_xor(29, e, f, g, h)
	irot(e, f, g, h) : ib4(e, f, g, h, a, b, c, d) : k_xor(28, a, b, c, d)
	irot(a, b, c, d) : ib3(a, b, c, d, e, f, g, h) : k_xor(27, e, f, g, h)
	irot(e, f, g, h) : ib2(e, f, g, h, a, b, c, d) : k_xor(26, a, b, c, d)
	irot(a, b, c, d) : ib1(a, b, c, d, e, f, g, h) : k_xor(25, e, f, g, h)
	irot(e, f, g, h) : ib0(e, f, g, h, a, b, c, d) : k_xor(24, a, b, c, d)
	irot(a, b, c, d) : ib7(a, b, c, d, e, f, g, h) : k_xor(23, e, f, g, h)
	irot(e, f, g, h) : ib6(e, f, g, h, a, b, c, d) : k_xor(22, a, b, c, d)
	irot(a, b, c, d) : ib5(a, b, c, d, e, f, g, h) : k_xor(21, e, f, g, h)
	irot(e, f, g, h) : ib4(e, f, g, h, a, b, c, d) : k_xor(20, a, b, c, d)
	irot(a, b, c, d) : ib3(a, b, c, d, e, f, g, h) : k_xor(19, e, f, g, h)
	irot(e, f, g, h) : ib2(e, f, g, h, a, b, c, d) : k_xor(18, a, b, c, d)
	irot(a, b, c, d) : ib1(a, b, c, d, e, f, g, h) : k_xor(17, e, f, g, h)
	irot(e, f, g, h) : ib0(e, f, g, h, a, b, c, d) : k_xor(16, a, b, c, d)
	irot(a, b, c, d) : ib7(a, b, c, d, e, f, g, h) : k_xor(15, e, f, g, h)
	irot(e, f, g, h) : ib6(e, f, g, h, a, b, c, d) : k_xor(14, a, b, c, d)
	irot(a, b, c, d) : ib5(a, b, c, d, e, f, g, h) : k_xor(13, e, f, g, h)
	irot(e, f, g, h) : ib4(e, f, g, h, a, b, c, d) : k_xor(12, a, b, c, d)
	irot(a, b, c, d) : ib3(a, b, c, d, e, f, g, h) : k_xor(11, e, f, g, h)
	irot(e, f, g, h) : ib2(e, f, g, h, a, b, c, d) : k_xor(10, a, b, c, d)
	irot(a, b, c, d) : ib1(a, b, c, d, e, f, g, h) : k_xor(9, e, f, g, h)
	irot(e, f, g, h) : ib0(e, f, g, h, a, b, c, d) : k_xor(8, a, b, c, d)
	irot(a, b, c, d) : ib7(a, b, c, d, e, f, g, h) : k_xor(7, e, f, g, h)
	irot(e, f, g, h) : ib6(e, f, g, h, a, b, c, d) : k_xor(6, a, b, c, d)
	irot(a, b, c, d) : ib5(a, b, c, d, e, f, g, h) : k_xor(5, e, f, g, h)
	irot(e, f, g, h) : ib4(e, f, g, h, a, b, c, d) : k_xor(4, a, b, c, d)
	irot(a, b, c, d) : ib3(a, b, c, d, e, f, g, h) : k_xor(3, e, f, g, h)
	irot(e, f, g, h) : ib2(e, f, g, h, a, b, c, d) : k_xor(2, a, b, c, d)
	irot(a, b, c, d) : ib1(a, b, c, d, e, f, g, h) : k_xor(1, e, f, g, h)
	irot(e, f, g, h) : ib0(e, f, g, h, a, b, c, d) : k_xor(0, a, b, c, d)
#If BLOCK_REVERSE Then
	out_blk(3) = bswap(a)
	out_blk(2) = bswap(b)
	out_blk(1) = bswap(c)
	out_blk(0) = bswap(d)
#Else
	MCopy (a, out_blk(0))
	MCopy (b, out_blk(1))
	MCopy (c, out_blk(2))
	MCopy (d, out_blk(3))
#End If
End Sub


Hasil akhir adalah: (klik untuk perbesar gambar)

cmd120a

cmd120b


Contoh modul / source code dalam bahasa VB (Visual Basic) dapat didownload disini:



Jika membutuhkan jasa kami dalam pembuatan program, keterangan selanjutnya dapat dilihat di Fasilitas dan Harga
Jika ada yang kurang paham dengan langkah-langkah algoritma diatas, silahkan berikan komentar Anda.
Selamat mencoba.


Tinggalkan Balasan ke pip Batalkan balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

2 pemikiran di “Algoritma Serpent