Use DES, RC2, Rijndael, TripleDES to decrypt and Encrypt files : Cypher Decypher File « File Directory « VB.Net

VB.Net
1. 2D
2. Application
3. Class
4. Data Structure
5. Database ADO.net
6. Development
7. Event
8. File Directory
9. Generics
10. GUI
11. Language Basics
12. Network Remote
13. Thread
14. Windows System
15. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
VB.Net » File Directory » Cypher Decypher FileScreenshots 
Use DES, RC2, Rijndael, TripleDES to decrypt and Encrypt files
Use DES, RC2, Rijndael, TripleDES to decrypt and Encrypt files

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

Public Class SymEnc
    Shared Dim algorithms() As String = {"DES""RC2""Rijndael""TripleDES"}
    Shared Dim b64Keys() As String = {"YE32PGCJ/g0=","vct+rJ09WuUcR61yfxniTQ==","PHDPqfwE3z25f2UYjwwfwg4XSqxvl8WYmy+2h8t6AUg=","Q1/lWoraddTH3IXAQUJGDSYDQcYYuOpm"}
    Shared Dim b64IVs() As String = {"onQX8hdHeWQ=","jgetiyz+pIc=","pd5mgMMfDI2Gxm/SKl5I8A==","6jpFrUh8FF4="}

    Public Shared Sub Main(ByVal CmdArgs() As String)
        If (CmdArgs.Length <> 4Then
            UsageAndExit()
        End If

        Dim algorithmsIndex As Integer = CmdArgs(0)

        If (algorithmsIndex < Or algorithmsIndex >= algorithms.LengthThen
            UsageAndExit()
        End If
        
        
        Dim inputFile As FileStream = File.OpenRead(CmdArgs(2))
        Dim outputFile As FileStream = File.OpenWrite(CmdArgs(3))
        Dim sa As SymmetricAlgorithm = SymmetricAlgorithm.Create(algorithms(algorithmsIndex))
        
        sa.IV = Convert.FromBase64String(b64IVs(algorithmsIndex))
        sa.Key = Convert.FromBase64String(b64Keys(algorithmsIndex))

        If (CmdArgs(1).ToUpper().StartsWith("E")) Then
            Encrypt(sa, inputFile, outputFile)
        Else
            Decrypt(sa, inputFile, outputFile)
        End If
        
    End Sub

    Shared Public Sub Encrypt(ByVal sa As SymmetricAlgorithm,ByVal inputFile As Stream,ByVal outputFile As Stream)
        Dim trans As ICryptoTransform = sa.CreateEncryptor()
        Dim buf() As Byte = New Byte(2048) {}
        Dim cs As CryptoStream = _
        New CryptoStream(outputFile, trans, CryptoStreamMode.Write)
        Dim Len As Integer
        inputFile.Position = 0
        Len = inputFile.Read(buf, 0, buf.Length)
        While (Len > 0)
            cs.Write(buf, 0, Len)
            Len = inputFile.Read(buf, 0, buf.Length)
        End While
        cs.Close()
        inputFile.Close()
    End Sub

    Shared Public Sub Decrypt(ByVal sa As SymmetricAlgorithm,ByVal inputFile As Stream,ByVal outputFile As Stream)
        Dim trans As ICryptoTransform = sa.CreateDecryptor()
        Dim buf() As Byte = New Byte(2048) {}
        Dim cs As CryptoStream = _
        New CryptoStream(inputFile, trans, CryptoStreamMode.Read)
        Dim Len As Integer
        Len = cs.Read(buf, 0, buf.Length)
        While (Len > 0)
            outputFile.Write(buf, 0, Len)
            Len = cs.Read(buf, 0, buf.Length)
        End While
        inputFile.Close()
        outputFile.Close()
    End Sub

    Shared Public Sub UsageAndExit()
        Console.Write("usage SymEnc <algorithms index> <D|E> <in> <out> ")
        Console.WriteLine("D =decrypt, E=Encrypt")
        For i As Integer = To (algorithms.Length - 1)
            Console.WriteLine("Algo index: {0} {1}", i, algorithms(i))
        Next i
        End
    End Sub
End Class

           
       
Related examples in the same category
1. Decrypt a Des Encryted File
2. Use Des to cypher and decypher File Use Des to cypher and decypher File
w___w___w_.___jav_a_2___s__.c_o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.