Use Des to cypher and decypher File : Cypher Decypher File « File Directory « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » File Directory » Cypher Decypher FileScreenshots 
Use Des to cypher and decypher File
Use Des to cypher and decypher File
 
Imports System
Imports System.Windows.Forms
Imports System.Security
Imports System.Security.Cryptography
Imports System.IO

Public Class MainClass

   Shared Sub Main()
        Dim plainTextFileName As String = "test.txt"
        Dim cypherTextFileName As String = "cyphertext.dat"
        Dim decypheredFileName As String = "decyphered.txt"

        CryptFile("MyPassword", plainTextFileName, cypherTextFileName, True)

        Console.WriteLineFile.ReadAllText(plainTextFileName))
        Console.WriteLineFile.ReadAllText(cypherTextFileName))

        CryptFile("MyPassword", cypherTextFileName, decypheredFileName, False)

        Console.WriteLineFile.ReadAllText(decypheredFileName))

   End Sub 

    ' Encrypt or decrypt a file, saving the results 
    ' in another file.
    Shared Private Sub CryptFile(ByVal password As String, ByVal in_file As String, ByVal out_file As String, ByVal encrypt As Boolean)
        ' Create input and output file streams.
        Dim in_stream As New FileStream(in_file, FileMode.Open, FileAccess.Read)
        Dim out_stream As New FileStream(out_file, FileMode.Create, FileAccess.Write)

        Dim des_provider As New TripleDESCryptoServiceProvider()

        ' Find a valid key size for this provider.
        Dim key_size_bits As Integer = 0
        For i As Integer = 1024 To Step -1
            If des_provider.ValidKeySize(iThen
                key_size_bits = i
                Exit For
            End If
        Next i
        'Debug.Assert(key_size_bits > 0)

        Dim block_size_bits As Integer = des_provider.BlockSize

        Dim key As Byte() = Nothing
        Dim iv As Byte() = Nothing
        MakeKeyAndIV(password, key_size_bits, block_size_bits, key, iv)

        Dim crypto_transform As ICryptoTransform
        If encrypt Then
            crypto_transform = des_provider.CreateEncryptor(key, iv)
        Else
            crypto_transform = des_provider.CreateDecryptor(key, iv)
        End If

        Dim crypto_stream As New CryptoStream(out_stream, crypto_transform, CryptoStreamMode.Write)

        Const BLOCK_SIZE As Integer = 1024
        Dim buffer(BLOCK_SIZEAs Byte
        Dim bytes_read As Integer
        Do
            bytes_read = in_stream.Read(buffer, 0, BLOCK_SIZE)
            If bytes_read = Then Exit Do

            crypto_stream.Write(buffer, 0, bytes_read)
        Loop

        crypto_stream.Close()
        in_stream.Close()
        out_stream.Close()
    End Sub

    ' Use the password to generate key bytes.
    Shared Private Sub MakeKeyAndIV(ByVal password As String, ByVal key_size_bits As Integer, ByVal block_size_bits As Integer, ByRef key As Byte(), ByRef iv As Byte())
        Dim password_derive_bytes As New PasswordDeriveBytes_
            "MyPassword", Nothing, "SHA384"1000)

        key = password_derive_bytes.GetBytes(key_size_bits \ 8)
        iv = password_derive_bytes.GetBytes(block_size_bits \ 8)
    End Sub

End Class

           
         
  
Related examples in the same category
1.Use DES, RC2, Rijndael, TripleDES to decrypt and Encrypt filesUse DES, RC2, Rijndael, TripleDES to decrypt and Encrypt files
2.Decrypt a Des Encryted File
3.Encrypting a file
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.