§ September 28, 2006

Triple DES between PHP and C#

The past while at work I've been working through some annoyingly overcomplicated encryption issues. The problem is not that Triple DES is all that complicated or annoying, it's just that when you have 2 different technologies (one doing the encrypting and one doing the decrypting) at work; it can be frustrating to get anything accomplished.

Our issues stemmed around the fact that we were using .NET 1.1 and they were using Java (with the standard crypto providers). There are a few subtleties between Microsoft's crypto providers and Java's. .NET provides 3 (ok, really only 2) Padding modes, and Java provides like 5, but they don't provide any in common. One easy padding mode is to append 0x00 bytes to the end of the final block to make it an even 64 bits wide. The Java provider didn't have this, but its easy enough to add ( by appending null char's '\0' to the end of the string then calling getBytes() ). So that's the route we took. After overcoming a few problems with "How do we encode our bytes used for the key and IV and still be compatible" we were off and running.

That made me wonder how compatible PHP and .NET's 3DES were. Since I didn't see any "Padding Mode" for PHP's, I simply hand coded the padding the same way we did with the Java code.

Here's my php script:    Read the rest of this article... (676 words)
Article Sections :: C # Articles :: PHP Articles

  § July 3, 2006

PHP Webservices and C# / .NET SOAP Clients

Code Zulu Bind Maker is a project I started working on about 3 years ago. Its a stupid little app that configures game settings for Counter-Strike. When I wrote it, version 1.6 was in beta, and everybody played version 1.5 (which was ever so different). Anyway I run a website that supports the app, and of course, its written in PHP / MySql (my poison of choice on the web).

The reason for the boring and seemingly pointless background is that ever since I wrote the app, I always wanted to integrate the website and application, but never really thought of a "good" way to do it. A couple days ago, I had some spare time and sat down to see how easy it would be to create some PHP webservices that I could easily consume in C#. I had been considering expanding the community aspect of the bind maker website into the application so that they'd work seamlessly together. I also wanted to add a common place that gaming communities could spend their time (which I had previously planned for just the website, but was thinking of the possibilities of bringing that into the app as well), and a PHP webservice combined with a C# windows app seemed like the best option.

C# webservices (server side) are about as easy-as-it-gets to write. C#'s SOAP client is completely transparent (or I should say completely generated so you code just like you would use any other objects) and is completely brainless to use. Since I'd never done webservice work in PHP, I started asking Mr. Google how it worked, and what would you now but that I stumbled across the NuSOAP library for php. It sure beat the heck out of writing all my WSDL files by hand, wrapping and unwrapping stupidly simple SOAP messages, and hacking everything very poorly.

My first PHP webservice iteration was a simple "hello <name> " web method. It used a simple type (xsd:string) as its parameter and returned a simple type (again another xsd:string) as its return value and looked like this:
 /**
 * ProcessSimpleType method
 * @param string $who name of the person we'll say hello to
 * @return string $helloText the hello  string
 */
function ProcessSimpleType($who) {
	return "Hello $who";
}
NuSoap was great. It took care of all the nitty-gritty work that I had previously done myself. All it took was about 8 lines of code to set up the SOAP server and register the method and my types.
require_once("lib/nusoap/nusoap.php");
$namespace = "http://sanity-free.org/services";
// create a new soap server
$server = new soap_server();
// configure our WSDL
$server->configureWSDL("SimpleService");
// set our namespace
$server->wsdl->schemaTargetNamespace = $namespace;
// register our WebMethod
$server->register(
                // method name:
                'ProcessSimpleType', 		 
                // parameter list:
                array('name'=>'xsd:string'), 
                // return value(s):
                array('return'=>'xsd:string'),
                // namespace:
                $namespace,
                // soapaction: (use default)
                false,
                // style: rpc or document
                'rpc',
                // use: encoded or literal
                'encoded',
                // description: documentation for the method
                'A simple Hello World web method');
                
// Get our posted data if the service is being consumed
// otherwise leave this data blank.                
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) 
                ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';

// pass our posted data (or nothing) to the soap service                    
$server->service($POST_DATA);                
exit();
That was it. From visual studio I was then able to consume the webservice by adding a web reference

Add A Web Reference

This article was first written with visual studio 2005. Visual studio 2008 and 2010 no longer have the "Add Web Reference" menu item. Instead they have an option to add a service reference. Selecting this option ends up generating a WCF C# client proxy, but we'll want to use the asmx style web service client proxy, so we'll navigate to the add web reference dialog which is hidden deep inside Add Service Reference dialog.

Visual Studio 2008 and 2010 Only
Right click on the references folder, and select Add Service Reference

Add A Service Reference


Click on the advanced button

Advanced

Click Add A Web Reference

Finally click the Add Web Reference button

Add A Web Reference Dialog

Add A Web Reference

With the reference added, I put a button on my form, and added the following button click event handler:
    Read the rest of this article... (928 words)
Article Sections :: C # Articles :: PHP Articles

  § September 21, 2005

A Battlefield 2 Rank Avatar Script in PHP

This is a little departure from my typical topic.

To cut a rather tedious and boring story short, I play Battlefield 2, which has a cool stats service that folks have been hacking with outside of the game (the game has a stats board built in, which stats board uses some pseudo web-services to obtain game statistic info).

There are a multitude of server hosts out there that generate dynamic signature images with your game stats in them, so that you can look cool and show off how much time you actually waste online gaming, on every forum you frequent.

I decided to join the masses and write a script that pulls my users rank and score from the stats servers and returns an image that can be used as an avatar on most bulletin board systems out there. I had a size restriction on the server I wanted it for, so the images produced are 80x80 and less than 6k in size.

Using the avatar script is pretty easy, you direct the image source url to the server where you install the script (or you can just use the one here)

html
<img src="http://www.sanity-free.org/bf2rank/?pid=45647367">

bbCode
[img]http://www.sanity-free.org/bf2rank/?nick=[OinK]_MadHatter[/img]



most bulliten board systems have places for you to link in a remote avatar, and one would add this same url w/ a name just as you would a normal image.

One annoying thing about most bbcode systems is that they require a valid image extension (.png, .jpg, .gif ... ) on the url inside the [img][/img] blocks (otherwise it just shows text inside the img blocks). To get your rank image to show up if you're having troubles is to simply add an &avatar.jpg to the end of the url. The php code will ignore it, and the forum will think its a valid image and will show it correctly. Another way is to use the simple url described in the forums here.

The resulting image looks like this:

MadHatter's Battlefield 2 Rank Avatar

You can also specify a couple of options.
  • adding &score will display your global score at the top of the image.
    ex: &score Example
  • adding &no_text to the end of the url will keep the text from being written on the image.
    ex:&no_text Example
  • adding &delta to the end will show the difference in your progress between your last rank and your next one.
    ex: &delta example
  • you can also combine any combination of them as well (this one is using &delta, &no_text, and &score options).
    ex: &delta and &no_text and &ampscore options

EDIT Feb. 15 2006: since the last patch, I havent updated this article source code. HERE'S the most recent version of the script. you can download the package and copy that over the script provided.

EDIT March 20 2006: I updated the code to include nick name resolution (using searchforplayers.aspx). the link to the code above has that change in it. so you can use your player nick again instead of trying to find out your player ID.

EDIT June 30 2006: I removed the source code from the article and repacked the download with everything you need.

EDIT July 18 2006: I've added "easy url's" for the rank avatars. for more info on that--and if you'd like to add them to your server--take a look at the thread in the forums here.

Here's the code for the script (the download has a true type font that needs to be located in the script dir w/ all the image files which are also referenced, so make sure to download the script pack):


Download source and resources here.


Article Sections :: PHP Articles
© 2003 - 2024 NullFX
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License