图像文件加密

 

 

        /// <summary>
        /// 文件加密
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="savePath">保存路径</param>
        /// <param name="keyStr">密钥</param>
        public static void encryptFile(string filePath, string savePath, string keyStr)
        {
            //通过des加密
            var des = new DESCryptoServiceProvider();
            //通过流打开文件
            var fs = File.OpenRead(filePath);
            //获取文件二进制字符
            var inputByteArray = new byte[fs.Length];
            //读流文件
            fs.Read(inputByteArray, 0, (int)fs.Length);
            //关闭流
            fs.Close();
            //获得加密字符串二进制字符
            var keyByteArray = Encoding.Default.GetBytes(keyStr);
            //计算指定字节组指定区域哈希值
            SHA1 ha = new SHA1Managed();
            var hb = ha.ComputeHash(keyByteArray);
            //加密密钥数组
            var sKey = new byte[8];
            //加密变量
            var sIV = new byte[8];
            for (var i = 0; i < 8; i++)
                sKey[i] = hb[i];
            for (var i = 8; i < 16; i++)
                sIV[i - 8] = hb[i];
            //获取加密密钥
            des.Key = sKey;
            //设置加密初始化向量
            des.IV = sIV;
            var ms = new MemoryStream();
            var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            if (!Directory.Exists(savePath.Substring(0, savePath.LastIndexOf(@"\", StringComparison.Ordinal))))
            {
                Directory.CreateDirectory(savePath.Substring(0,
                    savePath.LastIndexOf(@"\", StringComparison.Ordinal)));
            }


            fs = File.OpenWrite(savePath);
            foreach (var b in ms.ToArray())
            {
                fs.WriteByte(b);
            }
            fs.Close();
            cs.Close();
            ms.Close();
        }
        

 

 

 

        /// <summary>
        /// 图片解密
        /// </summary>
        /// <param name="filePath">源文件</param>
        /// <param name="savePath">保存文件</param>
        /// <param name="keyStr">密钥</param>
        public static bool decryptFile(string filePath, string savePath, string keySt)r
        {
            //通过des解密
            var des = new DESCryptoServiceProvider();
            //通过流读取文件
            var fs = File.OpenRead(filePath);
            //获取文件二进制字符
            var inputByteArray = new byte[fs.Length];
            //读取流文件
            fs.Read(inputByteArray, 0, (int)fs.Length);
            //关闭流
            fs.Close();
            //密钥数组
            var keyByteArray = Encoding.Default.GetBytes(keyStr);
            //定义哈希变量
            SHA1 ha = new SHA1Managed();
            //计算指定字节组指定区域哈希值
            var hb = ha.ComputeHash(keyByteArray);
            //加密密钥数组
            var sKey = new byte[8];
            //加密变量
            var sIV = new byte[8];
            for (var i = 0; i < 8; i++)
                sKey[i] = hb[i];
            for (var i = 8; i < 16; i++)
                sIV[i - 8] = hb[i];
            //获取加密密钥
            des.Key = sKey;
            //加密变量
            des.IV = sIV;
            var ms = new MemoryStream();
            var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            try
            {

                cs.FlushFinalBlock();
            }
            catch (Exception)
            {
                return false;
            }
            if (!Directory.Exists(savePath.Substring(0, savePath.LastIndexOf(@"\", StringComparison.Ordinal))))
            {
                Directory.CreateDirectory(savePath.Substring(0,
                    savePath.LastIndexOf(@"\", StringComparison.Ordinal)));
            }

            fs = File.OpenWrite(savePath);
            foreach (var b in ms.ToArray())
            {
                fs.WriteByte(b);
            }
            fs.Close();
            cs.Close();
            ms.Close();
            return true;
        }

 

posted @ 2014-04-03 14:14  Alf7  阅读(255)  评论(0编辑  收藏  举报