DRM-内容数据版权加密保护技术学习(上):视频文件打包实现

 
1. DRM介绍:

DRM,英文全称Digital Rights Management, 可以翻译为:内容数字版权加密保护技术。

DRM技术的工作原理是,首先建立数字节目授权中心。编码压缩后的数字节目内容,可以利用密钥(Key)进行加密保护(lock),加密的数字节目头部存放着KeyID和节目授权中心的URL。用户在点播时,根据节目头部的KeyID和URL信息,就可以通过数字节目授权中心的验证授权后送出相关的密钥解密(unlock),节目方可播放。

需要保护的节目被加密,即使被用户下载保存,没有得到数字节目授权中心的验证授权也无法播放,从而严密地保护了节目的版权。

f677b1c3cc292546b319a809

 

2. 安装SDK:

SDK下载

 

3. 去微软的网站注册以保证获得许可授权:

网站地址:http://licenseserver.windowsmedia.com/registration.asp?v2=true

填写邮箱、公司、国家等信息进行提交。

稍候你的邮箱会收到一封邮件,点击邮件里的地址,输入你的邮箱和邮件里收到的token。

 

3. 现在就要开始打包程序的编写了,在以下的例子中用WinForm程序实现

DRM打包需要公钥、私钥还有种子,我们只要新建一个文本文件,然后将如下代码复制进去

Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")

dim keysobjs
dim privkey
dim pubkey
dim seed

Set keysobj= CreateObject("wmrmobjs.WMRMKeys")
keysobj.GenerateSigningKeys privkey, pubkey
seed = keysobj.GenerateSeed()
Dim cmd

' Write the private key to privkey.txt.
cmd = "command.com /C echo " + privkey + " > privkey.txt" 
WSHShell.Run cmd,0

'Write the public key to pubkey.txt.
cmd = "command.com /C echo " + pubkey + " > pubkey.txt"
WSHShell.Run cmd,0

' Write the license key seed to seed.txt.
cmd = "command.com /C echo " + seed + " > seed.txt"
WSHShell.Run cmd,0

 

将文本文档更改为.vbs文件,运行该文件,会在本目录下形成PRIVKEY.TXT、PUBKEY.TXT、SEED.TXT三个文件,其中PRIVKEY.TXT中是私钥、PUBKEY.TXT中是公钥、SEED.TXT中是种子。

 

新建WinForm工程,添加对WMRMObjs.dll的引用,如果SDK安装成功的话,在SDK安装目录的Bin目录下可以找到。

 

将生成的公钥、私钥和种子在配置文件内进行配置

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <!-- 私钥 -->
        <add key="PrivateKey" value="!qYNXRckn69VzoSNmGJ9umMfgSw=" />
        <!-- 公钥 -->
        <add key="PublicKey" value="R76wg3M2nq3yDRWWI2hFESO*zRXNo2qcNdeVocH7cjwBSN!WnOi8Gg==" />
        <!-- 种子 -->
        <add key="Seed" value="eQ50hSVGytB3IEr0Tr6UAUMEVlJfcD9XObjE8UhP" />
        <!-- License获取页面URL -->
        <add key="LicenseAcpURL" value="http://localhost:8080/GetLicense.aspx" />
    </appSettings>
</configuration>

 

 

WinForm界面,这里简单画一个界面,界面上包括一个用来显示选择文件的本文框,选择文件按钮,开始打包按钮和一个OpenFileDialog控件,在OpenFileDialog控件的Filter属性内加上“支持DRM加密文件|*.WMV;*.WMA;*.ASF;*.WMS”

image

VideoDRMPackageForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VideoDRMPackageApplication
{
    public partial class VideoDRMPackageForm : Form
    {
        public VideoDRMPackageForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            openVideoFile.ShowDialog();
        }

        /// <summary>
        /// 开始打包
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBeginPackage_Click(object sender, EventArgs e)
        {
            //检查是否选择了文件
            if (String.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请选择要进行打包的文件!", "提示");
                return;
            }

            //打包Key
            string keyID = "DRMTest";
            //消息
            string msg = "";

            if (Package.PackageVideoFile(keyID, txtFilePath.Text, out msg))
                msg = "打包成功!" + msg;
            else
                msg = "打包失败!" + msg;

            MessageBox.Show(msg, "提示");
        }

        /// <summary>
        /// 文件选择完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openVideoFile_FileOk(object sender, CancelEventArgs e)
        {
            txtFilePath.Text = openVideoFile.FileName;
        }
    }
}

 

Package.cs

using System;
using System.IO;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using WMRMOBJSLib;
using System.Runtime;
using System.Runtime.InteropServices;

namespace VideoDRMPackageApplication
{
    public class Package
    {
        /// <summary>
        /// 打包视频文件
        /// </summary>
        /// <param name="keyID">用于打包的Key</param>
        /// <param name="sourcePath">打包文件地址</param>
        /// <param name="msg">输出消息</param>
        /// <returns>返回打包结果:true-成功,false-失败</returns>
        public static bool PackageVideoFile(string keyID, string sourcePath, out string msg)
        {
            //创建WMRMHeaderClass对象
            WMRMOBJSLib.WMRMHeaderClass myWMRMHeaderClass = new WMRMOBJSLib.WMRMHeaderClass();
            //创建WMRMProtectClass对象
            WMRMOBJSLib.WMRMProtectClass myWMRMProtectClass = new WMRMOBJSLib.WMRMProtectClass();

            //生成DRM加密后文件路径,在源文件名后加上_DRM,和源文件放在同一目录
            string drmFilePath = Path.GetDirectoryName(sourcePath) + Path.GetFileNameWithoutExtension(sourcePath) + "_DRM" + Path.GetExtension(sourcePath);

            try
            {
                //从配置文件内获得私钥、公钥、种子和后发放License获取URL地址
                string privateKey = ConfigurationManager.AppSettings["PrivateKey"];
                string publicKey = ConfigurationManager.AppSettings["PublicKey"];
                string seed = ConfigurationManager.AppSettings["Seed"];
                string licenseAcpUrl = ConfigurationManager.AppSettings["LicenseAcpUrl"];

                //创建用于DRM加密的Key
                string drmKey = KeyGenerate(keyID, seed);

                //给myWMRMHeaderClass对象赋值
                myWMRMHeaderClass.KeyID = keyID;
                myWMRMHeaderClass.IndividualizedVersion = "2.2";
                myWMRMHeaderClass.ContentID = keyID;
                myWMRMHeaderClass.LicenseAcqURL = licenseAcpUrl;
                myWMRMHeaderClass.SetCheckSum(drmKey);
                myWMRMHeaderClass.Sign(privateKey);

                myWMRMProtectClass.Header = myWMRMHeaderClass.Header;
                myWMRMProtectClass.InputFile = sourcePath;
                myWMRMProtectClass.Key = drmKey;
                myWMRMProtectClass.V1LicenseAcqURL = licenseAcpUrl;
                myWMRMProtectClass.V1KeyID = keyID;
                myWMRMProtectClass.ProtectFile(drmFilePath);
            }
            catch (Exception e)
            {
                msg = "错误信息:" + e.ToString();
                return false;
            }
            finally
            {
                try
                {
                    //释放对象
                    if (myWMRMHeaderClass != null)
                    {
                        Marshal.ReleaseComObject(myWMRMHeaderClass);
                        myWMRMHeaderClass = null;
                    }
                    if (myWMRMProtectClass != null)
                    {
                        Marshal.ReleaseComObject(myWMRMProtectClass);
                        myWMRMProtectClass = null;
                    }
                }
                catch (Exception ex)
                {
                }
            }
            msg = "打包后文件:" + drmFilePath;
            return true;
        }

        /// <summary>
        /// 创建用于DRM加密的Key
        /// </summary>
        /// <param name="keyID">用于打包的Key</param>
        /// <param name="seed"></param>
        /// <returns></returns>
        private static string KeyGenerate(string keyID, string seed)
        {
            //创建WMRMKeysClass对象
            WMRMKeysClass myWMRMKeysClass = new WMRMKeysClass();
            myWMRMKeysClass.KeyID = keyID.Trim();
            myWMRMKeysClass.Seed = seed.Trim();
            return myWMRMKeysClass.GenerateKey();
        }
    }
}

 

运行该程序,选择要进行打包的文件,点开始打包,过一会就打包完成了,现在去播放打包后的视频文件,嘿嘿,是不是开始获取License了啊,不过现在是获取不成功的,下一篇文章将会介绍License获取。

 

谢谢,希望我的文章对大家有帮助!

 

文章源代码:

下载

posted @ 2009-07-25 16:16  Terence  阅读(4652)  评论(16编辑  收藏  举报