Solution to hide the playback file address of MediaPlayer control in ASP

Source: Internet
Author: User
Tags constructor decrypt key net return string silverlight

When you play the video, you see the video file address directly from the page source file, and the MediaPlayer control player adds the following initialization statement to the page:

MediaPlayer control initialization to find the source file address

Sys.Application.add_init (function ()

{

$create (Sys.UI.Silverlight.MediaPlayer,

{

"AutoPlay": true, "MediaSource": "Http://211.142.31.235/Nas1/VOD/CP0001/490Kbps/D/DI/DIA/DiaoZhongYuan1/DiaoZhongYuan1.wmv",

"ScaleMode": 2, "source": "Usercontrol/playertheme/player.xaml", "Volume": 1},

NULL, NULL,

$get ("Myusermediaplayer_player_parent"));

});

The solution is to create a string encryption and decryption service first

Encryption, decryption tool class

1/**////<summary>

2///Tool class for string encryption and decryption

3///</summary>

4 public class Desencryptor

5 {

6 static Public method #region static public method

7/**////<summary>

8///to encrypt a string

9///</summary>

///<param name= "strsource" > Source string </param>

One-///<returns> encrypted string </returns>

public static string Execencrypt (String strsource)

13 {

Desencryptor desencryptor = new Desencryptor ();

desencryptor.inputstring = strsource;

Desencryptor.desencrypt ();

return desencryptor.outstring;

18}

/**////<summary>

20///to decrypt the encrypted string

///</summary>

///<param name= "strsource" > Encrypted string </param>

///<returns> restored string </returns>

public static string Execdeencrypt (String strsource)

25 {

Desencryptor desencryptor = new Desencryptor ();

desencryptor.inputstring = strsource;

Desencryptor.desdecrypt ();

return desencryptor.outstring;

30}

#endregion

32

33 Private member #region Private member

/**////<summary>

35///Input String

///</summary>

Notoginseng private String Inputstring=null;

/**////<summary>

39///Output String

///</summary>

a private string outstring=null;

/**////<summary>

43///Input File path

///</summary>

The private string inputfilepath=null;

/**////<summary>

47///Output File path

///</summary>

Outfilepath=null private string;

M/**////<summary>

51///Encryption Key

///</summary>

The private string encryptkey= "Fanmenglife";

/**////<summary>

55///Decryption Key

///</summary>

The private string decryptkey = "Fanmenglife";

/**////<summary>

59///Hint Information

///</summary>

Notemessage=null private string;

#endregion

63

64 Public Properties #region Public properties

/**////<summary>

66///Input String

///</summary>

The public string inputstring

69 {

Get{return inputstring;

Set{inputstring=value;}

72}

/**////<summary>

74///Output String

///</summary>

Outstring public string

77 {

Get{return outstring;

Set{outstring=value;}

80}

Bayi/**////<summary>

82///Input File path

///</summary>

Inputfilepath public string

85 {

Get{return Inputfilepath;

Set{inputfilepath=value;}

88}

/**////<summary>

90///Output file path

///</summary>

Outfilepath public string

93 {

Get{return Outfilepath;

Set{outfilepath=value;}

96}

/**////<summary>

98///Encryption Key

///</summary>

Encryptkey public string

101 {

102 Get{return Encryptkey;}

Set{encryptkey=value;}

104}

/**////<summary>

106///Decryption Key

///</summary>

108 public String Decryptkey

109 {

Get{return Decryptkey;

Set{decryptkey=value;}

112}

113/**////<summary>

114///Error message

///</summary>

116 public String Notemessage

117 {

118 Get{return Notemessage;}

119 Set{notemessage=value;}

120}

121 #endregion

122

123 constructor #region Constructor function

124/**////<summary>

125///Constructors

126///</summary>

127 Public Desencryptor ()

128 {

129//

130//TODO: Add constructor logic here

131//

132}

#endregion

134

135 des encrypted string #region des encrypted string

136/**////<summary>

137///Encrypted string

138///Note: The key must be 8 bits

139///</summary>

140 public void Desencrypt ()

141 {

Byte[] Bykey=null;

143 byte[] iv= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};

144 Try

145 {

146 Bykey = System.Text.Encoding.UTF8.GetBytes (this.encryptKey.Substring (0,8));

147 DESCryptoServiceProvider des = new DESCryptoServiceProvider ();

148 byte[] Inputbytearray = Encoding.UTF8.GetBytes (this.inputstring);

149 MemoryStream ms = new MemoryStream ();

CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (Bykey, IV), cryptostreammode.write);

151 CS. Write (Inputbytearray, 0, inputbytearray.length);

152 CS. FlushFinalBlock ();

153 This.outstring=convert.tobase64string (Ms. ToArray ());

154}

catch (System.Exception error)

156 {

157 This.notemessage=error. message;

158}

159}

160 #endregion

161

162 des decryption string #region des decryption string

163/**////<summary>

164///Decryption String

165///</summary>

166 public void Desdecrypt ()

167 {

byte[] Bykey = null;

169 byte[] iv= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};

170 byte[] Inputbytearray = new Byte[this.inputstring.length];

171 try

172 {

173 Bykey = System.Text.Encoding.UTF8.GetBytes (decryptkey.substring (0,8));

174 DESCryptoServiceProvider des = new DESCryptoServiceProvider ();

175 Inputbytearray = convert.frombase64string (this.inputstring);

176 MemoryStream ms = new MemoryStream ();

177 CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (Bykey, IV), cryptostreammode.write);

178 cs. Write (Inputbytearray, 0, inputbytearray.length);

179 CS. FlushFinalBlock ();

180 System.Text.Encoding Encoding = new System.Text.UTF8Encoding ();

181 this.outstring=encoding. GetString (Ms. ToArray ());

182}

183 catch (System.Exception error)

184 {

185 This.notemessage=error. message;

186}

187}

188 #endregion

189}

Encrypt, decrypt WebService, call the tool encryption, decryption method:

1/**////<summary>

2///Summary description for Service1

3///</summary>

4 [WebService (Namespace = "http://tempuri.org/")]

5 [webservicebinding (ConformsTo = wsiprofiles.basicprofile1_1)]

6 [System.ComponentModel.ToolboxItem (false)]

7//To allow this Web Service to is called from script, using ASP.net AJAX, uncomment the following line.

8//[System.Web.Script.Services.ScriptService]

9 public class Service1:System.Web.Services.WebService

10 {

One/**////<summary>

12///Encryption

///</summary>

///<param name= "url" > Address </param>

///<returns></returns>

[WebMethod]

The public string Encrypt (string url)

18 {

return Desencryptor.execencrypt (URL);

20}

21st

/**////<summary>

23///Decryption

///</summary>

///<param name= "url" > Address </param>

///<returns></returns>

[WebMethod]

public string deencrypt (string url)

29 {

return Desencryptor.execdeencrypt (URL);

31}

32

}

When the MediaPlayer control plays the page load, the WebService encryption method is invoked to encrypt the playback address, and then the JavaScript script is registered to invoke the video file playback method:

Play page load

protected void Page_Load (object sender, EventArgs e)

{

if (! IsPostBack)

{

Encrypt the playback address

String Encrypturl = Local. Encrypt (Playurl);

Registering scripts, calling JavaScript playback methods

Page.ClientScript.RegisterStartupScript (this.               GetType (), "" "," settimeout (\ "Onplay (' + encrypturl +") \ ", 1000)", true); }

}

The JavaScript call WebService is implemented through Microsoft's WEBSERVICE.HTC, and using WEBSERVICE.HTC in asp.net requires adding the following code to the HTML code:

<div id= "Servicediv" style= "Behavior:url (WEBSERVICE.HTC)" ></div> Note: The div must be outside of forms

JavaScript code:

1 function getUrl (str) {

2//The first parameter is the URL of the webservice, followed by the name

3 Servicediv.useservice ("Http://localhost:88/Service1.asmx?WSDL", "News");

4//Set a callback function, the service returns the result of the callback; The first parameter is the name of the callback function, followed by the WebService parameter

5 Intcallid = ServiceDiv.news.callService (Url_result, "Deencrypt", str); Here are two parameters.

6}

7

8 function Url_result (Result)//callback functions

9 {

Ten var media_player = $find (' Myusermediaplayer_player ');

One media_player.set_mediasource (result.value);

Media_player.set_autoplay (TRUE);

13}

14

function Onplay (str) {

GETURL (str);

17}

There are several points to note:

1. Why the registration script in the Page_Load event does not call the JavaScript playback method directly, because the page needs to be fully loaded, the WEBSERVICE.HTC will be initialized, so I use the SetTimeout method to delay a period of time to invoke the playback method.

2. When using the WebService callback function, remember to judge if the return result is successful.

By using the above method, you cannot see the playback file address on the page by viewing the source file, and the MediaPlayer initialization statement is updated as follows:

MediaPlayer control initialization after change

Sys.Application.add_init (function ()

{

$create (Sys.UI.Silverlight.MediaPlayer,

{"AutoPlay": true, "ScaleMode": 1, "source": ". /usercontrol/playertheme/vipplayer.xaml "," Volume ": 1},

NULL, NULL, $get ("ump_player_parent"));

});

But this is only for the general user, if we have a better way to share, I hope my article for everyone to help







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.