content top
What is my remote IP? Creating Java web-service for JBoss Deploy Java webservice on JBoss SSH/SFTP client in VB.net with sources

Encrypt and decrypt files in VB.net

As I’ve been working on a new project lately, I needed a way to encrypt/decrypt files quickly and easily in VB.net. For some reason I found tons of examples around the internet, but either you had to put them together yourself, or they were poorly coded or coded in a way that seemed like a random mess. Thus I put together this Class that makes it rather easy for you to encrypt/decrypt files. It is based on managed Rijndael and the Class is fairly easy to use.

FileEncryption.vb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'// Copyright (c) 2009 - Kjell Arne Brudvik
'// Email: kjell.arne@brudvik.org - WWW: http://www.brudvik.org/
'// This is released open-source, free to use for anyone whom wants.
 
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
 
Public Class FileEncryption
 
    Private password As String = ""
    Private encryptionKey() As Byte = Nothing
    Private encryptionIV() As Byte = Nothing
    Private inputFile As FileStream = Nothing
    Private outputFile As FileStream = Nothing
 
    Public Sub New(ByVal password As String)
        Me.password = password
        Me.encryptionKey = Me.generateKey
        Me.encryptionIV = Me.generateIV
    End Sub
 
    Public Sub encryptFile(ByVal input As String, ByVal output As String)
        Try
            inputFile = New FileStream(input, FileMode.Open, FileAccess.Read)
            outputFile = New FileStream(output, FileMode.OpenOrCreate, FileAccess.Write)
            outputFile.SetLength(0)
 
            Dim buffer(4096) As Byte
            Dim bytesProcessed As Long = 0
            Dim fileLength As Long = inputFile.Length
            Dim bytesInCurrentBlock As Integer
            Dim rijandael As New RijndaelManaged
            Dim cryptoStream As CryptoStream = New CryptoStream(outputFile, rijandael.CreateEncryptor(encryptionKey, encryptionIV), CryptoStreamMode.Write)
 
            While bytesProcessed < fileLength
                bytesInCurrentBlock = inputFile.Read(buffer, 0, 4096)
                cryptoStream.Write(buffer, 0, bytesInCurrentBlock)
                bytesProcessed = bytesProcessed + CLng(bytesInCurrentBlock)
            End While
 
            cryptoStream.Close()
            inputFile.Close()
            outputFile.Close()
 
            File.Delete(input)
        Catch ex As Exception
 
        End Try
    End Sub
 
    Public Sub decryptFile(ByVal input As String, ByVal output As String)
        Try
            inputFile = New FileStream(input, FileMode.Open, FileAccess.Read)
            outputFile = New FileStream(output, FileMode.OpenOrCreate, FileAccess.Write)
            outputFile.SetLength(0)
 
            Dim buffer(4096) As Byte
            Dim bytesProcessed As Long = 0
            Dim fileLength As Long = inputFile.Length
            Dim bytesInCurrentBlock As Integer
            Dim rijandael As New RijndaelManaged
            Dim cryptoStream As CryptoStream = New CryptoStream(outputFile, rijandael.CreateDecryptor(encryptionKey, encryptionIV), CryptoStreamMode.Write)
 
            While bytesProcessed < fileLength
                bytesInCurrentBlock = inputFile.Read(buffer, 0, 4096)
                cryptoStream.Write(buffer, 0, bytesInCurrentBlock)
                bytesProcessed = bytesProcessed + CLng(bytesInCurrentBlock)
            End While
 
            cryptoStream.Close()
            inputFile.Close()
            outputFile.Close()
 
            File.Delete(input)
        Catch ex As Exception
 
        End Try
    End Sub
 
    Private Function generateKey() As Byte()
        Try
            Dim data() As Char = Me.password.ToCharArray
            Dim length As Integer = data.GetUpperBound(0)
            Dim hash(length) As Byte
            Dim i As Long = 0
            Dim sha512 As New SHA512Managed
            For i = 0 To data.GetUpperBound(0)
                hash(i) = CByte(Asc(data(i)))
            Next
            Dim result As Byte() = sha512.ComputeHash(hash)
            Dim key(31) As Byte
            For i = 0 To 31 Step 1
                key(i) = result(i)
            Next
            Return key
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
 
    Private Function generateIV() As Byte()
        Try
            Dim data() As Char = Me.password.ToCharArray
            Dim length As Integer = data.GetUpperBound(0)
            Dim hash(length) As Byte
            Dim i As Long = 0
            Dim sha512 As New SHA512Managed
            For i = 0 To data.GetUpperBound(0)
                hash(i) = CByte(Asc(data(i)))
            Next
            Dim result As Byte() = sha512.ComputeHash(hash)
            Dim iv(15) As Byte
            For i = 32 To 47
                iv(i - 32) = result(i)
            Next
            Return iv
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
 
End Class

How to use this?

Below is a small example on how you can use this class. When creating a new instance of the class you then supply the key you want to use. Then you can quickly encrypt/decrypt files. The input file in the encryptFile method is the file you want to encrypt, while the output is the encrypted file you generate. And for decrypting it is obviously the other way around.

1
2
3
Dim encryption as FileEncryption = New FileEncryption("YOUR KEY GOES HERE")
encryption.encryptFile("input file path", "output file path")
encryption.decryptFile("input file path", "output file path")
Skrevet av Kjell Arne Brudvik den 23.03.2009 liker denne artikkelen.
content top