Fungsi Untuk Enkripsi dan Decripsi di VB.NET

Image

Berikut ini adalah fungsi untuk Encrypt dan Decrypt teks / string di VB.NET, semoga bisa bermanfaat untuk teman-teman programmer.

Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Public Class FrmEncrypt
Dim password As String = “ZE161207”
Dim salt() As Byte = {&H0, &H1, &H2, &H3, &H4, &H5, &H6, &HF1, &HF0, &HEE, &H21, &H22, &H45}

Public Function EncryptString(ByVal str As String) As String
Dim k1 As New Rfc2898DeriveBytes(password, salt, 1000)
Dim encAlg As TripleDES = TripleDES.Create()
encAlg.Key = k1.GetBytes(16)

Dim encryptionStream As New MemoryStream()
Dim encrypt As New CryptoStream(encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write)
Dim utfD1 As Byte() = New System.Text.UTF8Encoding(False).GetBytes(str)
encrypt.Write(utfD1, 0, utfD1.Length)
encrypt.FlushFinalBlock()
encrypt.Close()
Dim edata1 As Byte() = encryptionStream.ToArray()
k1.Reset()

Return Convert.ToBase64String(edata1) & Convert.ToBase64String(encAlg.IV) ‘Encoding.ASCII.GetString(encAlg.IV)
End Function

Public Function DecryptString(ByVal str As String) As String
Dim k2 As New Rfc2898DeriveBytes(password, salt)

Dim strIV As String = str.Substring(str.Length – 12, 12)
str = str.Substring(0, str.Length – 12)

Dim decAlg As TripleDES = TripleDES.Create()
decAlg.Key = k2.GetBytes(16)
decAlg.IV = Convert.FromBase64String(strIV)
Dim decryptionStreamBacking As New MemoryStream()
Dim decrypt As New CryptoStream(decryptionStreamBacking, decAlg.CreateDecryptor(), CryptoStreamMode.Write)
decrypt.Write(Convert.FromBase64String(str), 0, Convert.FromBase64String(str).Length)
decrypt.Flush()
decrypt.Close()
k2.Reset()
Return New UTF8Encoding(False).GetString(decryptionStreamBacking.ToArray())
End Function

Private Sub BtnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEncrypt.Click
TxtHasil.Text = EncryptString(TxtTeks.Text)
End Sub

Private Sub BtnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDecrypt.Click
TxtHasil.Text = DecryptString(TxtTeks.Text)
End Sub
End Class

Selamat mencoba..

3 Responses to “Fungsi Untuk Enkripsi dan Decripsi di VB.NET”

  1. soni Says:

    ellor gan…

    • Admin Says:

      error-nya gimana gan??

  2. riki hidayat Says:

    kalo programnya pake metode algoritma, untuk coding metodenya di tempatin di mana yah??


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: