Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / Visual Basic
Article

Cryptography application

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
12 May 2008CPOL 22.9K   275   16   1
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)


Written By
Web Developer
Croatia Croatia
Good evening !

I was born 18.10.1969.In Zagreb(Croatia)
My pleasure is in our cooperation.
Like a Photographer,Graphic designer,
Web designer and with smooth finish
in Programming Internet applications
is secret of my good spirit.
Find my interests in Designing and
programming in XML,SQL,VB,Action script,
Like nature,skiing,swimming,fishing,fightsports.


Kinds regards

Comments and Discussions

 
GeneralMy vote of 1 Pin
Dave Kreskowiak13-Jan-10 7:47
mveDave Kreskowiak13-Jan-10 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.