Hi

Well... I am trying to code a module with two methods (one do encrypt, another
to decrypt) using the namespace system.securty.cryptography classes. I can
encrypt, but when i decrypt, the destination file misses 1.. like.. Source
file is 17 kb, destination final is 16,9. I have tried, and tried many things
like add numbers on the loop etc etc... I´ll apreciate that anyone check
my code and say what i am doing wrong, because i can´t see it anymore today!!
By the way, i use DES encryption, with two properties, one to set the key
and another to the Vector. THe main(entry) of the module will prompt for
the parameters and then encrypt/decrypt the files, while you see it on the
HD...
Thanks, jonas

***********************************************************
Code: (in portuguese, but you will understand.. i hope
***********************************************************


Imports System.IO 'para manipular ficheiros

'namespaces da segurança e criptografia
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text 'para manipulação de texto

Module enc

Private m_Chave As String 'variável interna para chave
Private m_Vector As String 'variável interna para vector

'proprieade CHAVE somente de escrita
Friend WriteOnly Property Chave() As String
Set(ByVal Value As String)
m_Chave = Value
End Set
End Property

'propriedade VECTOR somente de escrita
Friend WriteOnly Property Vector() As String
Set(ByVal Value As String)
m_Vector = Value
End Set
End Property

Friend Sub EncFicheiro(ByVal Fonte As String, ByVal Destino As String)

'criar/abrir os ficheiros passados nos parâmetros
Dim stFonte As New FileStream(Fonte, FileMode.Open, FileAccess.Read)
Dim stDestino As New FileStream(Destino, FileMode.OpenOrCreate,
FileAccess.Write)

stDestino.SetLength(0)

Dim b(4096) As Byte 'chunck de leitura será 4096

Dim TotalBytesEsc As Long 'total de bytes lidos
Dim TotalBytes As Long = stFonte.Length 'total de bytes da stream
do ficheiro

Dim Tamanho As Integer

'criar novo serviço de criptografia DES!
Dim DES As New DESCryptoServiceProvider()

'definir chave e vector. Usamos System.Text para obter os bytes
da string!
DES.Key = ASCIIEncoding.ASCII.GetBytes(m_Chave)
DES.IV = ASCIIEncoding.ASCII.GetBytes(m_Vector)

'criar encriptador usando DES
Dim DesEnc As ICryptoTransform = DES.CreateEncryptor

'criar stream de encriptação. Usamos o ficheiro destino, o encriptador
e modo de escrita
Dim streamEnc As New CryptoStream(stDestino, DesEnc, CryptoStreamMode.Write)

'enquanto os bytes escritos forem inferiores ao total
Do Until stFonte.Position = stFonte.Length
Tamanho = stFonte.Read(b, 0, 4096) 'ler fonte

streamEnc.Write(b, 0, Tamanho) 'escrever na stream

'definir total para o loop
TotalBytesEsc = (TotalBytesEsc + ((Tamanho / DES.BlockSize) *
DES.BlockSize))
MsgBox(TotalBytesEsc)
Loop

'fechar e destruir objectos
stDestino.Close() : DES = Nothing
DesEnc = Nothing : stFonte.Close()
End Sub

Friend Sub DecFicheiro(ByVal Fonte As String, ByVal Destino As String)

Dim stFonte As New FileStream(Fonte, FileMode.Open, FileAccess.Read)
Dim stDestino As New FileStream(Destino, FileMode.OpenOrCreate, FileAccess.Write)

stDestino.SetLength(0)

Dim b(4096) As Byte
Dim TotalBytesEsc As Long
Dim TotalBytes As Long = stFonte.Length

Dim Tamanho As Integer
Dim DES As New DESCryptoServiceProvider()

DES.Key = ASCIIEncoding.ASCII.GetBytes(m_Chave)
DES.IV = ASCIIEncoding.ASCII.GetBytes(m_Vector)

'criar decriptador!!!
Dim DesEnc As ICryptoTransform = DES.CreateDecryptor

'usar o decriptador na CryptoStream
Dim streamEnc As New CryptoStream(stDestino, DesEnc, CryptoStreamMode.Write)

stFonte.Seek(0, SeekOrigin.Begin)
stDestino.Seek(0, SeekOrigin.Begin)
Do Until stFonte.Position = stFonte.Length
Tamanho = stFonte.Read(b, 0, 4096)

streamEnc.Write(b, 0, Tamanho)
TotalBytesEsc = (TotalBytesEsc + ((Tamanho / DES.BlockSize) *
DES.BlockSize))
Loop






stDestino.Close() : DES = Nothing
DesEnc = Nothing : stFonte.Close()
End Sub

Sub Main()

Dim strChave, strvector As String
Dim strFonte, strDestino As String

Console.WriteLine("Insira chave:")
enc.Chave = Console.ReadLine

Console.WriteLine("Insira vector inicial:")
enc.Vector = Console.ReadLine

Console.WriteLine("Insira ficheiro a encriptar:")
strFonte = Console.ReadLine

Console.WriteLine("Gravar ficheiro como:")
strDestino = Console.ReadLine

enc.EncFicheiro(strFonte, strDestino)

Console.WriteLine("Encriptado com sucesso!")
Console.ReadLine()

enc.DecFicheiro(strDestino, strFonte)
Console.WriteLine("Ficheiro decriptado...")
Console.ReadLine()



End Sub


End Module



******************************************