Source code for Cryptography in VB.NET

Emc2

JF-Expert Member
Joined
Sep 26, 2011
Posts
16,808
Reaction score
16,953
REM: Wana JF I created a class in vb.net for file Encryption and decryption you can comment.


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

Friend Class Security
Private dtTable As New DataTable("Security")
Private ImageTable As New DataTable("Security")
Private TEMPLOC As String
Private Const Key As String = "KL)(!09#"
Public Sub New()
dtTable.Columns.Add("Key", GetType(Guid))
dtTable.Columns.Add("Date", GetType(Date))
dtTable.Columns.Add("Data", GetType(Byte()))
ImageTable.Columns.Add("Key", GetType(Guid))
TEMPLOC = IIf(My.Computer.FileSystem.SpecialDirectories.Temp.EndsWith("\"), My.Computer.FileSystem.SpecialDirectories.Temp, My.Computer.FileSystem.SpecialDirectories.Temp & "\")

End Sub
Public Function Encrypt(ByVal Location As String, ByVal seclocation As String) As String
Try
Dim gui As Guid = Guid.NewGuid
Dim DataFile As String = Guid.NewGuid.ToString
Dim SecurityFile As String = Guid.NewGuid.ToString
Dim by() As Byte = IO.File.ReadAllBytes(Location)
dtTable.Rows.Add(gui, Today, by)
ImageTable.Rows.Add(gui)
dtTable.WriteXml(TEMPLOC & DataFile)
ImageTable.WriteXml(TEMPLOC & SecurityFile)
Dim DataBy() As Byte = IO.File.ReadAllBytes(TEMPLOC & DataFile)
Dim SecBy() As Byte = IO.File.ReadAllBytes(TEMPLOC & SecurityFile)
My.Computer.FileSystem.DeleteFile(TEMPLOC & DataFile)
My.Computer.FileSystem.DeleteFile(TEMPLOC & SecurityFile)
Dim fileInfo As New IO.FileInfo(seclocation)
Dim D As String = IIf(fileInfo.DirectoryName.EndsWith("\"), fileInfo.DirectoryName, fileInfo.DirectoryName & "\")
Encrypt(SecBy, D & "Security.SEC")
Encrypt(DataBy, TEMPLOC & DataFile & ".Temp")
Return TEMPLOC & DataFile & ".Temp"
Catch ex As Exception
Throw ex
End Try

Return ""
End Function
Private Sub Encrypt(ByVal Data As Byte(), ByVal outFile As String)
Try
Dim DESalg As New System.Security.Cryptography.DESCryptoServiceProvider

Dim objEncod As Encoding = Encoding.ASCII
DESalg.Key = objEncod.GetBytes(Key)
DESalg.IV = objEncod.GetBytes("11110000")
Dim outStream As New FileStream(outFile, FileMode.Create, FileAccess.Write)

Dim CryFile As New CryptoStream(outStream, DESalg.CreateEncryptor(DESalg.Key, DESalg.IV), CryptoStreamMode.Write)
CryFile.Write(Data, 0, Data.Length)

CryFile.Close()
outStream.Close()


Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
Public Function Decrypt(ByVal Location As String) As String
Try
Dim fileInfo As New IO.FileInfo(Location)
Dim Dir As String = IIf(fileInfo.DirectoryName.EndsWith("\"), fileInfo.DirectoryName, fileInfo.DirectoryName & "\") & "Security.SEC"
If Not My.Computer.FileSystem.FileExists(Dir) Then
Throw New Exception("Security file does not exists")
End If
Decrypt(Dir, Me.ImageTable)
If ImageTable.Rows.Count > 0 Then
If ImageTable.Rows.Count <= 3 Then
Decrypt(Location, Me.dtTable)

If dtTable.Rows(0)("Key") <> ImageTable.Rows(0)("Key") Then

Throw New Exception("Wrong Security file")
End If

Dim gui As Guid = dtTable.Rows(0)("Key")
ImageTable.Rows.Add(gui)
Dim data() As Byte = dtTable.Rows(0)("Data")
Dim loc As String = TEMPLOC & Guid.NewGuid.ToString & ".Temp"
IO.File.WriteAllBytes(loc, data)
My.Computer.FileSystem.DeleteFile(Dir)
Dim SecurityFile As String = Guid.NewGuid.ToString
ImageTable.WriteXml(TEMPLOC & SecurityFile)
Dim SecBy() As Byte = IO.File.ReadAllBytes(TEMPLOC & SecurityFile)
My.Computer.FileSystem.DeleteFile(TEMPLOC & SecurityFile)
Encrypt(SecBy, Dir)
Return loc
Else

Throw New Exception("You have exceeded the maximum decryption")
End If

Else
Throw New Exception("No Record in Security File")
End If
Catch ex As Exception
Throw ex
End Try
Return ""
End Function
Private Sub Decrypt(ByVal inFile As String, ByVal dt As DataTable)
Try
dtTable.Rows.Clear()
Dim DESalg As New DESCryptoServiceProvider
Dim fs As New FileStream(inFile, FileMode.Open)
Dim objEncod As Encoding = Encoding.ASCII
DESalg.IV = objEncod.GetBytes("11110000")
DESalg.Key = objEncod.GetBytes(Key)
Dim cryp As New CryptoStream(fs, DESalg.CreateDecryptor(DESalg.Key, DESalg.IV), CryptoStreamMode.Read)
dt.ReadXml(cryp)


cryp.Close()
fs.Close()


Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub

End Class
 
Private Const Key As String = "KL)(!09#"

nieleweshe hiyo ndiyo encryption key? au ndiyo nini?
 
Just a key you can change it lazima ziwe character 8.
 

please tumia CODE tags (
Code:
) kuwrap ur code
 
Jamni ikiwasaidia hata kunilike siyo mbaya.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…