November 01, 2008

Low level encryption in PHP

I was creating this site ih PHP where in there are lots of configuration details. Details like DB username and password, site theme information, etc. The site would be installed and used by an unknown person who would become the site admin. Now, if the admin is not so computer savvy and wanted to change some of the configurations, he could easily edit the configurations which could screw up several things. To correct the data, the admin will have to reinstall the whole site. In order to prevent this from happening, I wanted to encrypt the config data.

I used a simple conbination of hex conversion and binary packing to do this. Here is the code I used for this:


function hex2bin($h)
{
if (!is_string($h)) return null;
$r='';
for ($a=0; $a<strlen($h); $a+=2)
{
$r.=chr(hexdec($h{$a}.$h{($a+1)}));
}
return $r;

}

$config_data = file ("normal.txt");
$CFGSTR = "DO NOT EDIT THIS FILE";
foreach ($config_data as $n_str)
{
$hex_str = bin2hex ( $n_str );
$bin_str = pack ('h*', $hex_str);
$CFGSTR = $CFGSTR."\n".$bin_str;
}

$fp = fopen ("club.cfg", "w+");
fwrite ($fp, $CFGSTR, strlen ($CFGSTR));
fclose ($fp);

$cfg_data = file ("club.cfg");
echo "<hr>CONFIG DATA: <br>";
for ($i=1; $i<23; $i++)
{
$cfg_arr = unpack ('h*',$cfg_data[$i]);
$cfg = "";
foreach ($cfg_arr as $c)
$cfg = $cfg.$c;
$cfg = hex2bin ($cfg);

echo "$cfg <br>";
}


A brief explanation on the above code: The normal.txt file contains the ACSII data. This would be converted to a non-human readable format and stored in club.cfg file. My site is a club of sorts, so the name "club". Anyway, the first part of encryption is to convert the data to hex. Then pack it to a binary format. The reason why im converting it to hex first is to make the code platform independent. I am storing each configuration detail in a seperate line. So I can reference the data by the line number. The second foreach loop reads thils encrypted file and displays the data. The hex2bin function was created by someone else. (Ref + credits: http://us.php.net/bin2hex).

So a simple conbination of hex conversion and binary packing is good enough to create and easy low level data encryption in php.

Peace ..V,

October 18, 2008

Color chooser with PHP

I made this color chooser in PHP which is a rather simple code. It displays a table of known colors with RGB values ranging from 0 to 255. The table cell onclick property is used to redirect back to the same page by passing the color value through GET.

Here is the code:


<table cellspacing="0" cellpadding="0">

<?php
$inc = 255/5;
for ($r=0; $r<=255; $r+=$inc)
{
echo "<tr>";
for ($g=0; $g<=255; $g+=$inc)
{
for ($b=0; $b<=255; $b+=$inc)
{
$cstr = sprintf ("%02X%02X%02X", $r, $g, $b);
echo "<td width="5px" height="5px" bgcolor="#$cstr"
onclick="window.location='hello.php?g_cstr=$cstr'">n";
echo "</td>n";
}
}
$cstr = sprintf ("%02X%02X%02X", $r, $r, $r);
echo "<td width="5px" height="5px" bgcolor="#$cstr"
onclick="window.location='hello.php?g_cstr=$cstr'">n";
echo "</td>n";
echo "</tr>";
}
?>
</table>

The color you picked was:
<table> <tr>
<?php
$sel_col = $_GET['g_cstr'];
echo "<td width="25px" bgcolor="$sel_col"></td>n";
echo "<td>$sel_col</td>";
?>
</tr> </table>



The output will be something like this:


Peace ..V,

September 16, 2008

Simple bar graph in java

Here's a simple program in Java that reads integer values from a file and graphs it. This program is written in three files. The code is shown below.

File: drawGraph.java


import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class drawGraph
{
public static void main( String args[] )
{
JFrame frame = new JFrame( "Graph" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

CreateGraph createGraph = new CreateGraph();
createGraph.setBackground( Color.WHITE );
frame.add( createGraph );
frame.setSize( 640, 480 );
frame.setVisible( true );
}
}


File: Create Graph.java


import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class CreateGraph extends JPanel
{
public int x (int tx)
{
return (tx + 5);
}

public int y (int ty)
{
return (400 - ty + 5);
}

public void paintComponent( Graphics g )
{
super.paintComponent( g ); // call superclass's paint method
this.setBackground( Color.WHITE );

GraphComponents graphComponents = new GraphComponents();
graphComponents.openFile();
graphComponents.readRecords();
graphComponents.closeFile();

g.setColor( Color.BLACK );
g.drawLine( x(0), y(0), x(620), y(0) );
g.drawLine( x(0), y(0), x(0), y(460) );

g.setColor( Color.BLUE );
for (int i = 0; i < graphComponents.getMaxYCount(); i ++)
{
g.fillRect( x(i*50+10), y(graphComponents.getYValues(i)*20),
40, graphComponents.getYValues(i)*20 );
}


File: GraphComponents.java


import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class GraphComponents
{
private Scanner input;
private int yValues [];
private int yCounter;

public GraphComponents ()
{
yValues = new int [10];
yCounter = 0;
}

public void storeYValues (int val)
{
yValues [yCounter++] = val;
}

public int getYValues (int index)
{
return yValues[index];
}

public int getMaxYCount ()
{
return yCounter;
}

public void openFile()
{
try
{
input = new Scanner( new File( ".\\graphvalues.txt" ) );
yCounter = 0;
}
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( "Error opening file." );
System.exit( 1 );
}
}

public void readRecords()
{
try
{
while ( input.hasNext() )
{
storeYValues (input.nextInt());
System.out.println (yValues[yCounter - 1]);
}
}
catch ( NoSuchElementException elementException )
{
System.err.println( "ERROR: Unable to read file (No such element)" );
input.close();
System.exit( 1 );
}
catch ( IllegalStateException stateException )
{
System.err.println( "ERROR: Unable to read file (State exception)" );
System.exit( 1 );
}
}

public void closeFile()
{
if ( input != null )
input.close();
}
}


Peace ..V,

September 13, 2008

Creating round corners using CSS

In a website, menus and small rectangular regions with round corners look impressive. It can be seen in several sites. But as a web designer I found that creating round corners are not as easy as it looks. Here is one method I found to make things easy:

First a preview of that you're gonna read about:



The HTML code:

<div class="windowFrame">
<div class="windowTopBar">
<img src="Images/cTL.png" class="imgL" />
<img src="Images/cTR.png" class="imgR" />
</div>

<div class="windowContentFrame">
Lorem ipsum... and other similar crap!
</div>

<img src="Images/cBL.png" class="imgL" />
<img src="Images/cBR.png" class="imgR" />
</div>


The CSS used along with this is:

.windowFrame {
width: 100%;
background-color: #BBBBFF;
margin-bottom: 10px;
text-align: center;
}
.windowTopBar {
width: 100%;
background-color: #0000FF;
height: 25px;
}
.windowContentFrame {
padding: 10px;
text-align: left;
}
.imgL {
float: left;
}
.imgR {
float: right;
}




The images cBL, cBR, tTL, cTR refer to the four corners. The size of these should be atleast 15px X 15px. Otherwise there will some strange vertical alignment problems. They are images that are rounded at the corners that will be placed at each corner. Here's a sample:






I've used .png images as they allow transparency. The above corners are perfect for a white background. Oh yes, the images must match the background. Should be careful about that. You can save those images and use it if you need them. They're not copyrighted... ;)

The above method can also be modified to include all the images in the CSS and not have any img tag at all. The advantage of doin this is the images will be at the background. When you try to select contents of the page, the corners will not get selected. In better words, the corners will look more merged with the background. But another disadvantage of this is you'll have to have 4 div tags and 4 classes. But the final output is the same.

CSS rocks!

January 06, 2008

Introduction to Flash

Lately I've been addicted to flash. Its like I feel flash is the answer for everything. My first work-study project was to design a web-site. That's when the craze hit me. I first thought of doing and ASP.net site, but i realized its just a dumb site and there aren't any serverside functions to worry about. All I needed was a nice and flashy look that conveys the message.

Flash to the rescue.

Flash uses vector graphics and it is a very convenient tool for vector based animations. I first thought it might have a lot of support for drawing, but it doesn't. To get graphics into flash, you need to do it through photoshop (my all time favourite). So once you've done drawing something in photoshop just import it to your library or to the stage in Flash (Im using Flash MX, but it really doesn't matter).

In flash layers matter a lot. In fact its a good practice to make use of layers in everything. Both Flash and photoshop. Create distinct parts of whatever it is that you're doing on different layers and name those layers. Definitely helps when you're doin something big.

Once you've got all your images on the stage, its time to tween them and move them and make components out of them. The graphics part in flash is just one half of it. The second half is the actionscripting part. This is a lot like javascript. Also flash is event based - functions are called when an event occurs.

This is my introduction to flash. Also Its good to have dreamweaver 8.0.2 update if you want to use flash with decently in IE. IE has some irritating properties (Click to activate this control thing) and we need to use a bit of javascript to overcome this. The dreamweaver update does this automatically.

Im still a newbie in Flash. And I'll post whatever I learn on this blog. Hope it helps.