Algoritma Blowfish 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. Blowfish memiliki ukuran blok 64 bit dan panjang key dari 32 sampai 448 bit. Blowfish menggunakan cipher Feistel dan menggunakan parameter SBOX yang sangat besar dan nilainya bergantung dari key. Strukturnya sangat mirip dengan struktur CAST-128, yang menggunakan SBOX dengan nilai tetap.
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 xc As New CryptCore() xc.InitCore() xc.Key = kataKunci
* Skrip tersebut akan melakukan inisialisasi pada Class CryptCore. Class ini adalah inti class untuk pemanggilan fungsi enkripsi dan dekripsi yang mengacu pada algoritma blowfish. Deklarasi Class CryptCore adalah sebagai berikut:
Public Class CryptCore Private _key As String = Nothing Public Property Key() As String Get Return _key End Get Set(value As String) _key = Me.formatKey(value) End Set End Property Private Function formatKey(key As String) As String If key Is Nothing OrElse key.Length = 0 Then Return Nothing End If Return key.Trim() End Function Private DefaultKey As String = "" Public Sub New() DefaultKey = "enkripsi" End Sub Private _coreSymmetric As CoreAlgoritmaSymmetric Public Function InitCore() As Boolean _coreSymmetric = New CoreAlgoritmaSymmetric() Return True End Function . . . End Class
* Pada saat melakukan proses InitCore, maka proses tersebut akan melakukan inisialisasi pada Class CoreAlgoritmaSymmetric. Pemanggilan fungsi enkripsi / dekripsi pada Class sebelumnya akan mengarah pada Class ini untuk selanjutnya dilakukan perhitungan enkripsi / dekripsi yang sebenarnya. Deklarasi Class CoreAlgoritmaSymmetric adalah sebagai berikut:
Public Class CoreAlgoritmaSymmetric Private metodeEncode As System.Security.Cryptography.SymmetricAlgorithm Public Sub New() metodeEncode = New BlowfishAlgoritma() metodeEncode.Mode = System.Security.Cryptography.CipherMode.CBC metodeEncode.KeySize = 40 metodeEncode.GenerateKey() metodeEncode.GenerateIV() End Sub . . . End Class
* Pada saat menginisialisasi variabel metodeEncode, maka proses tersebut akan melakukan inisialisasi pada Class BlowfishAlgoritma. Class ini berisi tentang beberapa properti dan fungsi yang menggantikan properti dan fungsi yang diturunkan dari Class SymmetricAlgorithm dan ICryptoTransform. Deklarasi Class BlowfishAlgoritma adalah sebagai berikut:
Public Class BlowfishAlgoritma Inherits System.Security.Cryptography.SymmetricAlgorithm Implements System.Security.Cryptography.ICryptoTransform Private m_bf As Blowfish Private m_bfcbc As BlowfishBaseCBC Private m_blIsEncryptor As Boolean Private m_rng As System.Security.Cryptography.RNGCryptoServiceProvider Public Sub New() MyBase.New() m_bf = Nothing m_bfcbc = Nothing IVValue = Nothing KeyValue = Nothing KeySizeValue = Blowfish.MaksPanjangKey * 8 LegalBlockSizesValue = New System.Security.Cryptography.KeySizes(0) {} LegalBlockSizesValue(0) = New System.Security.Cryptography.KeySizes(BlockSize, BlockSize, 8) LegalKeySizesValue = New System.Security.Cryptography.KeySizes(0) {} LegalKeySizesValue(0) = New System.Security.Cryptography.KeySizes(0, Blowfish.MaksPanjangKey * 8, 8) ModeValue = System.Security.Cryptography.CipherMode.ECB m_rng = Nothing End Sub . . . 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 = xc.Encrypt(input)
* Skrip tersebut akan menjalankan proses enkripsi yang terdapat dalam Class CoreAlgoritmaSymmetric.
Public Function ProsesEncrypt(Source As String, Key As String) As String If Source Is Nothing OrElse Key Is Nothing OrElse Source.Length = 0 OrElse Key.Length = 0 Then Return Nothing End If If metodeEncode Is Nothing Then Return Nothing End If Dim lPanjangStream As Long Dim jumlahBufferTerbaca As Integer Dim byteBuffer As Byte() = New Byte(2) {} Dim srcData As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(Source) Dim encData As Byte() Dim streamInput As New System.IO.MemoryStream() streamInput.Write(srcData, 0, srcData.Length) streamInput.Position = 0 Dim streamOutput As New System.IO.MemoryStream() Dim streamEncrypt As System.Security.Cryptography.CryptoStream metodeEncode.Key = GetValidKey(Key) metodeEncode.IV = GetValidIV(Key, metodeEncode.IV.Length) streamEncrypt = New System.Security.Cryptography.CryptoStream(streamOutput, metodeEncode.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write) lPanjangStream = streamInput.Length Dim totalBufferTerbaca As Integer = 0 While totalBufferTerbaca < lPanjangStream jumlahBufferTerbaca = streamInput.Read(byteBuffer, 0, byteBuffer.Length) streamEncrypt.Write(byteBuffer, 0, jumlahBufferTerbaca) totalBufferTerbaca += jumlahBufferTerbaca End While streamEncrypt.Close() encData = streamOutput.ToArray() 'Konversi menjadi base64 agar hasil dapat digunakan dalam xml Return Convert.ToBase64String(encData) End Function
* Sedangkan proses enkripsi utama algoritma Blowfish adalah sebagai berikut
Protected Sub BaseEncrypt(ByRef unHiRef As UInteger, ByRef unLoRef As UInteger) Dim unHi As UInteger = unHiRef Dim unLo As UInteger = unLoRef Dim sbox1 As UInteger() = m_sbox1 Dim sbox2 As UInteger() = m_sbox2 Dim sbox3 As UInteger() = m_sbox3 Dim sbox4 As UInteger() = m_sbox4 Dim pbox As UInteger() = m_pbox unHi = unHi Xor pbox(0) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(1) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(2) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(3) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(4) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(5) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(6) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(7) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(8) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(9) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(10) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(11) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(12) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(13) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(14) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(15) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(16) unLoRef = unHi unHiRef = unLo Xor pbox(17) 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 = xc.Decrypt(hasilEnkripsi)
* Skrip tersebut akan menjalankan proses dekripsi yang terdapat dalam Class CoreAlgoritmaSymmetric.
Public Function ProsesDecrypt(Source As String, Key As String) As String If Source Is Nothing OrElse Key Is Nothing OrElse Source.Length = 0 OrElse Key.Length = 0 Then Return Nothing End If If metodeEncode Is Nothing Then Return Nothing End If Dim lPanjangStream As Long Dim jumlahBufferTerbaca As Integer Dim byteBuffer As Byte() = New Byte(2) {} Dim encData As Byte() = Convert.FromBase64String(Source) Dim decData As Byte() Dim streamInput As New System.IO.MemoryStream(encData) Dim streamOutput As New System.IO.MemoryStream() Dim streamDecrypt As System.Security.Cryptography.CryptoStream metodeEncode.Key = GetValidKey(Key) metodeEncode.IV = GetValidIV(Key, metodeEncode.IV.Length) streamDecrypt = New System.Security.Cryptography.CryptoStream(streamInput, metodeEncode.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read) lPanjangStream = streamInput.Length Dim totalBufferTerbaca As Integer = 0 While totalBufferTerbaca < lPanjangStream jumlahBufferTerbaca = streamDecrypt.Read(byteBuffer, 0, byteBuffer.Length) If 0 = jumlahBufferTerbaca Then Exit While End If streamOutput.Write(byteBuffer, 0, jumlahBufferTerbaca) totalBufferTerbaca += jumlahBufferTerbaca End While streamDecrypt.Close() decData = streamOutput.ToArray() For i As Integer = 0 To decData.Length - 1 If decData(i) < 8 Then decData(i) = 0 Next Dim encodeASCII As New System.Text.ASCIIEncoding() Return encodeASCII.GetString(decData) End Function
* Sedangkan proses dekripsi utama algoritma Blowfish adalah sebagai berikut
Public Overridable Sub Decrypt(ByRef unHiRef As UInteger, ByRef unLoRef As UInteger) Dim unHi As UInteger = unHiRef Dim unLo As UInteger = unLoRef Dim sbox1 As UInteger() = m_sbox1 Dim sbox2 As UInteger() = m_sbox2 Dim sbox3 As UInteger() = m_sbox3 Dim sbox4 As UInteger() = m_sbox4 Dim pbox As UInteger() = m_pbox unHi = unHi Xor pbox(CInt(17)) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(16) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(15) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(14) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(13) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(12) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(11) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(10) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(9) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(8) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(7) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(6) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(5) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(4) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(3) unLo = unLo Xor (((sbox1(CInt(unHi >> 24)) + sbox2(CInt((unHi >> 16) And &HFF))) Xor sbox3(CInt((unHi >> 8) And &HFF))) + sbox4(CInt(unHi And &HFF))) Xor pbox(2) unHi = unHi Xor (((sbox1(CInt(unLo >> 24)) + sbox2(CInt((unLo >> 16) And &HFF))) Xor sbox3(CInt((unLo >> 8) And &HFF))) + sbox4(CInt(unLo And &HFF))) Xor pbox(1) unLoRef = unHi unHiRef = unLo Xor pbox(0) End Sub
* Secara keseluruhan, skrip ini membutuhkan 2 Class tambahan yaitu Class Blowfish dan Class BlowfishBaseCBC. Class Blowfish berisi tentang ukuran blok, panjang key, semua nilai PBOX dan SBOX, dan beberapa fungsi enkripsi dan dekripsi dengan metode Blowfish. Class BlowfishBaseCBC adalah inti class Blowfish yang menggunakan teknik cipher CBC (Cipher Block Chaining), dan berisi tentang beberapa properti dan fungsi yang menggantikan properti dan fungsi yang diturunkan dari Class Blowfish dan SymmetricAlgorithm. Deklarasi kedua Class tersebut adalah sebagai berikut:
Public Class Blowfish 'Panjang maksimal ukuran key dalam byte Public Const MaksPanjangKey As Integer = 56 'Panjang ukuran blok dalam byte 'Perlu diingat bahwa ukuran data harus menyesuaikan ukuran blok Public Const ukuranBlok As Integer = 8 Const jumlahInputPBOX As Integer = 18 Const jumlahInputSBOX As Integer = 256 Private m_pbox As UInteger() Private m_sbox1 As UInteger() Private m_sbox2 As UInteger() Private m_sbox3 As UInteger() Private m_sbox4 As UInteger() . . . Public Sub New(key As Byte()) Dim nI As Integer m_pbox = New UInteger(jumlahInputPBOX - 1) {} For nI = 0 To jumlahInputPBOX - 1 m_pbox(nI) = InitPBOX(nI) Next m_sbox1 = New UInteger(jumlahInputSBOX - 1) {} m_sbox2 = New UInteger(jumlahInputSBOX - 1) {} m_sbox3 = New UInteger(jumlahInputSBOX - 1) {} m_sbox4 = New UInteger(jumlahInputSBOX - 1) {} For nI = 0 To jumlahInputSBOX - 1 m_sbox1(nI) = InitSBOX1(nI) m_sbox2(nI) = InitSBOX2(nI) m_sbox3(nI) = InitSBOX3(nI) m_sbox4(nI) = InitSBOX4(nI) Next 'Lakukan operasi XOR key pada semua pbox Dim panjangKey As Integer = key.Length If panjangKey = 0 Then Return Dim posisiKey As Integer = 0 Dim unBuild As UInteger = 0 For nI = 0 To jumlahInputPBOX - 1 For nJ As Integer = 0 To 3 unBuild = (unBuild << 8) Or key(posisiKey) If System.Threading.Interlocked.Increment(posisiKey) = panjangKey Then posisiKey = 0 End If Next m_pbox(nI) = m_pbox(nI) Xor unBuild Next 'Lakukan enkripsi pada semua box dengan semua string 0 Dim unZeroHi As UInteger = 0 Dim unZeroLo As UInteger = 0 For nI = 0 To jumlahInputPBOX - 1 Step 2 BaseEncrypt(unZeroHi, unZeroLo) m_pbox(nI) = unZeroHi m_pbox(nI + 1) = unZeroLo Next For nI = 0 To jumlahInputSBOX - 1 Step 2 BaseEncrypt(unZeroHi, unZeroLo) m_sbox1(nI) = unZeroHi m_sbox1(nI + 1) = unZeroLo Next For nI = 0 To jumlahInputSBOX - 1 Step 2 BaseEncrypt(unZeroHi, unZeroLo) m_sbox2(nI) = unZeroHi m_sbox2(nI + 1) = unZeroLo Next For nI = 0 To jumlahInputSBOX - 1 Step 2 BaseEncrypt(unZeroHi, unZeroLo) m_sbox3(nI) = unZeroHi m_sbox3(nI + 1) = unZeroLo Next For nI = 0 To jumlahInputSBOX - 1 Step 2 BaseEncrypt(unZeroHi, unZeroLo) m_sbox4(nI) = unZeroHi m_sbox4(nI + 1) = unZeroLo Next End Sub . . . End Class Public Class BlowfishBaseCBC Inherits Blowfish Private m_unIvHi As UInteger Private m_unIvLo As UInteger Public Sub New(key As Byte(), iv As Byte()) MyBase.New(key) Me.Iv = iv End Sub . . .
Hasil akhir adalah: (klik untuk perbesar gambar)
Contoh modul / source code dalam bahasa VB (Visual Basic) dapat didownload disini:
[sdm_download id="2851" fancy="0"]
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.
Leave a Reply