微软DRM尝试

最近研究了一下数字版权管理,用的是微软的DRM 7.1,这两天把它改写成了C#的,今天粘出来。不过还是有不少问题,比如,有些视频无法添加数字版权。可能是格式不是完全符合微软的标准吧。

package.asp改写成package.aspx

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using WMRMOBJSLib;

public partial class doPackage : System.Web.UI.Page
{
    private string seed;
    private string siteurl;
    private string owner;
    private string contentserverprivkey;
    private string contentserverpubkey;
    private string regpage;
    private string licurl;
    private string inputfile;
    private string outputfile;
    private string cid;
    private string rid;
    private string kid;
    private string header;

    private string key;

    protected void Page_Load(object sender, EventArgs e)
    {
        initValue();
        readForm();
        doFun();
    }
    private void initValue()
    {
        seed = (String)Application["seed"];
        siteurl = (String)Application["siteurl"];
        owner = (String)Application["ownerid"];
        contentserverprivkey = (String)Application["contentserverprivkey"];
        contentserverpubkey = (String)Application["contentserverpubkey"];
        cid = "500";
    }
    private void readForm()
    {
        inputfile = "C:\sample.wmv";//Request.Form["filepath"];
        rid = Request.Form["RID"];
        if (rid == null || rid == "")
        {
            rid = "0";
        }

        outputfile = "c:\"+Request.Form["newname"]+".wmv";
        regpage = Request.Form["URL"];

        licurl = this.siteurl + "/"+regpage;
    }
    private void doFun()
    {
        try
        {
            //创建一个Header对象
            WMRMOBJSLib.WMRMHeaderClass headObj = new WMRMOBJSLib.WMRMHeaderClass();
            //创建一个key对象
            WMRMOBJSLib.WMRMKeysClass keyObj = new WMRMOBJSLib.WMRMKeysClass();
            //创建项目对象
            WMRMOBJSLib.WMRMProtectClass projectObj = new WMRMOBJSLib.WMRMProtectClass();

            kid = keyObj.GenerateKeyID();
            keyObj.KeyID = kid;
            keyObj.Seed = seed;
            key = keyObj.GenerateKey();

            headObj.KeyID = kid;
            headObj.LicenseAcqURL = licurl;
            headObj.ContentID = cid;

            headObj.set_Attribute("Description","影片限制:" + rid);
            headObj.SetCheckSum(key);

            if (regpage != "simple.aspx")
            {
                headObj.set_Attribute("RID", rid);
            }

            headObj.Sign(contentserverprivkey);

            header = headObj.Header;

            if (outputfile != "")
            {
                projectObj.InputFile = inputfile;
                projectObj.Key = key;
                projectObj.Header = header;
                projectObj.V1KeyID = kid;

                projectObj.ProtectFile(outputfile);

                Response.Write("<a href=’" + outputfile + "’>" + outputfile + "</a>");
            }
        }
        catch (Exception ex)
        {
            string _error = ex.Message;
            Response.Write(_error);
        }
    }
}
simple.asp 改写成simple.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class simple : System.Web.UI.Page
{
    public string licresponse;
    public string strLicenseRequested;
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Buffer = true;
        Response.Expires = 0;

        string conpulicKey;
        string seed;

        conpulicKey = (String)Application["contentserverpubkey"];
        seed = (String)Application["seed"];

        string description;
        Boolean silent;
        string delivery_tag;

        description = "";


        silent = true;

        if (Request.Form["nonsilent"] != "")
        {
            silent = false;
        }

        try
        {
            //创建客户端对象
            WMRMOBJSLib.WMRMChallengeClass objChallenge = new WMRMOBJSLib.WMRMChallengeClass();

            //获取客户端信息
           
            strLicenseRequested = Request.Form.Get("challenge");

            objChallenge.Challenge = strLicenseRequested;

            //
            string varClientInfo;
            string varHeader;

            //在Chanllenge对象中设置客户信息
            varClientInfo = objChallenge.ClientInfo;
            //设置在Chanllenge对象的头
            varHeader = objChallenge.Header;

            /*
             *头对象
             */
            WMRMOBJSLib.WMRMHeaderClass objHeader = new WMRMOBJSLib.WMRMHeaderClass();
            string Kid;

            //把header放入到Header对象中以获得key ID 信息
            objHeader.Header = varHeader;

            int blnResult;
            blnResult = objHeader.Verify(conpulicKey);

            //从Header对象中获取key ID
            Kid = objHeader.KeyID;

            string indiVersion;
            indiVersion = "";
            //indiVersion = objHeader.IndividualizedVersion;

            /*
             * Key 对象
             *
             */
            WMRMOBJSLib.WMRMKeysClass objKey = new WMRMOBJSLib.WMRMKeysClass();
            string varKEY;

            //
            objKey.KeyID = Kid;
            objKey.Seed = seed;

            //
            varKEY = objKey.GenerateKey();

            /*
             * 开始设置权限
             */
            WMRMOBJSLib.WMRMRights objRights = new WMRMOBJSLib.WMRMRights();
            string SetV2Rights;

            objRights.MinimumAppSecurity = 500;     //最小的安全级别 http://msdn.microsoft.com/en-us/library/ms986423.aspx
            objRights.AllowPlayOnPC = 1;            //表示允许在PC机上播放
            objRights.AllowBackupRestore = 1;       //这个表示不允许备份许可证
            objRights.AllowBurnToCD = 0;            //表示不允许烧录到CD上
            objRights.AllowSaveStreamProtected = 0; //表示不允许把已打包的流保存到磁盘上
            objRights.AllowTransferToSDMI = 1;      //表示不允许把文件转移到非SDMI设备上
            objRights.AllowTransferToNonSDMI = 0;   //表示不允许把文件转移到非SDMI设备上
            objRights.DisableOnClockRollback = 0;   //当客户端机器时间更改到更早时间时,该证书失效
            objRights.DeleteOnClockRollback = 0;    //当客户端机器时间更改到更早时间时,该证书自动删除
            objRights.Playcount = 2;
            objRights.PMRights = 51;
            objRights.PMAppSecurity = 150;

            SetV2Rights = objRights.GetAllRights(); //获取所有权限设置

           

            /*
             * Generate a license with the License object
             */
            WMRMOBJSLib.WMRMLicGenClass objLicense = new WMRMOBJSLib.WMRMLicGenClass();
            string varLicense;
           
            object varCategory = new object();
            object varVersion = new object();
           
            Response.Write("====");

            objLicense.KeyID = Kid;
            objLicense.SetKey("", varKEY);
            objLicense.Priority = 10;       //设置优先级别,默认为1

            objLicense.set_Attribute("LIECENSESERVER", "<NAME>License Sever</NAME>");
            objLicense.Rights = SetV2Rights;    //设置证书的播放权限
            objLicense.ClientInfo = varClientInfo; //设置关于客户端计算机信息
            objLicense.IndividualizedVersion = indiVersion;
            objLicense.GetClientVersion(ref varCategory, ref varVersion);
            if (varCategory.ToString() != "0" && varCategory.ToString() != "1")//0:Default DRM component on Win32 platform,1:DRM component on Win32 platform with security upgrade
            {
                //return false;
            }
            objLicense.BindToPubKey = conpulicKey;
            varLicense = objLicense.GetLicenseToDeliver();

            delivery_tag = "deliver";
            Response.Write("——");
            /*
             * Response Object
             */
            WMRMOBJSLib.WMRMResponseClass responseObj = new WMRMOBJSLib.WMRMResponseClass();


            responseObj.AddLicense("2.0.0.0", varLicense);

            this.licresponse = responseObj.GetLicenseResponse();
            Response.Write(licresponse);
        }
        catch (Exception ex)
        {
            Response.Write("\n");
            Response.Write(ex.Message);
        }

    }
}

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: