Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Cryptography application

1.00/5 (2 votes)
12 May 2008CPOL 1   275  
Application for Encription and decription

DecryptoImage - maximum width is 600 pixels CryptoImage width=

Introduction

This is useful application for keeping data on safely place in your computer.

Background

Is there any background to this article that may be useful such as an introduction to the basic ideas presented?

Today is very popular spying and stealing.With time we understand necessity for keeping our data on special places and under safely encription.This is something about we must thinking..

Using the code

Step 1. What do you need to do :
1. In your WebSite directory add folder zastita
2. In folder zastita put Default.aspx page

VB.NET
Imports Microsoft.VisualBasic
Imports System.Security.Cryptography
Public Class zastita_Default
    Inherits System.Web.UI.Page
    Dim filepart As String

    Sub Crypt()
        Dim textinput As String
        Dim password As String
        password = Lozinka.Text()
        textinput = Ciuso.Text()
        filepart = TextBox1.Text
        Dim empire As New crypto(password)
        Dim cryptoText As String = empire.EncryptData(textinput)

        Ciuso.Text = ("The crypto text is: " &_
        cryptoText)
        My.Computer.FileSystem.WriteAllText( _
            My.Computer.FileSystem.SpecialDirectories.MyDocuments
            &filepart, cryptoText, False)
    End Sub
    
    Sub Decrypt()
        filepart = TextBox1.Text
        Dim cryptoText As String = _
        My.Computer.FileSystem.ReadAllText( _
        My.Computer.FileSystem.SpecialDirectories.MyDocuments _
        & filepart)


        Dim password As String
        password = Lozinka.Text()
        Dim empire As New crypto(password)

        Try
            Dim textinput As String = _
            empire.DecryptData(cryptoText)
            Ciuso.Text = (textinput)
        Catch ex As _
        System.Security.Cryptography.CryptographicException
            MsgBox("Please write correct password           ",_                    
            MsgBoxStyle.Information, "Information")

        End Try
    End Sub

    Protected Sub EncodeB_Click(ByVal sender As Object,_
    ByVal e As System.EventArgs) Handles EncodeB.Click
        Crypt()
    End Sub

    Protected Sub DecodeB_Click(ByVal sender As Object,_
    ByVal e As System.EventArgs) Handles DecodeB.Click
        Decrypt()
    End Sub   
 
End Class

Step 2. In App_Code folder put crypto.vb page

VB.NET
Language="VB"

Imports Microsoft.VisualBasic
Imports System.Security.Cryptography

Public NotInheritable Class crypto
    Inherits System.Web.UI.Page
    Private TripleDes As New _
    TripleDESCryptoServiceProvider
    Private Function TruncateHash( _
    ByVal key As String, _
    ByVal length As Integer) _
    As Byte()
        Dim sha1 As New SHA1CryptoServiceProvider()
        Dim keyBytes() As Byte = _
            System.Text.Encoding.Unicode.GetBytes(key)
        Dim hash() As Byte = sha1.ComputeHash(keyBytes)

        ReDim Preserve hash(length - 1)
        Return hash
    End Function
    Sub New(ByVal key As String)
        TripleDes.Key = TruncateHash(key,_
        TripleDes.KeySize \ 8)
        TripleDes.IV = TruncateHash("", _
        TripleDes.BlockSize \ 8)
    End Sub
    Public Function EncryptData( _
        ByVal textinput As String) _
        As String

        Dim textinputBytes() As Byte = _
            System.Text.Encoding.Unicode.GetBytes(textinput)
        Dim ms As New System.IO.MemoryStream
        Dim encStream As New CryptoStream(ms,_
        TripleDes.CreateEncryptor(), _
            System.Security.Cryptography.CryptoStreamMode.Write)

        encStream.Write(textinputBytes, 0,textinputBytes.Length)
        encStream.FlushFinalBlock()

        Return Convert.ToBase64String(ms.ToArray)
    End Function
    Public Function DecryptData(ByVal encryptedtext As_
    String) As String
        Dim encryptedBytes() As Byte = Convert.FromBase64String_
        (encryptedtext)
        Dim ms As New System.IO.MemoryStream
        Dim decStream As New CryptoStream_
        (ms,TripleDes.CreateDecryptor(), _
            System.Security.Cryptography.CryptoStreamMode.Write)

        decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
        decStream.FlushFinalBlock()

        Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
    End Function

End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)