压缩和加密文档并在C#中输入pswd [英] Zipping and encrypting a doc and sotring the pswd in C#

查看:71
本文介绍了压缩和加密文档并在C#中输入pswd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..我需要压缩和加密文件并存储在DB..i中也应该能够检索文件(加密和压缩格式)并通过pswd发送邮件... Plz帮助代码



我的尝试:



能够加密但是无法以加密格式检索

Hi..I need to zip and encrypt a file and store in DB..i should also be able to retrieve the file (encrypted and in zipped format) and send over mail along with the pswd...Plz help with the code

What I have tried:

able to encrypt but not able to retrieve in encrypted format

推荐答案

数据加密后,它只是二进制数据 - 因此存储和检索数据与任何其他字节数组完全相同基于数据。

因此存储它是微不足道的:

Once the data is encrypted, it's just binary data - so storing and retrieving it is exactly the same as for any other byte array based data.
So storing it is trivial:
byte[] data = myEncryptedStream.ToArray();
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO MyTable (EncryptedColumn) VALUES (@ED)", con))
        {
        com.Parameters.AddWithValue("@ED", data);
        com.ExecuteNonQuery();
        }
    }



检索它是这样的:


And so is retrieving it:

using (SqlConnection con = DBAccess.DBCon)
    {
    using (SqlCommand cmd = new SqlCommand("SELECT EncryptedColumn FROM MyTable WHERE Id=@ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", Id);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
            {
            byte[] data = (byte[])reader["EncryptedColumn"];
            using (MemoryStream stream = new MemoryStream(bytes))
                {
                // Stream ready to be passed to decryption.
                ...
                }
            }
        }
    }


这篇关于压缩和加密文档并在C#中输入pswd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆