Çarşamba, Şubat 23, 2011

Getting along with JQuery,Validation,Masked Input

JQuery is a nice library with whom I havent played till yesterday. Since yesterday I am struggling with this nice library, actually not solely with it but with validation plugin. That plugin is nice too but when it comes to integrating it with another plugin which you guess Masked Input, I was having problems. Because my form is dinamic and I am adding elements whose names are the as previous ones, I have struggled a lot. But at the end I did what I wanted to do. This

jQuery.validator.addMethod("acceptreg", function(value, element, param) {
  return value.match(new RegExp("^" + param + "$"));
});

small code piece helped me a lot when validating text and phone fields and their integration with Masked Input. I know validation plugin normally does validate against phone but my case was different. I declared such a method as described above and used it inside with

tel: {
required: true, minlength:11, maxlength:11,acceptreg: "[0-9]{3}-[0-9]{7}"
},

I did some dom manipulation with dom adding and removing dom elements too.I struggled a lot but after I am pleased to work deeper with javascript and JQuery.

 

Salı, Şubat 15, 2011

Centos mcelog reports incorrect record size

On a centos 5.5 64 bit server I was getting mcelog errors from cron.hourly/mcelog.cron stating

"/etc/cron.hourly/mcelog.cron:

mcelog: warning: record length longer than expected. Consider update"

As a workaround I did add 2>&1 to the last line of /etc/cron.hourly/mcelog.cron file as mentioned in Known Issues in this page

http://elrepo.org/tiki/kernel-ml

 

 

Centos Sendmail Problem

On a centos 5.5 server I was getting sendmail errors which I didnt want to try to solve. Then I installed postfix as an alternative and it does the job now.

 

Perşembe, Şubat 03, 2011

An Encryption Method

Lately I have been told to make a new encryption method as an assignment. The method should be not preused and tried before. So it was me who is supposed to invent a new encryption method.

I have thought of Rubik's cube and placing the letters of the alphabet on this cube. Then the coordinates of each letter would be send as ciphertext. Thinking on it lead to some diffucilties. So I decided to

take another way. I believe that a good  encryption technique is still available by using Rubik's cube but for now I quitted. Instead, I thought of 3x3 or 5x5 squares on which we place the letters beginning from

an arbitrary letter but obeying the order. So here is an example with a 3x3 square

 

2HIJ
1KLM
0NOP
 012

With such a layout we represent each letter on this square using the pivot letter (lets say) L

H=L02   I=L12   J=L 22   K=L01  L=L11  M=L21 etc.  On a 5x5 square we will have similar representations of letters.

Even one can generalize this to an (2n+1)x(2n+1) square.

So as understood, when we change the pivot letter, the encryption changes. So a letter will have many encryptions.

As a rule for this method of encryption, if we used a 5x5 square then we need to declare it after the pivot letter.

So in that case say your encryption would look like M=K531  where K is the pivot letter. But if we used a 3x3 square then

we omit and simply write as shown above.

As another rule of this method of encryption, if a pivot table is used more than one successive encrypitions then we dont

declare each time the pivot letter and instead write like this K5311203  which when decrypted says we used a K pivotted 5x5 square

and the coordinates of 3 encrypted letters are 31,12,03 according to this square.

Here is an encryption of message "TOPSECRET"

R542R5430232G22I5131033P20.

Here are others T11J52131W533L503343103Y523, P20N532Z52403C00F12O10D21T11, Q10O112120N534L534U12B501V22 etc.

The restrictions to this method is that we can only use capital English letters and nothing even a space is not supported.

I have implemented a php script to encrypt and decrypt messages sent by this way.

Here is the script.

<?php
//Bu Program Ingiliz alfabesindeki buyuk harflerle yazilmis bosluksuz bir metni sifreler ve cozer.
//Programda phpnin chr ve ord fonksiyonlari kullanilmak istendigi ve kolay olmasi icin sadece buyuk harfler
//secildi.Aslinda diger harf ve karakterler array kullanilarak istenildigi gibi sifrelenebilir.
//Programda boslugu eklemek program bitince aklima geldi.Eklemek de vakit alacagi icin yapamadim.
//O yuzden su anda bosluksuz metinleri sifreliyor.
//Semalarimiz 3x3 ve 5x5 boyutlarinda.Aslinda genellestirilebilir.
//Kullanilan sema 5x5 boyutunda ise ciphertexte 5 ekleniyor.3x3 ise  hicbir sey eklenmiyor.
//Pivot degismedi ise ciphertexte yazilmiyor.Sadece koordinatlar ekleniyor.
//Sifreleme yontemi hic bir yerden alinti degildir.
//Koordinat bulmak icin gerekli arrayleri elle olusturdum.Size daha once gonderdigim word dosyasindaki koordinat bulma
//formullerini dogru ama eksiklik var.Array varken algoritma yazmak da gereksiz diye dusundum.
//Ornegin $arr3=array(-4=>"02",-3=>"12",-2=>"22",-1=>"01",0=>"11",1=>"21",2=>"00",3=>"10",4=>"20");
//arrayinde 02 koordinatindaki bir harf 3x3luk semada pivot harften 4 onceki harftir(ASCII sirasinda gore)
//Yazan Sinan Iyisoy

function pivotbul($sifrelenecek,$sema)
{
//Sifrelemede kullanmak icin bir pivot bulur.Pivotun uygun sartlarda secilmesi gereklidir.
//Semanin boyutu 3 ise Sifrelenecek olan harfin pivot harfe uzakligi -4 ve 4 arasinda olmalidir
//Semanin boyutu 5 ise bu uzaklik -12 ve 12 arasinda olmalidir.Aksi halde harf karenin disinda kalir.
//O yuzden uygun pivot bulana kadar doner.

    if($sema==3) $uzaklik=4;
    if($sema==5) $uzaklik=12;

    do
    {
    //A ile Z arasinda bir harf secelim. A nin ASCII degeri 65, Z nin 90.
    $pivot=rand(65,90);
    }
    while(abs(ord($sifrelenecek)-$pivot)>$uzaklik);
    // Aradaki fark uzakliktan buyuk olmamali.
    return $pivot;

}


function koordinatbul($fark,$sema)
    {
//Ascii degerleri arasindaki farka bakarak ciphertextin semadaki koordinatlarini bulur.
//Bu fonksiyon encrypt etmede kullanilir.
//Farka bakarak harfin semadaki koordinatlarini bulalim.
    $arr3=array(-4=>"02",-3=>"12",-2=>"22",-1=>"01",0=>"11",1=>"21",2=>"00",3=>"10",4=>"20");
    $arr5=array(-12=>"04",-11=>"14",-10=>"24",-9=>"34",-8=>"44",-7=>"03",-6=>"13",-5=>"23",-4=>"33",-3=>"43",
    -2=>"02",-1=>"12",0=>"22",1=>"32",2=>"42",3=>"01",4=>"11",5=>"21",6=>"31",7=>"41",8=>"00",9=>"10",10=>"20",11=>"30",12=>"40");
    if($sema==3) $koordinat=$arr3[$fark];
    if($sema==5) $koordinat=$arr5[$fark];

    return $koordinat;
    }



function encrypt($string)
{
//Bu fonksiyon girilen metni sifreler.
//Sifreleme icin once bir pivot ve sema secer.Bu sema ve pivotun olusturdugu semada harfin koordinatlarini ciphertexte ekler.
//Metni sifrelerken zar atar.Eger zar 1 ise yeni bir pivot harf belirler.Ve sifrelenecek metni ona gore sifreler.
//Eger zar 2 ise ve sartlar uygunsa eski pivota gore sifreler.Sartlar uygun degilse yeni pivot secer.
//Default sema 3 tur.Eger sema 3 ise bu bilgiyi ciphertexte yazmaz.5 ise yazar.
//Pivot degistiyse yeni pivotu ciphertexte ekler.Degismediyse eklemez.
$sifrelenmis="";
$pivot="";
$eskisema="";

for($i=0;$i<strlen($string);$i++)
    {


    //Sifrelenecek olan harf

    $harf=$string[$i];

    //Harfin ASCII degeri

    $ascii=ord($harf);
     

    $zar=rand(1,2);
    if($pivot!="")
        {
            if($zar==1)  //Zar bir ise yeni pivot isteyelim degilse eski pivotla devam
                {
                //Sifreleme icin pivot harfi bulalim
                $sema=rand(1,2)*2+1;
                $pivot=pivotbul($harf,$sema);
                $pivotekle=true;  //$pivotekle yeni pivot secildigi icin yeni pivotu ciphertexte ekler
               
                }
            else
            {
                // Yeni pivot secmedik.Ama $farki kontrol etmeliyiz.Aksi halde harf karenin disinda olur.Karenin disindaysa yeni pivot seciyoruz.
                if(($sema==3 && abs($pivot-$ascii)>4) || ($sema==5 && abs($pivot-$ascii)>12))
                {
                $sema=rand(1,2)*2+1;
                $pivot=pivotbul($harf,$sema);
                $pivotekle=true;
                }
                else
                {
                $sema=$eskisema;  //pivot degismedi.Sema da degismemeli.
                $pivotekle=false;
                }
               
            }
        }
    else //Hic pivot yoksa bir pivot bulalim
        {
        $sema=rand(1,2)*2+1;
        $pivot=pivotbul($harf,$sema);
        $pivotekle=true;
        }

    $fark=$ascii-$pivot;
    $koordinat=koordinatbul($fark,$sema);
    if($pivotekle)
    {
   
    $sifrelenmis.=chr($pivot);
    if($sema==5)  //$sema 5 olunca 5 oldugunu belirtiyoruz.3 ise bir sey yapmiyoruz.
    {
    $sifrelenmis.=$sema;
    }
    }

    $eskisema=$sema;
    $sifrelenmis.=$koordinat;
    }

return $sifrelenmis;
}


function terskoordinatbul($tersfark,$sema)
    {
    //Koordinatlara bakarak ciphertext ile pivot arasindaki ascii deger farkini bulur.
    //Bu fonksiyon decrypt etmede kullanilir.
   
    $arr3=array("02"=>-4,"12"=>-3,"22"=>-2,"01"=>-1,"11"=>0,"21"=>1,"00"=>2,"10"=>3,"20"=>4);
    $arr5=array("04"=>-12,"14"=>-11,"24"=>-10,"34"=>-9,"44"=>-8,"03"=>-7,"13"=>-6,"23"=>-5,"33"=>-4,"43"=>-3,
    "02"=>-2,"12"=>-1,"22"=>0,"32"=>1,"42"=>2,"01"=>3,"11"=>4,"21"=>5,"31"=>6,"41"=>7,"00"=>8,"10"=>9,"20"=>10,"30"=>11,"40"=>12);
    if($sema==3) $koordinat=$arr3[$tersfark];
    if($sema==5) $koordinat=$arr5[$tersfark];

    return $koordinat;
    }



function decrypt($string)
{
//Bu fonksiyon cypertexti harf harf okuyarak decrypt yapar.
//Bu okuma sirasinda eger bulunan bir alfabetik harf ise bufferdaki sifreli metni cozer.
//Eger bulunan 5 ise sema degerini 5 yapar.
//Eger bulunan bunlarin harincindeyse buffera atar.
//Default sema degeri 3tur.

$buffer="";
$cozum="";
for($i=0;$i<strlen($string);$i++)
    {
    //Desifrelenecek olan harf

    $harf=$string[$i];
    $ascii=ord($harf);
    if($ascii>=65 && $ascii<=90)
    {
        if($buffer!="")  //Daha once pivot bulunmus,decrypt baslasin
        {
            if($sema=="") //Daha once $sema bulunmamis o zaman $sema 3
            {
            $sema=3;
            }
            for($j=0;$j<strlen($buffer);$j++)
                {
                if($j%2==0) //Ikiser ikiser rakamlari almaliyiz
                {
                $terskoordinat=$buffer[$j].$buffer[$j+1];
                $tersfark=terskoordinatbul($terskoordinat,$sema);
                $cozum.=chr($pivot+$tersfark);
                }
                }
            $buffer="";
        }
    $pivot=$ascii;
    $sema="";
    }
    elseif($harf==5)
    {
    $sema=5;
    }
    else
    {
    $buffer.=$harf;  //Eger okunan deger ne pivot ne de sema ise buffera atiyoruz.
    }
   
   
    }
   
   
    //Donguden kalan buffer icin decrypt yapmaliyiz.Dongu bitiyor ama islenmemis buffer kaliyor.
        if($sema=="") //Daha once $sema bulunmamis o zaman $sema 3
            {
            $sema=3;
            }
        for($j=0;$j<strlen($buffer);$j++)
            {
            if($j%2==0)
            {
            $terskoordinat=$buffer[$j].$buffer[$j+1];
            $tersfark=terskoordinatbul($terskoordinat,$sema);
            $cozum.=chr($pivot+$tersfark);
            }
            }
        $buffer="";
       
   
    return $cozum;

}


//Ornek kullanim

$r=encrypt("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
echo $r."\n";
echo decrypt($r)."\n";





?>