Labels

php (34) javascript (30) phpjavascript (29) jquery (23) html (19) mysql (14) database (9) codeigniter (4) json (4) bar chart (2) calendar (2) column chart (2) framework (2) google maps (2) query (2) tables (2) url (2) dropdown (1)

Tuesday, February 6, 2018

Encrypt a string with own encryption key using PHP

Data encryption is the more essential in the current world to maintain data privacy and to secure its form. Here we are providing a simple encryption method. Before explaining about coding will talk about Data Encryption.

Data encryption translates data into another form, or code so that only people with access to a secret key (formally called a decryption key) or password can read it. Encrypted data is commonly referred to as ciphertext, while unencrypted data is called plaintext. Currently, encryption is one of the most popular and effective data security methods used by organizations. Two main types of data encryption exist - asymmetric encryption, also known as public-key encryption, and symmetric encryption.
<?php
//sting which you want to encrypt
$str = 'http://www.phpjavascript.com/';
//the secryt key which encrypts the sting
$ky = 'ZCVGEGLLLS';

if ($ky == '') {
    echo $str;
} else {
    $ky = str_replace(chr(32), '', $ky);
    if (strlen($ky) < 8) {
        echo 'the key length should be more than 8';
    } else {
        $kl = strlen($ky) < 32 ? strlen($ky) : 32;
        $k = array();
        for ($i = 0; $i < $kl; $i++) {
            $k[$i] = ord($ky{$i}) & 0x1F;
        }
        $j = 0;
        for ($i = 0; $i < strlen($str); $i++) {
            $e = ord($str{$i});
            $str{$i} = $e & 0xE0 ? chr($e ^ $k[$j]) : chr($e);
            $j++;
            $j = $j == $kl ? 0 : $j;
        }
        echo $str;
    }
}
?>
Result:
rwbw?(#{{d4s~wofzmphjfs+dca#

No comments:

Post a Comment

More Posts

FOLLOW