-1

希望有人可以提供帮助。我遵循了一个关于如何制作加密/解密工具的非常简单的教程。加密工作完美,但解密不起作用。加密的工作原理是,当我在文本文档中打开加密文件时,它都是随机字符,但是当我尝试解密它时,原始数据并没有恢复,它只是更多的随机字符。

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.


}
4

1 回答 1

1

简短的回答:在 CryptoStreams 周围放置一个 using 块。

您正在为您的加密提供商使用“电子密码本”模式。这意味着在您为该块提供足够的数据进行加密之前,它不会写入加密块。

我不知道默认的块长度,但为了说明目的,我们假设它是 4。

加密前的文本:ABCDEFGHI
[ABCD] -> 填满区块并加密。
[DEFG] -> 填满区块并加密。
[HI__] -> 不会填满代码块,也不会被加密或写入您的文件。

要强制 CryptoStream 加密不完整的块,您可以调用 CryptoStream.Flush() 或者您可以在 CryptoStream 周围包装 using 块。

于 2019-02-23T22:02:38.213 回答