在上一篇文章裏實現了對媒體文體的DRM加密,如今一塊兒來實現License的預發放。javascript
所謂預發放就是在播放媒體文件以前先獲取到License,License獲取成功後,可直接在電腦上進行媒體文件的播放。html
實現步驟以下:
1. 新建WebApplication工程,添加對WMRMObjs.dll的引用,在本文章中工程命名爲DRMLicenseGet。java
2. License預發放程序代碼編寫:安全
Web.config:配置文件服務器
<appSettings> <!-- 私鑰 --> <add key="PrivateKey" value="!qYNXRckn69VzoSNmGJ9umMfgSw="/> <!-- 公鑰 --> <add key="PublicKey" value="R76wg3M2nq3yDRWWI2hFESO*zRXNo2qcNdeVocH7cjwBSN!WnOi8Gg=="/> <!-- 種子 --> <add key="Seed" value="eQ50hSVGytB3IEr0Tr6UAUMEVlJfcD9XObjE8UhP"/> </appSettings>
配置文件內配置的私鑰、公鑰、種子和上一篇文章內的信息相同。app
GenerateLicense.cs:ide
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:該頁面爲用戶操做頁面,不須要在服務器端編寫代碼this
<%@ 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.效果:加密
輸入媒體文件加密時使用的Key,點擊獲取License,獲取成功就能夠播放媒體文件啦。
下一篇文章將介紹License後發放的實現。
謝謝,但願個人文章對你們有幫助!
文章源代碼: