读取文本文件不能解密,我在这里出错?

时间:2014-07-25 10:35:21

标签: security encryption cryptography c#-2.0

我创建了一个加密方法来将数据加密到test.txt文本文件中。加密工作正常,并将加密的文本文件读入textBox1

我使用2个按钮和2个textBox来执行此操作。

现在我要Read test.txttextBox2的解密文字test.txt 但是当我点击按钮从textBox2(CryptographicException was unhandled) Bad Data读取数据时。

我收到了private void button2_Click(object sender, EventArgs e) { FileStream stream = new FileStream("C:\\test.txt", FileMode.Open, FileAccess.Read); DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider(); cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH"); cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH"); CryptoStream crStream = new CryptoStream(stream, cryptic.CreateDecryptor(), CryptoStreamMode.Read); StreamReader reader = new StreamReader(crStream); //string data = reader.ReadToEnd(); textBox2.Text = reader.ReadLine(); //stream.Close(); } 等异常。

以下是我用于Dycryption的代码。

     private void button1_Click(object sender, EventArgs e)
    {
        FileStream stream = new FileStream("C:\\test.txt", FileMode.OpenOrCreate, FileAccess.Write);

        DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();

        cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
        cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");

        CryptoStream crStream = new CryptoStream(stream,
           cryptic.CreateEncryptor(), CryptoStreamMode.Write);


        byte[] data = ASCIIEncoding.ASCII.GetBytes(textBox2.Text);

        crStream.Write(data, 0, data.Length);
        crStream.Close();
        stream.Close();
        string text = System.IO.File.ReadAllText(@"C://test.txt");
        textBox1.Text = text.ToString();


    }

我用于加密的代码在这里:

{{1}}

1 个答案:

答案 0 :(得分:1)

我不知道你为什么要从test.txt文件中读取它。当你在文本框中显示它时。但检查下面的代码,你可以加密和解密,同时能够显示和保存在文本框中。

    protected void Button1_Click(object sender, EventArgs e)
    {
        FileStream stream = new FileStream("C:\\test.txt", FileMode.OpenOrCreate, FileAccess.Write);

        string datastring=    Encrypt("ABCDEFGH", "ABCDEFGH");


        byte[] data = ASCIIEncoding.ASCII.GetBytes(datastring);

        stream.Write(data, 0, data.Length);
        stream.Close();

        string text = System.IO.File.ReadAllText(@"C:\\test.txt");
        TextBox1.Text = text.ToString();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
         StreamReader reader =null;
        try
        {
            FileStream stream = new FileStream("C:\\test.txt",
                       FileMode.Open, FileAccess.Read);
           string fileContents;
           using ( reader = new StreamReader(stream))
{
    fileContents = reader.ReadToEnd();
}
            string data = Decrypt(fileContents, "ABCDEFGH");

            TextBox2.Text = data;

            stream.WriteByte(Convert.ToByte(data));
            stream.Close();

        }
        catch (Exception ex)
        {

        }
        finally
        {
            reader.Close();
        }

    }

    public static string Encrypt(string message, string password)
    {
        // Encode message and password
        byte[] messageBytes = ASCIIEncoding.ASCII.GetBytes(message);
        byte[] passwordBytes = ASCIIEncoding.ASCII.GetBytes(password);

        // Set encryption settings -- Use password for both key and init. vector
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        ICryptoTransform transform = provider.CreateEncryptor(passwordBytes, passwordBytes);
        CryptoStreamMode mode = CryptoStreamMode.Write;

        // Set up streams and encrypt
        MemoryStream memStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memStream, transform, mode);
        cryptoStream.Write(messageBytes, 0, messageBytes.Length);
        cryptoStream.FlushFinalBlock();

        // Read the encrypted message from the memory stream
        byte[] encryptedMessageBytes = new byte[memStream.Length];
        memStream.Position = 0;
        memStream.Read(encryptedMessageBytes, 0, encryptedMessageBytes.Length);

        // Encode the encrypted message as base64 string
        string encryptedMessage = Convert.ToBase64String(encryptedMessageBytes);

        return encryptedMessage;
    }

    public static string Decrypt(string encryptedMessage, string password)
    {
        // Convert encrypted message and password to bytes
        byte[] encryptedMessageBytes = Convert.FromBase64String(encryptedMessage);
        byte[] passwordBytes = ASCIIEncoding.ASCII.GetBytes(password);

        // Set encryption settings -- Use password for both key and init. vector
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        ICryptoTransform transform = provider.CreateDecryptor(passwordBytes, passwordBytes);
        CryptoStreamMode mode = CryptoStreamMode.Write;

        // Set up streams and decrypt
        MemoryStream memStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memStream, transform, mode);
        cryptoStream.Write(encryptedMessageBytes, 0, encryptedMessageBytes.Length);
        cryptoStream.FlushFinalBlock();

        // Read decrypted message from memory stream
        byte[] decryptedMessageBytes = new byte[memStream.Length];
        memStream.Position = 0;
        memStream.Read(decryptedMessageBytes, 0, decryptedMessageBytes.Length);

        // Encode deencrypted binary data to base64 string
        string message = ASCIIEncoding.ASCII.GetString(decryptedMessageBytes);

        return message;
    }