如何播放解密文件

时间:2014-01-22 07:26:04

标签: asp.net video encryption video-streaming

对于要复制保护的视频,我想到了

步骤1)用密钥加密视频文件。

步骤-2)解密文件或在内存流中解密。

步骤3)播放解密文件或从内存流播放。

我已成功加密并使用密钥解密视频文件。但不知道如何播放解密文件(.dnc文件)。

有人可以帮我解密(文件或内存流)播放视频文件。

解密代码

 private void Decryption(string filePath)
        {
            try
            {
                DateTime current = DateTime.Now;
                string encName = filePath + "data" + ".enc";
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

                #region Seperate key and data
                byte[] alldata = File.ReadAllBytes(filePath);
                byte[] getencryptedkey = new byte[128];
                byte[] data = new byte[alldata.Length - 128];
                for (int i = 0; i < alldata.Length - 128; i++)
                { data[i] = alldata[i]; }
                for (int i = alldata.Length - 128, j = 0; i < alldata.Length; i++, j++)
                { getencryptedkey[j] = alldata[i]; }
                using (BinaryWriter bw = new BinaryWriter(File.Create(encName)))
                {
                    bw.Write(data);
                    bw.Close();
                }
                #endregion 

                #region key decryption
                StreamReader reader = new StreamReader("PublicPrivateKey.xml");
                string publicprivatekeyxml = reader.ReadToEnd();
                RSA.FromXmlString(publicprivatekeyxml);
                reader.Close();
                byte[] decryptedKey = RSA.Decrypt(getencryptedkey, false);
                pwd = System.Text.ASCIIEncoding.Unicode.GetString(decryptedKey);
                byte[] dk = null;
                byte[] div = null;
                crm.getKeysFromPassword(pwd, out dk, out div);
                cryptoKey = dk;
                cryptoIV = div;
                #endregion

                string ext = Path.GetExtension(encName).ToLower();
                if (ext != ".enc")
                {
                    MessageBox.Show("Please Enter correct File");
                    return;
                }
                string dncName = Path.GetDirectoryName(encName) + "\\" + Path.GetFileNameWithoutExtension(encName);
                dncName = current.Date.Day.ToString() + current.Date.Month.ToString() + current.Date.Year.ToString() + current.TimeOfDay.Duration().Hours.ToString() + current.TimeOfDay.Duration().Minutes.ToString() + current.TimeOfDay.Duration().Seconds.ToString() + ".wmv";
                try
                {
                    if (crm.DecryptData(encName, dncName, cryptoKey, cryptoIV))
                    {
                        FileInfo fi = new FileInfo(encName);
                        FileInfo fi2 = new FileInfo(dncName);
                        if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                        { fi.Attributes &= ~FileAttributes.ReadOnly; }
                        //copy creation and modification time
                        fi2.CreationTime = fi.CreationTime;
                        fi2.LastWriteTime = fi.LastWriteTime;
                        //delete encrypted file
                        File.Delete(encName);
                        MessageBox.Show("File Decrypted");
                    }
                    else
                    {
                        MessageBox.Show("The file can't be decrypted - probably wrong password");
                    }
                }

                catch (CryptographicException ex)
                { MessageBox.Show(ex.Message); }
                catch (IOException ex)
                { MessageBox.Show(ex.Message); }
                catch (UnauthorizedAccessException ex)
                { //i.e. readonly
                    MessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

1 个答案:

答案 0 :(得分:0)

要复制保护视频文件,实现它的最佳方法是将DRM应用于它。通过这种方式,您可以限制它应该播放的次数或用户可以使用多长时间,但这仍然可以通过很多方式来解决。

您不能将任何视频100%复制保护。请阅读以下文章。如果是这样的话,好莱坞电影就不会通过洪流网络免费提供。

http://www.streamingmedia.com/Articles/Editorial/Featured-Articles/DRM-Is-Dead-79353.aspx