Friday, August 21, 2009

Encryption and Decryption Between .NET and PHP

I recently worked on a project that required encryption and decryption by and between .NET and PHP. By default, the 2 technologies don't mesh very well. Being that the data was originally being encrypted and decrypted by .NET, I had to write PHP code that worked with the encryption schemas being used. One of the main problems I ran into was the use of padding, in my case pkcs7 which was used by default in .NET. First thing to do was to make sure the encyption schemas were the same. For example, when using DES, the .NET default mode is MCRYPT_MODE_CBC.  Once that was setup, I could initialize the mcrypt libraries.


$module = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, '');
if($module === false)
die("DES module could not be opened");

$blockSize = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
 

The $blockSize variable is used later for padding and padding removal using pkcs7.  Next to encrypt data I had to implement the following:
 
//encryption
$key = substr($key, 0, 8);

$iv = $key;
$rc = mcrypt_generic_init($module, $key, $iv);
//apply pkcs7 padding
$value_length = strlen($value);
$padding = $blockSize - ($value_length % $blockSize);
$value .= str_repeat( chr($padding), $padding);
$value = mcrypt_generic($module, $value);
$value = base64_encode($value);
mcrypt_generic_deinit($module);

//value now encrypted


Basically, the encryption scheme the .NET side was using was set the iv to the key, pad data, encrypt data, then base64 encode data.  So here I've done the same thing in PHP.  Now I needed to do the exact same thing for decryption:


//Decryption
$key = substr($key, 0, 8);
$iv = $key;
$rc = mcrypt_generic_init($module, $key, $iv);
$value = base64_decode($value);
$value = mdecrypt_generic($module, $value);

//apply pkcs7 padding removal
$packing = ord($value[strlen($value) - 1]);
if($packing && $packing < $this->_blockSize){
    for($P = strlen($value) - 1; $P >= strlen($value) - $packing; $P--){
        if(ord($value{$P}) != $packing){
            $packing = 0;
        }//end if
    }//end for
}//end if

$value = substr($value, 0, strlen($value) - $packing);

mcrypt_generic_deinit($module);

//value now decrypted

This is basically the same as encryption but in reverse.  The only real difference is the pkcs7 padding removal.  Hopefully this tidbit helps a few others out there who run into encrypt and decryption issues between .NET and PHP.

Tuesday, August 18, 2009

Thursday, August 6, 2009

Gentoo Linux Resolving the Read Only NTFS Issue

If you run into the problem of mounting an NTFS share, drive, USB, etc. where the mount reads as (rw) but the "dmesg tail" gives you a message like the following:

NTFS-fs error (device sdxx): load_system_files(): $LogFile is not clean. Mounting read-only. Mount in Windows.

Here's the fix for enabling write functionality under Gentoo specifically and more than likely similar in other flavors as well.


First emerge ntfs3g
Next go to your kernel source fire off a make menuconfig and enable:


File systems --->
<*> Filesystem in Userspace support


or in later kernels


File systems --->
<*> FUSE (Filesystem in Userspace) support


Next do a make clean && make
If you're using modules then also do a make modules_install && update-modules.

Next mount your boot partition mount /dev/sdxx /boot (where sdxx is your drive specific device - in my case /dev/sda1)

Copy your new kernel into the boot partition (ie. cp arch/x86/boot/bzImage /boot/kernel)

Unmount the boot partition and reboot loading the new kernel.

Now to mount the NTFS partition with write support use the following:

mount -t ntfs-3g /dev/sdxx /mnt/xxx (where sdxx and xxx are your specific device and mount points - in my case /dev/sdc1 /mnt/usb)

You should be golden from this point forward. If you get an error stating that the drive is not clean or the like use the force option the clean it up mount -t ntfs-3g /dev/sdxx /mnt/xxx -o force.

Windows 7 Intermittent Freeze

So I posted earlier discussing the Windows 7 freezing issue and how updating the bios, etc. appeared to help. That is partially true. However, I'm still fighting the freezing issue. So far I've been able to track it down to non-user input. I can leave the machine alone at the login screen, no user interaction and the machine will still sometimes freeze. It looks like it may have something to do with a background process or an on idle process. I'll keep checking and update my findings.

Monday, July 27, 2009

Fixing The Annoying Internet Explorer 8 Dropdown Popup Blocker

So after using IE8 for about five minutes I found a super annoying feature that seems to cause the popup blocker to be invoked after clicking on any dropdown box. I began trying to fix this by adjusting the security settings (As Microsoft Suggests In the Information Bar), I literally tried every setting available and nothing would turn it off. Closing and re-opening browser instances, you name it, nothing worked. Eventually I found the fix and here it is.

Open Add/Remove Software -> Uninstall IE8

Fully uninstall IE8, ignore the warnings about "This program has dependencies, blah blah blah".

Once the uninstall is complete, do a full restart.

Now open the default IE version (should be IE6 if you're using XP), check the dropdown functionality on any webpage. Should work without displaying the popup blocker. Once that is confirmed, redirect your browser to:

http://www.microsoft.com/ie

Download IE8 and install. Once the install completes, reboot again. Once you've back up, launch IE8 and you should be prompted with the firstrun configuration wizard. Select "Custom Settings" not "Express Settings", proceed normally through the options selecting what you need.

Now go to any web page and check the dropdown functionality again. Should work just fine. I've done this on serveral machines now and this seems to be the fix.

Wednesday, July 1, 2009

Enlightenment E-17 On Gentoo The Easy Way

I'm an extremely heavy Linux user and I've used Enlightenment for quite some time, I recently downloaded the E-17 svn trunk and did a full build by hand. After spending the better part of a day building everything from hand, figuring out the correct order, fixing dependencies, etc. I finally got the new Enlightenment desktop up and running. It is absolutely beautiful, they're doing a great job. Along those same lines, I've recently found a script that makes installing E-17 a snap. Check out the script easy_e17.sh

http://exchange.enlightenment.org/application/show/134

This script makes short change of what took me the better part of the day and allows you to stay up to date with the latest fixes. Great work by morlenxus. The script is very easy to use, just download and execute:


chmod a+x easy_e17.sh
./easy_e17.sh -i


Then sit back and relax while your new desktop is downloaded, configured and built. The final binaries are installed at /opt/e17, so after compilation make sure you set you .xinitrc to "exec /opt/e17/bin/enlightenment_start"

Sunday, June 28, 2009

Watch Anime Online 2.0

Watch Anime Online has been upgrade to 2.0, in this version many many bugs have been fixed, the site has been layed out completely differently to address many of the issues people seemed to have been having when visiting the site. The site is much brighter with a much more fun color scheme, and the streamlining of the site has made the site performance much faster and easier to use. Come check out what Watch Anime Online has to offer and enjoy some anime while you're at it.

http://www.watchanimeonline.com/

Adventures In Windows 7

I recently decided to give Windows 7 a try. Being that it's not officially released yet, I expected to run into quite a few problems. Overall, most of the OS seems pretty solid. I did run into however, an issue that took me quite some time to overcome. Every once in a while the entire OS would just lock up or freeze. No input, no mouse movement, just frozen. I installed Windows 7 onto a machine with the following specs:

Fatality AN8 Sli motherboard
4 Gb of RAM
2.20 Ghz Socket 939 AMD 64 Athalon Processor
Realtek Sound Card
SATA 133 120Gb HD
2x 1024MB NVidia GeForce 9500 GT Graphics Cards


The freezing was fixed by going to the ABit site and downloaded the latest BIOS update for the motherboard. It appears that Microsoft has targeted it drivers to use only the latest BIOS. After than issue was resolved, there were no more freezes. Another odd thing to note though; after the BIOS update, my TI 1394 OHCI Compliant Host Controller stopped working. I wonder if the two issues are related? Perhaps there working are mutually exclusive.

The next issue, kind of small, but still an issue. Was getting the sound card to work properly along with the sound configuration dialogs, etc. My original approach was to download the Realtek drivers for Vista 64, but the configuration screens didn't work properly and I would get nonsense error messages (broken English). After uninstalling the Realtek Vista 64 drivers, I did a check on the Windows Update site. Behold, there were updated Realtek drivers there. Installed them and everything worked after that.

Overall, I like the powertools command prompt that now ships with Windows 7. The interface is very much like Vista for the most part which I find, OK. The control panel is pretty wack. I personally liked the Windows XP control panel much more, at least you had the option to change back and forth from new to old layout. In Windows 7, I don't see any options for doing that.

One thing that's not quite an issue, but it is a bit bothersome is the switch from SLI to non-SLI modes. After you launch the NVidia control panel, switch a configuration and hit "apply", you're going to be sitting there for a little bit with almost no visual feedback. Every once in a while the screen will flash, you might try to move the configuration window but then it will say "The window is not responding". Eventually it pulls itself out of it, but it's kind of annoying due to the fact that you don't really know what's going on until at the very end a dialog pops up and says "Do you want to keep these settings?".

Other than thoese issues, the OS seems pretty sturdy (as far as Windows goes). I haven't had any major crashes or issues since the BIOS update. Good work considering :)

Friday, May 1, 2009

Installing VMWare Server 2.0 On Gentoo Linux

Last time I installed VMWare server on Gentoo I had to download the installer directly from the VMWare site and do my best to mimic an rc0.d environment. To my suprise I found that there is now and ebuild available for it. So to get started type:

emerge vmware-server

One of the first problems I ran into was an error relating to a kernel configuration. This was fixed by going into the kernel and enabling:

Kernel Hacking -> Enable unused/obsolete exported symbols

I did a rebuild and a reboot, then continued with the emerge. Next issue was a an error relating to Wireless, struct something wireless member not found blah blah blah, basically just go back into the kernel and enable wireless, rebuild, rinse, repeat.

After that kernel change, the package finally installed itself correctly. Now all I had to do was run the post install perl script:

/opt/vmware/bin/vmware-config.pl

Follow the steps, enter your key, accept the EULA, blah blah blah.

After that, I needed to add the root user to the vmware authd group:

gpasswd -a root vmware

Next, make sure xinetd is configured correctly so that the vmware server console can talk to it. Edit the /etc/services file and remove the "only_from = " line, otherwise you'll only be able to access the vmware server from the host configured in that line.

Lastly, restart the xinetd service and make sure vmware is running:

/etc/init.d/xinetd restart

/etc/init.d/vmware status

Now we're golden :)