Cifrador de archivos Curso de Programación Visual Basic

Lección:

Administración de Archivos


Ejercicio:

Cifrador de archivos 6


Objetivo:

Cree un programa para cifrar un archivo de texto en otro archivo de texto. Debe incluir la clase de cifrado que ha creado anteriormente (el 17 de enero)


Código:

Imports System.IO
Imports System
Class TextFileEncrypter
    Private Shared Sub Main(ByVal args As String())
        Dim inputFile As StreamReader
        Dim outputFile As StreamWriter
        Dim line As String
        Dim name As String

        If args.Length < 1 Then
            Console.WriteLine("Not enough parameters!")
            Console.WriteLine("Enter file name: ")
            name = Console.ReadLine()
        Else
            name = args(0)
        End If

        inputFile = File.OpenText(name)
        outputFile = File.CreateText(name & ".encrypted")

        Do
            line = inputFile.ReadLine()

            If line IsNot Nothing Then
                Dim newText As String = Encrypter.Encrypt(line)
                outputFile.WriteLine(newText)
            End If
        Loop While line IsNot Nothing

        inputFile.Close()
        outputFile.Close()
    End Sub
End Class

Class Encrypter
    Public Shared Function Encrypt(ByVal originalString As String) As String
        Dim result As String = ""

        For Each letter As Char In originalString
            Dim newLetter As Char = letter
            newLetter += 1
            result += newLetter
        Next

        Return result
    End Function

    Public Shared Function Decrypt(ByVal originalString As String) As String
        Dim result As String = ""

        For Each letter As Char In originalString
            Dim newLetter As Char = letter
            newLetter -= 1
            result += newLetter
        Next

        Return result
    End Function
End Class