2012-06-15 16 views
5

Hola He estado tratando de utilizar System.Security.Cryptography para cifrar y descifrar un archivo, pero no su trabajo para míCriptografía en VB.net - archivo descifrado es más grande que el archivo fuente?

este código

Private Sub EncryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String) 
    Dim fsInput As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read) 
    Dim fsEncrypted As New FileStream(sOutputFilename, FileMode.Create, FileAccess.Write) 
    Dim DES As New DESCryptoServiceProvider() 
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey) 
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey) 
    Dim desencrypt As ICryptoTransform = DES.CreateEncryptor() 
    Dim cryptostream As New CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write) 
    Dim bytearrayinput(fsInput.Length - 1) As Byte 
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length) 
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length) 
    cryptostream.Close() 
End Sub 

llamada con

EncryptFile(OpenFileDialog1.FileName, SaveFileDialog1.FileName, "12345678")[/CODE] 

parece funcionar está bien y obtengo un archivo del mismo tamaño que el archivo fuente

Aquí es donde va mal

este código

Private Sub DecryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String) 
    Dim DES As New DESCryptoServiceProvider() 
    DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey) 
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey) 
    Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read) 
    Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor() 
    Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read) 
    Dim fsDecrypted As New StreamWriter(sOutputFilename) 
    fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd) 
    fsDecrypted.Flush() 
    fsDecrypted.Close() 
End Sub 

llamada con

DecryptFile(OpenFileDialog1.FileName, SaveFileDialog1.FileName, "12345678")[/CODE] 

da salida a un archivo que es casi 2 veces más grande que el archivo de origen que se ha cifrado.

qué está pasando estoy seguro de que esto estaba funcionando bien hace unas semanas y no puedo ver nada obviamente mal con él.

any ideas please?

+0

El uso de StreamReader/Writer no es apropiado, no lo usó al leer el archivo original. –

Respuesta

3

El problema principal es que EncryptFile lee en los datos usando una matriz de bytes y DecryptFile está leyendo en los datos usando secuencias. La única diferencia entre los métodos EncryptFile y DecryptFile debería ser su asignación ICryptoTransform. Sería más fácil tener el código común en 1 Procedimiento:

Private Sub EncryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String) 
    Crypto(sInputFilename, sOutputFilename, sKey, True) 
End Sub 

Private Sub DecryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String) 
    Crypto(sInputFilename, sOutputFilename, sKey, False) 
End Sub 

Private Sub Crypto(ByVal sInputFileName As String, ByVal sOutputFileName As String, ByVal sKey As String, ByVal bEncrypt As Boolean) 
    'Define the service provider 
    Dim DES As New DESCryptoServiceProvider() 
    DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey) 
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey) 


    'Read the input file into array 
    Dim fsInput As New FileStream(sInputFileName, FileMode.Open, FileAccess.Read) 
    Dim bytearrayinput(fsInput.Length - 1) As Byte 
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length) 


    'Define the crypto transformer 
    Dim cryptoTransform As ICryptoTransform 

    If bEncrypt Then 
     cryptoTransform = DES.CreateEncryptor() 
    Else 
     cryptoTransform = DES.CreateDecryptor 
    End If 


    'Create the encrypting streams 
    Dim fsEncrypted As New FileStream(sOutputFileName, FileMode.Create, FileAccess.Write) 
    Dim cryptostream As New CryptoStream(fsEncrypted, cryptoTransform, CryptoStreamMode.Write) 

    'Write the output file 
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length) 
    cryptostream.Close() 
End Sub 

procedimiento El Crypto es casi idéntico a lo que solía ser EncryptFile. La diferencia es que cambio la asignación ICryptoTransform en función de si está encriptando o descifrando.

+1

gracias por la explicación. trabajando bien ahora gracias por tu sub :) – user1459286

Cuestiones relacionadas