[破解] DRM-内容数据版权加密保护技术学习(中):License预发放实现

时间:2023-12-10 21:14:32

在上一篇文章里实现了对媒体文体的DRM加密,现在一起来实现License的预发放。

所谓预发放就是在播放媒体文件之前先获取到License,License获取成功后,可直接在电脑上进行媒体文件的播放。

实现步骤如下:
1. 新建WebApplication工程,添加对WMRMObjs.dll的引用,在本文章中工程命名为DRMLicenseGet。

2. License预发放程序代码编写:

Web.config:配置文件

<appSettings>
<!-- 私钥 -->
<add key="PrivateKey" value="!qYNXRckn69VzoSNmGJ9umMfgSw="/>
<!-- 公钥 -->
<add key="PublicKey" value="R76wg3M2nq3yDRWWI2hFESO*zRXNo2qcNdeVocH7cjwBSN!WnOi8Gg=="/>
<!-- 种子 -->
<add key="Seed" value="eQ50hSVGytB3IEr0Tr6UAUMEVlJfcD9XObjE8UhP"/>
</appSettings>

配置文件内配置的私钥、公钥、种子和上一篇文章内的信息相同。

GenerateLicense.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WMRMOBJSLib;
using System.Configuration; namespace DRMLicenseGet
{
public class GenerateLicense
{
/// <summary>
/// 生成License
/// </summary>
/// <param name="keyID">加密时使用的keyID</param>
/// <param name="Challenge">页面传过来的Challenge值</param>
/// <returns></returns>
public static string Generate(string keyID, string Challenge)
{
//从配置文件内获得私钥、公钥、种子和后发放License获取URL地址
string privateKey = ConfigurationManager.AppSettings["PrivateKey"];
string publicKey = ConfigurationManager.AppSettings["PublicKey"];
string seed = ConfigurationManager.AppSettings["Seed"]; //处理许可请求对象
WMRMChallenge myWMRMChallenge = new WMRMChallenge();
//管理内容头对象
WMRMHeader myWMRMHeader = new WMRMHeader();
//管理加密密钥和许可密钥种子对象
WMRMKeys myWMRMKeys = new WMRMKeys();
//指定加密内容的使用权限对象
WMRMRights myWMRMRights = new WMRMRights();
//创建许可对象
WMRMLicGen myWMRMLicGen = new WMRMLicGen();
//将许可递交给客户端对象
WMRMResponse myWMRMResponse = new WMRMResponse(); try
{
string strLicenseRequested = Challenge;
myWMRMChallenge.Challenge = strLicenseRequested; myWMRMKeys.KeyID = keyID;
myWMRMKeys.Seed = seed; #region 设置播放权限 //最小的安全级别
myWMRMRights.MinimumAppSecurity = 500;
//是否允许播放,false-不允许,true-允许
myWMRMRights.AllowPlay = true;
//是否允许在PC机上播放,0-不允许,1-允许
myWMRMRights.AllowPlayOnPC = 1;
//允许播放次数
myWMRMRights.Playcount = 2;
//是否允许拷贝,false-不允许,true-允许
myWMRMRights.AllowCopy = false;
//允许拷贝次数
myWMRMRights.CopyCount = 1;
//是否允许联合播放,false-不允许,true-允许
myWMRMRights.AllowCollaborativePlay = false;
//是否允许刻录,false-不允许,true-允许
myWMRMRights.AllowPlaylistBurn = false;
//最大刻录次数
myWMRMRights.MaxPlaylistBurnCount = 1;
//能刻录到光盘上的最大次数
myWMRMRights.PlaylistBurnTrackCount = 1;
//是否允许备份许可证,0-不允许,1-允许
myWMRMRights.AllowBackupRestore = 0;
//是否允许烧录到CD上,0-不允许,1-允许
myWMRMRights.AllowBurnToCD = 0;
//是否允许把已打包的流保存到磁盘上,0-不允许,1-允许
myWMRMRights.AllowSaveStreamProtected = 0;
////当客户端机器时间更改到更早时间时,该证书是否失效,0-不失效,1-失败
//myWMRMRights.DisableOnClockRollback = 1;
////当客户端机器时间更改到更早时间时,该证书是否自动删除,0-不删除,1-删除
//myWMRMRights.DeleteOnClockRollback = 1;
myWMRMRights.PMRights = 51;
myWMRMRights.PMAppSecurity = 150; #endregion myWMRMLicGen.KeyID = myWMRMKeys.KeyID;
myWMRMLicGen.SetKey("", myWMRMKeys.GenerateKey());
myWMRMLicGen.Rights = myWMRMRights.GetAllRights();
myWMRMLicGen.ClientInfo = myWMRMChallenge.ClientInfo;
myWMRMLicGen.Priority = 10; #region 添加版权信息 //版权
myWMRMLicGen.set_Attribute("Copyright", "Microsoft");
//类型
myWMRMLicGen.set_Attribute("ContentType", "Video");
//作者
myWMRMLicGen.set_Attribute("Author", "Terence");
//网站
myWMRMLicGen.set_Attribute("ArtistURL", "http://www.cnblogs.com/fanmenglife");
//标题
myWMRMLicGen.set_Attribute("Title", "Terence's Video");
//License提供商
myWMRMLicGen.set_Attribute("LicenseDistributor", "Terence");
//License提供商网站
myWMRMLicGen.set_Attribute("LicenseDistributorURL", "http://www.cnblogs.com/fanmenglife");
//内容提供商
myWMRMLicGen.set_Attribute("ContentDistributor", "Terence");
//等级
myWMRMLicGen.set_Attribute("Rating", "高级");
//描述
myWMRMLicGen.set_Attribute("Description", "Terence's Video"); #endregion myWMRMLicGen.BindToPubKey = publicKey; //生成License
string license = myWMRMLicGen.GetLicenseToDeliver();
myWMRMResponse.AddLicense("2.0.0.0", license);
return myWMRMResponse.GetLicenseResponse();
}
catch (Exception e)
{
return "";
}
}
}
}

IssueLicense.aspx:该文件只需在服务器端编写代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace DRMLicenseGet
{
public partial class IssueLicense : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//获取challenge值
string challenge = Page.Request.Params["challenge"];
//获取KeyID值
string keyID = Page.Request.Params["keyid"];
//获得License
string license = GenerateLicense.Generate(keyID, challenge);
//输出License
Page.Response.Write(license);
}
}
}
}

GetLicense.aspx:该页面为用户操作页面,不需要在服务器端编写代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetLicense.aspx.cs" Inherits="DRMLicenseGet.GetLicense" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title> <script language="javascript" type="text/javascript">
function GetLicense() {
try {
var url = "http://localhost:20103/IssueLicense.aspx?keyid=" + document.getElementById("txtKeyID").value;
licenseObj.GetLicenseFromURL("<a></a>", url); alert('许可证下载成功!');
}
catch (e) {
alert("许可证下载失败,错误信息:" + e.message);
return;
}
}
</script> </head>
<body>
<object name="licenseObj" classid="clsid:A9FC132B-096D-460B-B7D5-1DB0FAE0C062" id="licenseObj"
width="0">
</object>
<form id="form1" runat="server">
<div>
<input id="txtKeyID" type="text" value="请输入加密时使用的KeyID" onclick="this.value = '';" />
<input id="btnGetLicense" type="button" value="获取License" onclick="GetLicense()" /></div>
</form>
</body>
</html>

3.效果:

[破解] DRM-内容数据版权加密保护技术学习(中):License预发放实现

输入媒体文件加密时使用的Key,点击获取License,获取成功就可以播放媒体文件啦。

下一篇文章将介绍License后发放的实现。

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

文章源代码:

下载