سؤال

net I'm trying to modify some code .. my goal is to encrypt a file with a key and store the key for decryption here is the code for encryption

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    OpenFileDialog1.Title = "Please Select a File To Encrypt"

    OpenFileDialog1.InitialDirectory = "C:\"

    OpenFileDialog1.ShowDialog()

    Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()

    Dim label As String = ASCIIEncoding.ASCII.GetString(desCrypto.Key)
    Label2.Text = label

    Dim myStream As Stream
    Dim saveFileDialog1 As New SaveFileDialog()
    saveFileDialog1.Title = "Save Encryption Key"
    saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True

    If saveFileDialog1.ShowDialog() = DialogResult.OK Then
        myStream = saveFileDialog1.OpenFile()

        If (myStream IsNot Nothing) Then

            myStream.Close()
        End If
    End If

    Dim fs As FileStream = Nothing

    Dim fileLoc As String = saveFileDialog1.FileName.ToString
    Using sw As StreamWriter = New StreamWriter(fileLoc)

        sw.WriteLine(label)


        sw.Close()
    End Using

    Dim sInputFilename As String = OpenFileDialog1.FileName.ToString

    Dim myEncStream As Stream
    Dim saveFileDialog2 As New SaveFileDialog()
    saveFileDialog2.Title = "Select Directory To Store Encrypted File"
    saveFileDialog2.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    saveFileDialog2.FilterIndex = 2
    saveFileDialog2.RestoreDirectory = True

    If saveFileDialog2.ShowDialog() = DialogResult.OK Then
        myEncStream = saveFileDialog2.OpenFile()

        If (myEncStream IsNot Nothing) Then
            myEncStream.Close()
        End If
    End If


    Dim Outputfile As String = saveFileDialog2.FileName.ToString
    Dim sKey As String = label
    Dim bytearray As Byte() = System.IO.File.ReadAllBytes(sInputFilename)
    Dim DES As New DESCryptoServiceProvider()
    Dim ms As New MemoryStream()

    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)


    Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
    Dim cryptostream As New CryptoStream(ms, desencrypt, CryptoStreamMode.Write)

    cryptostream.Write(bytearray, 0, bytearray.Length)
    cryptostream.Close()

    Dim encrypteddata As Byte() = ms.ToArray()
    System.IO.File.WriteAllBytes(Outputfile, encrypteddata)

End Sub

and the decryptor

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    OpenFileDialog2.Title = "Please Select a File To Decrypt"

    OpenFileDialog2.InitialDirectory = "C:\"

    OpenFileDialog2.ShowDialog()

    Dim sInputFilename As String = OpenFileDialog2.FileName.ToString

    Dim sKey As String
    Dim s As String
    MsgBox(sInputFilename)
    OpenFileDialog3.Title = "Please Select The Key"

    OpenFileDialog3.InitialDirectory = "C:\"

    OpenFileDialog3.ShowDialog()

    Dim keyfile As String = OpenFileDialog3.FileName.ToString
    MsgBox(keyfile)
    Dim tr As IO.TextReader = New IO.StreamReader(keyfile)

    s = tr.ReadToEnd
    sKey = s

    Dim myDecStream As Stream
    Dim saveFileDialog3 As New SaveFileDialog()
    saveFileDialog3.Title = "Select Directory To Store Decrypted File"
    saveFileDialog3.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    saveFileDialog3.FilterIndex = 2
    saveFileDialog3.RestoreDirectory = True

    If saveFileDialog3.ShowDialog() = DialogResult.OK Then
        myDecStream = saveFileDialog3.OpenFile()

        If (myDecStream IsNot Nothing) Then

            myDecStream.Close()
        End If
    End If


    Dim Orginal As String = saveFileDialog3.FileName.ToString
    Dim bytearray2 As Byte() = System.IO.File.ReadAllBytes(sInputFilename)
    Dim DES As New DESCryptoServiceProvider()
    Dim ms As New MemoryStream()

    DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

    Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
    Dim cryptostreamDecr As New CryptoStream(ms, desdecrypt, CryptoStreamMode.Write)

    cryptostreamDecr.Write(bytearray2, 0, bytearray2.Length)
    cryptostreamDecr.Close()

    Dim decryptedByte As Byte() = ms.ToArray
    System.IO.File.WriteAllBytes(Orginal, decryptedByte)


End Sub

Encryption goes will but when decrypting file an error raised something like "specified key is not a valid size for this Algorithm"

So what can I do to make the key valid ?

هل كانت مفيدة؟

المحلول

As I see it, you're converting the key bytes directly into ASCII text. If you do that, there might be some characters that will not show up and when you copy it elsewhere, it probably will not be there, and you'll lose a few characters, leading to the invalid keysize.

If you're planning on using StreamWriter.WriteLine(), try using Convert.ToBase64String() and Convert.FromBase64String() to convert your key to/from Base64.

b64Key = Convert.Base64String(desCrypto.Key)
sw.WriteLine(b64Key)
sw.Close()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top