C # простое шифрование работает, но дешифрование не работает? - PullRequest
0 голосов
/ 23 февраля 2019

Надеюсь, кто-нибудь может помочь.Я следовал очень простому руководству о том, как сделать инструмент шифрования / дешифрования.Шифрование работает отлично, но дешифрование не работает.Шифрование работает в том смысле, что когда я открываю зашифрованный файл в текстовом документе, это все случайные символы, но когда я пытаюсь расшифровать его, исходные данные не восстанавливаются, это просто более случайные символы.

private void Btn_Encrypt_Click(object sender, EventArgs e)
{
    MessageBox.Show("Your file has been encrypted", "Complete");
    // a simle message box used to notify the user that the encryption process has been completed.
    Encrypt(ShowPathEnc.Text, TxtSaved.Text, key);
    // when the button is clicked the private void "Encrypt" is called and ran.
}

private void Encrypt(string input, string output, string strHash)
{
    FileStream inStream, OutStream;
    // provideds a stearm for the input(selected raw file) to be ran through the encryption algorithm and for the Output(the saved encryption file) for that file to leave 
    //the stearm as an encrypted file
    CryptoStream CryptStream;
    // CryptoStream is used to define a stream that is used to link data streams (inStrean & OutStream) to cryptographyc transformations(ASCII Encoding in this case)
    TripleDESCryptoServiceProvider TripCrypto = new TripleDESCryptoServiceProvider();
    //Defines a wrapper object to access the cryptographic service provider for triple data encryption
    MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
    //defines new object that takes a string and uses MD5 to return a 32 character hexedecimal fotrmated string hash. 

    byte[] byteHash, byteText;

    inStream = new FileStream(input, FileMode.Open, FileAccess.Read);
    // takes the raw file that has been selected by the user that is in tne dtream opens that file and reads it.
    OutStream = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write);
    // takes the save as file that the user has selected opens or creates it and writes the encrypted data into it 
    byteHash = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash));
    //  uses the MD5 hash  to compute the data in the file and uses the ASCII key generated earlier to encrypt each byte of data in the file.
    byteText = File.ReadAllBytes(input);
    //reads all the bytes in the in the raw file making to have every byte encrypted.

    MD5.Clear();
    // the hash is then cleared for the next encryption as a completely new unique hash will be used for the next file.

    TripCrypto.Key = byteHash;
    // uses the key to give "TripCrypto" access to the bytes that have been computed by the hash and ASCII encoding.
    TripCrypto.Mode = CipherMode.ECB;
    //sets the triple layer data encryption to use the CBC standard of encryption.

    CryptStream = new CryptoStream(OutStream, TripCrypto.CreateEncryptor(), CryptoStreamMode.Write);
    //uses the cyrptostream to take the file in OutStrean tun it through the Triple layer dadta encryption and the the wtite mode to write the encrypted data back to the 
    //file in the OutStream

    int bytesRead;
    long length, position = 0;
    //instantiates variables used to check the length of the stream in bytes and the position of each byte in the stream
    length = inStream.Length;
    // gets the length of the stream in bytes

    while (position < length)
    {
        bytesRead = inStream.Read(byteText, 0, byteText.Length);
        position += bytesRead;

        CryptStream.Write(byteText, 0, bytesRead);
    }
    //the while loop is comparin g the length of each byte and position of each byte to ensure that there has been a change in position meaning data scrambling
    //is  used aswell as layered encryption.
    inStream.Close();
    OutStream.Close();
    //files have exited bothe streams and therefore there is no need for those streams to be running and they're closed.


}


private void Btn_Decrypt_Click(object sender, EventArgs e)
{
    MessageBox.Show("Your file has been decrypted", "Complete");
    // a simle message box used to notify the user that the encryption process has been completed.
    decrypt(ShowPathDec.Text, SaveDec.Text, key);
    // when the button is clicked the private void "Encrypt" is called and ran.
}
private void decrypt(string input, string output, string strHash)
{
    FileStream inStream, OutStream;
    // provideds a stearm for the input(selected raw file) to be ran through the encryption algorithm and for the Output(the saved encryption file) for that file to leave 
    //the stearm as an encrypted file
    CryptoStream CryptStream;
    // CryptoStream is used to define a stream that is used to link data streams (inStrean & OutStream) to cryptographyc transformations(ASCII Encoding in this case)
    TripleDESCryptoServiceProvider TripCrypto = new TripleDESCryptoServiceProvider();
    //Defines a wrapper object to access the cryptographic service provider for triple data encryption
    MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
    //defines new object that takes a string and uses MD5 to return a 32 character hexedecimal fotrmated string hash. 

    byte[] byteHash, byteText;

    inStream = new FileStream(input, FileMode.Open, FileAccess.Read);
    // takes the raw file that has been selected by the user that is in tne dtream opens that file and reads it.
    OutStream = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write);
    // takes the save as file that the user has selected opens or creates it and writes the encrypted data into it 
    byteHash = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash));
    //  uses the MD5 hash  to compute the data in the file and uses the ASCII key generated earlier to encrypt each byte of data in the file.
    byteText = File.ReadAllBytes(input);
    //reads all the bytes in the in the raw file making to have every byte encrypted.

    MD5.Clear();
    // the hash is then cleared for the next encryption as a completely new unique hash will be used for the next file.

    TripCrypto.Key = byteHash;
    // uses the key to give "TripCrypto" access to the bytes that have been computed by the hash and ASCII encoding.
    TripCrypto.Mode = CipherMode.ECB;
    //sets the triple layer data encryption to use the CBC standard of encryption.

    CryptStream = new CryptoStream(OutStream, TripCrypto.CreateDecryptor(), CryptoStreamMode.Write);
    //uses the cyrptostream to take the file in OutStrean tun it through the Triple layer dadta encryption and the the wtite mode to write the encrypted data back to the 
    //file in the OutStream

    int bytesRead;
    long length, position = 0;
    //instantiates variables used to check the length of the stream in bytes and the position of each byte in the stream
    length = inStream.Length;
    // gets the length of the stream in bytes

    while (position < length)
    {
        bytesRead = inStream.Read(byteText, 0, byteText.Length);
        position += bytesRead;

        CryptStream.Write(byteText, 0, bytesRead);
    }
    //the while loop is comparin g the length of each byte and position of each byte to ensure that there has been a change in position meaning data scrambling
    //is  used aswell as layered encryption.
    inStream.Close();
    OutStream.Close();
    //files have exited bothe streams and therefore there is no need for those streams to be running and they're closed.


}

1 Ответ

0 голосов
/ 24 февраля 2019

Краткий ответ: поместите блок использования вокруг вашего CryptoStreams.

Вы используете режим "Электронная кодовая книга" для своего провайдера шифрования.Это означает, что он не будет писать зашифрованный блок, пока вы не предоставите этому блоку достаточно данных для шифрования.

Я не знаю длины блока по умолчанию, но давайте представим, что это 4 для иллюстрациицели.

Text before encryption: ABCDEFGHI
[ABCD] -> Fills up the block and gets encrypted.
[DEFG] -> Fills up the block and gets encrypted.
[HI__] -> Doesn't fill up the code block and never gets encrypted or written to your file.

Чтобы заставить CryptoStream зашифровать неполный блок, вы можете либо вызвать CryptoStream.Flush (), либо обернуть блок using вокруг вашего CryptoStream.

...