Tuesday, July 15, 2014

Call WCF service using PHP

I have a WCF service used by a .net application. Due to some library issues I have to change .net application in to PHP language. Rather than creating a new web service, there is a simple way of accessing WCF using PHP. But there are few concerns. You cannot use WSHttpBinding with PHP. I have to change my bindings in to basicHttpBinding because PHP soap protocols are notsupporting wshttpbindings


<?php
$client = new SoapClient('http://188.188.188.188:8980/CalService/Service/?wsdl');
$params = new StdClass;
$params->value = 2;
$retval = $client->GetData($params);
echo $retval-> GetDataResult;
?>
// In echo $retval you need to mention your method name first and then Result.
//My service method
[OperationContract]
string GetData(int value);
// endpoint
<endpoint address="" binding="basicHttpBinding" contract=" ICalService">
view raw gistfile1.php hosted with ❤ by GitHub

Tuesday, March 25, 2014

Elliptic Curve Cryptography

Elliptic Curve Cryptography was proposed by Niel Koblitz and Victor Miller in 1985. This is based on algebraic of elliptic curves over finite fields and provided public key infrastructure to end user and it is considered as strong public key cryptosystem comparing with other algorithms like RSA. RSA is based on Integer Factorization Problem which means it is impossible or very difficult to find factor of very large prime numbers and that is an assumption. While ECC is based on elliptic curve discrete logarithm problem which means it is infeasible to determine discrete logarithm of point with multiplier in elliptic curve based on finite fields. Because of ECDLP feature elliptic curve cryptography can provide same security with small key size with related to other algorithms which can result speed computation with low memory, lower network bandwidth and lower power.  Most valuable feature in ECC is that it has highest strength per bit with related to other well-known cryptosystems. Therefore it is considered as high secure cryptosystem suitable for mobile devices.

The mathematical formula of ECC over the elliptic curve is
Y2 = X3 + aX + b

Where x, y, a and b are real numbers and with the condition
4a3 + 27b2 ≠ 0

By changing the values of ‘a’ and ‘b’ different elliptic curves can be generated. All the points which satisfy the above equation lie on the elliptic curve. Private Key is generated by random number generations while public key is obtained by multiplying the private key with a constant base point G (Scalar multiplication) in the curve. Public key is obtained as a point in this curve. ECC biggest advantage is its small key size, a 160 bit key size of ECC is equivalent in security to 1024 bit key size of RSA.

Implementation.

Tuesday, March 11, 2014

Clone objects C#

Cloning is a matter between copy object and copy its references. There are two types of cloning, deep clone and shallow clone. If you copy only references it’ll call shallow copy. If you copy referenced objects it’ll call deep copy.
Shallow copy:

Vehicle vehicleOne = new Vehicle();
Vehicle vehicleTwo = new Vehicle();
vehicleOne = vehicleTwo;
view raw gistfile1.cs hosted with ❤ by GitHub
Deep copy:
There are two ways of doing this.
1. Use the copy constructor (a good practice)
class Vehicle
{
// Copy constructor.
public Vehicle (Vehicle previousVehicle)
{
Name = previousVehicle.Name;
Year = previousVehicle.Year;
}
//// Alternate copy constructor calls the instance constructor.
//public Vehicle (Vehicle previous Vehicle)
// : this(previousVehicle.Name, previousVehicle.Year)
//{
//}
// Instance constructor.
public Vehicle(string name, int year)
{
Name = name;
Year = year;
}
public int Year { get; set; }
public string Name { get; set; }
}
// Create a Vehicle object by using the instance constructor.
Vehicle vehicle1 = new Vehicle("Toyota", 2014);
// Create another Vehicle object, copying vehicle1.
Vehicle vehicle2 = new Vehicle(person1);
view raw gistfile1.cs hosted with ❤ by GitHub
2. Using ICloneable interface
Manual way
public class Vehicle: ICloneable
{
public string Name;
public int Year;
public object Clone()
{
Vehicle v = new Vehicle ();
v.Name = this.Name;
v.Year = this.Year;
return v;
}
}
view raw gistfile1.cs hosted with ❤ by GitHub
Using memberwiseclone
public class Vehicle : ICloneable
{
public string Name;
public int Year;
public object Clone()
{
return this.MemberwiseClone();
}
}
//how to use
Vehicle toyota = new Vehicle();
Vehicle toyotaClone = (Vehicle)toyota.Clone();
view raw gistfile1.cs hosted with ❤ by GitHub