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 <> 4) Then
UsageAndExit()
End If
Dim algorithmsIndex As Integer = CmdArgs(0)
If (algorithmsIndex < 0 Or algorithmsIndex >= algorithms.Length) Then
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 = 0 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