Announcement

Collapse
No announcement yet.

A PHP Class to encrypt and decrypt from Blowfish AMS functions.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • A PHP Class to encrypt and decrypt from Blowfish AMS functions.

    It's just a sugestion...

    Trying to convert this into opensssl but not having much success, if anyone as any idea would be nice....

    Code:
    <?php
        //Pads data to mod 8 bytes, PKCS#5 style
        function pad($data)
        {
            $padlen = 8-(strlen($data) % 8);
            for ($i=0; $i<$padlen; $i++)
                $data .= chr($padlen);
            return $data;
        }
        //Unpads data (and checks if pad is valid)
        function unpad($data)
        {
            $padlen = ord(substr($data, strlen($data)-1, 1));
            if ($padlen>8)
                return $data;
              
            for ($i=strlen($data)-$padlen; $i<strlen($data); $i++) {
                if (ord(substr($data, $i, 1)) != $padlen)
                return false;  
            }
            return substr($data, 0, strlen($data)-$padlen);
        }
        //Swaps byte order (little-endian <-> big-endian)
        function swap($data)
        {
            $res="";
            for ($i=0; $i<strlen($data); $i+=4) {
                list(,$val) = unpack('N', substr($data, $i, 4));
                $res .= pack('V', $val);
            }
            return $res;
        }
    class Framework
    {
        protected $data;
        protected $key;
        public $result;
        public function __construct(){
        }
        public function encr($data){
            $key = 'TrustNo1WithThisP@$$wordHackers!';
            $result = swap(@mcrypt_encrypt(MCRYPT_BLOWFISH, $key, swap(pad($data)), 'ecb'));
            return base64_encode($result);
        }
        public function decr($data){
            $key = 'TrustNo1WithThisP@$$wordHackers!';
            $temp = base64_decode($data);
            $result = unpad(swap(mcrypt_decrypt(MCRYPT_BLOWFISH, $key, swap($temp), 'ecb')));
            return $result;
        }
    }
    // ********
    //  USAGE
    // ********
    $test = Framework::encr('My Test');
    echo $test;
    echo '<br/>';
    echo Framework::decr($test);
    ?>​
Working...
X
😀
🥰
🤢
😎
😡
👍
👎