Beispiel #1
0
        /// <summary>
        /// 加密文件
        /// </summary>
        /// <param name="inFile">待加密文件</param>
        /// <param name="outFile">加密后输入文件</param>
        /// <param name="password">加密密码</param>
        public static void EncryptFile(string inFile, string outFile, string password)
        {
            using (FileStream fin = File.OpenRead(inFile),
                   fout = File.OpenWrite(outFile))
            {
                long   lSize = fin.Length;            // 输入文件长度
                int    size  = (int)lSize;
                byte[] bytes = new byte[BUFFER_SIZE]; // 缓存
                int    read  = -1;                    // 输入文件读取数量
                int    value = 0;

                // 获取IV和salt
                byte[] IV   = GenerateRandomBytes(16);
                byte[] salt = GenerateRandomBytes(16);

                // 创建加密对象
                SymmetricAlgorithm sma = DESFileClass.CreateRijndael(password, salt);
                sma.IV = IV;

                // 在输出文件开始部分写入IV和salt
                fout.Write(IV, 0, IV.Length);
                fout.Write(salt, 0, salt.Length);

                // 创建散列加密
                HashAlgorithm hasher = SHA256.Create();
                using (CryptoStream cout = new CryptoStream(fout, sma.CreateEncryptor(), CryptoStreamMode.Write),
                       chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
                {
                    BinaryWriter bw = new BinaryWriter(cout);
                    bw.Write(lSize);

                    bw.Write(FC_TAG);

                    // 读写字节块到加密流缓冲区
                    while ((read = fin.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        cout.Write(bytes, 0, read);
                        chash.Write(bytes, 0, read);
                        value += read;
                    }
                    // 关闭加密流
                    chash.Flush();
                    chash.Close();

                    // 读取散列
                    byte[] hash = hasher.Hash;

                    // 输入文件写入散列
                    cout.Write(hash, 0, hash.Length);

                    // 关闭文件流
                    cout.Flush();
                    cout.Close();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 将所有文件加密成xxx文件
        /// </summary>
        /// <param name="path"></param>
        public void AllFile2xxx(string path)
        {
            string[] FilePath = Directory.GetDirectories(path);
            string   filename = string.Empty;

            for (int i = 0; i < FilePath.Length; i++)
            {
                AllFile2xxx(FilePath[i]);
            }
            string[] Newfilepath = Directory.GetFiles(path);
            for (int i = 0; i < Newfilepath.Length; i++)
            {
                string filepath     = Path.GetFullPath(Newfilepath[i]);
                string filerealpath = Path.GetDirectoryName(Newfilepath[i]) + "\\" + Path.GetFileNameWithoutExtension(Newfilepath[i]);
                DESFileClass.EncryptFile(filepath, filerealpath + ".xxx", Global.FileEnKey);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 从本地文件中读取文件信息,放进locallist列表
        /// </summary>
        /// <param name="path"></param>
        public void SetLocalFileList(string path)
        {
            try
            {
                string[] FilePath = Directory.GetDirectories(path);
                string   filename = string.Empty;
                for (int i = 0; i < FilePath.Length; i++)
                {
                    SetLocalFileList(FilePath[i]);
                }
                string[] Newfilepath = Directory.GetFiles(path);
                for (int i = 0; i < Newfilepath.Length; i++)
                {
                    filename = Path.GetFullPath(Newfilepath[i]);
                    string fileRealName = Path.GetDirectoryName(Newfilepath[i]) + "\\" + Path.GetFileNameWithoutExtension(Newfilepath[i]);
                    string fileExName   = Path.GetExtension(Newfilepath[i]);
                    if (string.Compare(fileExName, ".xxx") == 0)
                    {
                        string fileTempName = fileRealName + ".temp";

                        DESFileClass.DecryptFile(filename, fileTempName, Global.FileEnKey);
                        CommonData.Data data = new CommonData.Data
                        {
                            FilePath = fileRealName,
                            FileMD5  = GetMD5Content(fileTempName),
                            FileURL  = ""
                        };
                        fileListLocal.DataList.Add(data);
                    }
                }
                string sysPath = System.Windows.Forms.Application.StartupPath;
            }
            catch (Exception ex)
            {
                Log.Error("[" + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "][" + System.Reflection.MethodBase.GetCurrentMethod().Name + "] err" + ex);
                throw;
            }
        }
Beispiel #4
0
        public int DoUpdateFileList(BeingUpdateFile beingupdateform)
        {
            int  res      = -1;
            bool stepIsOk = true;

            try
            {
                //fileListLocal.DataList=fileListUpdate.DataList+fileListAdd.DataList;
                if (fileListUpdate.DataList.Count != 0 && fileListDelete.DataList.Count != 0 && fileListAdd.DataList.Count != 0)
                {
                    return(res);
                }
                int totalCount = fileListUpdate.DataList.Count * 2 + fileListDelete.DataList.Count + fileListAdd.DataList.Count;
                beingupdateform.SetMaxValue(10000 * (totalCount - 1) / totalCount);
                int index = 0;
                if (fileListUpdate.DataList.Count != 0)
                {
                    foreach (CommonData.Data d in fileListUpdate.DataList)
                    {
                        CommonData.Data delData = FindDataByPath(fileListLocal, d.FilePath);
                        if (delData != null)
                        {
                            //int index = fileListLocal.DataList.IndexOf();
                            if (File.Exists(delData.FilePath + ".xxx"))
                            {
                                File.Delete(delData.FilePath + ".xxx");
                                fileListLocal.DataList.Remove(delData);
                                fileListAdd.DataList.Add(d);
                                if (File.Exists(delData.FilePath + ".temp"))
                                {
                                    File.Delete(delData.FilePath + ".temp");
                                }
                                beingupdateform.SetTextMessage(10000 * index++ / totalCount);
                            }
                            else
                            {
                                stepIsOk = false;
                                break;
                            }
                        }
                    }
                    if (stepIsOk)
                    {
                        fileListUpdate.DataList.Clear();
                    }
                }

                if (fileListDelete.DataList.Count != 0)
                {
                    foreach (CommonData.Data d in fileListDelete.DataList)
                    {
                        CommonData.Data data = FindDataByPath(fileListLocal, d.FilePath);
                        if (data != null)
                        {
                            //int index = fileListLocal.DataList.IndexOf();
                            if (File.Exists(data.FilePath + ".xxx"))
                            {
                                File.Delete(data.FilePath + ".xxx");
                                fileListLocal.DataList.Remove(data);
                                if (File.Exists(data.FilePath + ".temp"))
                                {
                                    File.Delete(data.FilePath + ".temp");
                                }
                                beingupdateform.SetTextMessage(10000 * index++ / totalCount);
                            }
                            else
                            {
                                stepIsOk = false;
                                break;
                            }
                        }
                    }
                    if (stepIsOk)
                    {
                        fileListDelete.DataList.Clear();
                    }
                }

                if (fileListAdd.DataList.Count != 0)
                {
                    foreach (CommonData.Data d in fileListAdd.DataList)
                    {
                        //Log.Info("Application.StartupPath + \"//\" + d.FilePath + \".temp\" = " + Application.StartupPath + "//" + d.FilePath + ".temp");
                        if (HttpDownload(d.FileURL, Application.StartupPath + "//" + d.FilePath + ".temp"))
                        {
                            DESFileClass.EncryptFile(Application.StartupPath + "//" + d.FilePath + ".temp", Application.StartupPath + "//" + d.FilePath + ".xxx", Global.FileEnKey);
                            fileListLocal.DataList.Add(d);
                            beingupdateform.SetTextMessage(10000 * index++ / totalCount);
                        }
                        else
                        {
                            stepIsOk = false;
                            break;
                        }
                    }
                    if (stepIsOk)
                    {
                        fileListAdd.DataList.Clear();
                    }
                }
                if (fileListUpdate.DataList.Count == 0 && fileListDelete.DataList.Count == 0 && fileListAdd.DataList.Count == 0)
                {
                    res = 0;
                }
            }
            catch (Exception ex)
            {
                Log.Error("[" + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "][" + System.Reflection.MethodBase.GetCurrentMethod().Name + "] err" + ex);
                res = -1;
                throw;
            }
            return(res);
        }