Use DES, RC2, Rijndael, TripleDES to decrypt and Encrypt files : 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.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
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
3.Encrypting a file
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.