word = "Look at all the pretty Japanese index cards I have!"; $this->alg = MCRYPT_BLOWFISH; $this->mode = MCRYPT_MODE_CBC; } /* This functions takes the arguement and extracts, then decodes, and returns IV. This can only be used on data that has been encrypted using this class. */ function extract_iv($data) { echo "encoded IV and encrypted2: ".$data."
"; /* The data is decoded. */ $data = $this->decode($data); echo "decoded IV and encrypted2: ".$data."
"; /* Now the encoded IV is the first 20 characters of the decoded data string. So we extract those 20 characters. */ $ivraw = substr($data, 0, 20); /* The result is sent to the screen. This should be the same as the encoded IV. */ echo "encoded IV2: ".$ivraw."
"; /* It is then decoded to obtain the original and usable IV. */ $iv = $this->decode($ivraw); echo "IV: ".$iv."
"; return $iv; } /* This functions takes the arguement and extracts and decodes the IV and the encrypted data. It returns an array, the first of which is the IV and the second is the encrypted data. This can only be used on data that has been encrypted using this class. */ function seperate($data) { /* The extract_iv function is used to get the original IV. */ $iv = $this->extract_iv($data); /* The passed value first needs to be decoded. The extract_iv function did this to obtain the original IV. */ $data = $this->decode($data); /* The encoded data is the last 20 characters. So we extract starting on the 20th character all the way to the end of the string. */ $data = substr($data, 20); /* This is sent to the screen. This should be the encoded encryped data. */ echo "encoded encrypted2: ".$data; /* We decode it to convert it to the encrypted data and then return both the original IV and encrypted data. */ $data = $this->decode($data); echo "encrypted2: ".$data."
"; return array($iv, $data); } /* This functions takes the arguement and first serializes it and then takes that result and encodes it using base64_encode. It returns the result. */ function encode($data) { $a = base64_encode(serialize(stripslashes($data))); return $a; } /* This functions takes the arguement and first decodes it and then unserializes it. This is the complement for undoing data encoded using the class encode function. */ function decode($data) { $a = unserialize(base64_decode($data)); return $a; } /* This functions takes the key and data and encrypts them. First an IV is created. Then the data is stippred of slashes, and encrypted using the newly created IV. The key is the result of md5 hashing the combination of the $key string and the $word variable. The $word variable is a fixed phrase created and set at the creation of the instance of this class. The encrypted data and the IV are then serialized and encoded. Then they their strings are combined (IV coming first) and then that result is serialized and encoded. This is the final form. It returns the final form. */ function encrypt($key, $data) { /* The IV is created. */ $iv = mcrypt_create_iv(mcrypt_get_iv_size($this->alg, $this->mode), MCRYPT_DEV_URANDOM); /* The IV is then echoed to the screen. */ echo "IV: ".$iv."
"; /* mcrypt_encrypt is used with the alg being MCRYPT_BLOWFISH, the key is the md5 hash of the combination of the passed key variable and the $word variable, the slash-stripped form of the passed data, the mode is MCRYPT_MODE_CBC, and the IV is the created IV from above. */ $a = mcrypt_encrypt($this->alg, md5($key . $this->word), stripslashes($data), $this->mode, $iv); /* The encrypted result is then echoed to the screen. */ echo "encrypted: ".$a."
"; /* The encrypted form is then encoded using the encode($var) function of this class. It first serializes and then encodes using base64_encode. */ $a = $this->encode($a); /* The result of this is echoed to the screen. */ echo "encoded encrypted data: ".$a."
"; /* The IV is encoded using the class' encode function. */ $iv = $this->encode($iv); /* The result is echoed to the screen. */ echo "encoded IV: ".$iv."
"; /* The encoded IV and the encoded encrypted data is combined and then encoded using the class' encode function. */ $b = $this->encode($iv . $a); /* This is the final result and is echoed to the screen. */ echo "encoded IV and encrypted: ".$b."
"; return $b; } /* This functions is the complement of this classe's encrypt function. It takes the passed in data to be decrypted and first decodes and unserailizes it and then seperates it into the IV and data. The two are decoded and unserialized respectfully and used as the arguements for the mcrypt_decrypt. The key is the md5 hash of the given key combined with the $word variable. The $word variable is a constant phrase created and set at the creation of the instance of this class. */ function decrypt($key, $data) { /* The passed data is seperated into the IV and the encrpyted data. This is done by this class' seperate function. The IV and encrypted data are both also decoded and unserialized using this class' decode function. */ list ($iv, $data) = $this->seperate($data); /* The data is decrypted using the same values that it was encrypted by. The alg and mode are the same, the key is the same (md5 hash of the passed key and $word variable), and the data and mode are the same. */ $end = mcrypt_decrypt($this->alg, md5($key . $this->word), $data, $this->mode, $iv); /* The final result is dispalyed to the screen. This should display the original data. */ echo "unencrypted data: ".$end."
"; return $end; } } $key = "the key"; $data = "my name"; echo "Data to be encrypted: ".$data."
"; $en = new EncryptionType; $enc = $en->encrypt("the key", "my name"); $dec = $en->decrypt("the key", $enc); echo "




"; $a = "my name"; echo "my name
"; $newiv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM); echo "IV: ".$newiv."
"; $b = mcrypt_encrypt(MCRYPT_BLOWFISH, md5("damn"), stripslashes($a), MCRYPT_MODE_CBC, $newiv); echo "encrypted: ".$b."
"; $newiv = base64_encode(serialize($newiv)); echo "encoded IV: ".$newiv."
"; $b = base64_encode(serialize($b)); echo "encoded encrypted: ".$b."
"; $b = base64_encode(serialize($newiv.$b)); echo "encoded encrypted with iv: ".$b."
"; $data = unserialize(base64_decode($b)); echo "decoded encrypted with iv: ".$data."
"; $newiv = substr($data, 0, 20); echo "encoded IV: ".$newiv."
"; $newiv = unserialize(base64_decode($newiv)); echo "IV: ".$newiv."
"; $data = substr($data, 20); echo "encoded encrypted data: ".$data."
"; $b = unserialize(base64_decode($data)); echo "encrypted data: ".$b."
"; $un = mcrypt_decrypt(MCRYPT_BLOWFISH, md5("damn"), $b, MCRYPT_MODE_CBC, $newiv); echo $un."
"; echo md5("word al mighty"); $newiv = unserialize(base64_decode($newiv)); ?>