Module Module1 Sub Main() Console.WriteLine("Algoritma DES (Data Encryption Standard)") '1. Tentukan kalimat yang akan dienkrip Console.WriteLine("Masukkan kalimat yang akan dienkrip: ") Dim input As String = Console.ReadLine Console.WriteLine("") '2. Tentukan kata kunci enkripsi yang digunakan Console.WriteLine("Masukkan kata kunci enkripsi: ") Dim kataKunci As String = Console.ReadLine Console.WriteLine("") '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 '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) Console.WriteLine("Hasil enkripsi kalimat input adalah: " & vbCrLf & hasilEnkripsi.ToString & vbCrLf) '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) Console.WriteLine("Hasil dekripsi dari kalimat terenkripsi adalah: " & vbCrLf & hasilDekripsi & vbCrLf) Console.ReadLine() End Sub End Module 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 Public Function Decrypt(src As String) As String Dim hasil As String = "" If _key Is Nothing Then hasil = _coreSymmetric.ProsesDecrypt(src, DefaultKey) Else hasil = _coreSymmetric.ProsesDecrypt(src, _key) End If Return hasil End Function Public Function Decrypt(src As String, key As String) As String Dim hasil As String = "" hasil = _coreSymmetric.ProsesDecrypt(src, key) Return hasil End Function Public Function Encrypt(src As String) As String Dim hasil As String = "" If _key Is Nothing Then hasil = _coreSymmetric.ProsesEncrypt(src, DefaultKey) Else hasil = _coreSymmetric.ProsesEncrypt(src, _key) End If Return hasil End Function Public Function Encrypt(src As String, key As String) As String Dim hasil As String = "" hasil = _coreSymmetric.ProsesEncrypt(src, key) Return hasil End Function Public Class CoreAlgoritmaSymmetric Private metodeEncode As System.Security.Cryptography.SymmetricAlgorithm Public Sub New() metodeEncode = New System.Security.Cryptography.DESCryptoServiceProvider() End Sub Private Function GetValidKey(Key As String) As Byte() Dim sTemp As String If metodeEncode.LegalKeySizes.Length > 0 Then Dim lessSize As Integer = 0, moreSize As Integer = metodeEncode.LegalKeySizes(0).MinSize While Key.Length * 8 > moreSize AndAlso metodeEncode.LegalKeySizes(0).SkipSize > 0 AndAlso moreSize < metodeEncode.LegalKeySizes(0).MaxSize lessSize = moreSize moreSize += metodeEncode.LegalKeySizes(0).SkipSize End While If Key.Length * 8 > moreSize Then sTemp = Key.Substring(0, (moreSize / 8)) Else sTemp = Key.PadRight(moreSize / 8, " "c) End If Else sTemp = Key End If 'Konversi kata kunci menjadi byte array Return System.Text.ASCIIEncoding.ASCII.GetBytes(sTemp) End Function Private Function GetValidIV(InitVector As [String], panjangValid As Integer) As Byte() If InitVector.Length > panjangValid Then Return System.Text.ASCIIEncoding.ASCII.GetBytes(InitVector.Substring(0, panjangValid)) Else Return System.Text.ASCIIEncoding.ASCII.GetBytes(InitVector.PadRight(panjangValid, " "c)) End If End Function 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 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 End Class End Class
Write, Run & Share VB.net code online using OneCompiler's VB.net online compiler for free. It's one of the robust, feature-rich online compilers for VB.net language, running on the latest version 16. Getting started with the OneCompiler's VB.net compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as VB.net
. OneCompiler also has reference programs, where you can look for the sample code to get started with.
OneCompiler's VB.net online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample VB.net program which takes name as input and prints hello message with your name.
Public Module Program
Public Sub Main(args() As string)
Dim name as String = Console.ReadLine() ' Reading input from STDIN
Console.WriteLine("Hello " & name) ' Writing output to STDOUT
End Sub
End Module
Visual Basic is a event driven programming language by Microsoft, first released in the year 1991.
Variable is a name given to the storage area in order to identify them in our programs.
Simple syntax of Variable declaration is as follows
Dim variableName [ As [ New ] dataType ] [ = initializer ]
variableName = value
If condition-expression Then
'code
End If
If(conditional-expression)Then
'code if the conditional-expression is true
Else
'code if the conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
Else If(conditional-expression) Then
'code if the above conditional-expression is true
Else
'code if the above conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
If(conditional-expression)Then
'code if the above conditional-expression is true
End If
End If
Select [ Case ] expression
[ Case expressionlist
'code ]
[ Case Else
'code ]
End Select
For counter [ As datatype ] = begin To end [ Step step ]
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ counter ]
For Each element [ As datatype ] In group
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ element ]
While conditional-expression
'Code
[ Continue While ]
'Code
[ Exit While ]
'Code
End While
Do { While | Until } conditional-expression
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop
Do
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop { While | Until } conditional-expression
Procedure is a sub-routine which contains set of statements. Usually Procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Procedures are of two types.
Functions return a value when they are called.
[accessModifiers] Function functionName [(parameterList)] As returnType
'code
End Function
Sub-procedures are similar to functions but they don't return any value.
Sub ProcedureName (parameterList)
'Code
End Sub