Encryption class wrapper


/ Published in: PHP
Save to your folder(s)

Encryption class wrapper for Blowfish. Can be adapted to other encryption algorithms.


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3.  * This class encapsulates Blowfish encryption and decryption.
  4.  */
  5. class Encrypter{
  6. protected $key;
  7. protected $iv;
  8.  
  9. function __construct($iv=null, $key=null){
  10. $this->iv = $iv;
  11. $this->key = $key;
  12. }
  13.  
  14. /**
  15.   * Encrypts data
  16.   * Returns binary data.
  17.   */
  18. function encrypt($data){
  19. $len = strlen($data);
  20. $enc = mcrypt_encrypt( MCRYPT_BLOWFISH, $this->key, $data, MCRYPT_MODE_CBC, $this->iv );
  21. return $enc;
  22. }
  23.  
  24. /**
  25.   * Encrypts data and convert to hex.
  26.   * Ideal for storing as text.
  27.   */
  28. function encryptToHex($data){
  29. return $this->bin2hex($this->encrypt($data));
  30. }
  31.  
  32. /**
  33.   * Takes hex input, convert from hex to binary then decrypt.
  34.   */
  35. function decryptFromHex($data){
  36. //echo "hex \"$data\"\n";
  37. return $this->decrypt($this->hex2bin($data));
  38. }
  39.  
  40. /**
  41.   *
  42.   */
  43. function decrypt($data){
  44. $decode = mcrypt_decrypt( MCRYPT_BLOWFISH, $this->key, $data, MCRYPT_MODE_CBC, $this->iv );
  45. //echo "decode \"$decode\"\n";
  46. $realdata = rtrim($decode, "\0");
  47.  
  48. //echo sprintf("[%s]\n", $realdata);
  49. return $realdata;
  50. }
  51.  
  52. /**
  53.   * Convert from binary to hex
  54.   */
  55. function bin2hex($s){
  56. return bin2hex($s);
  57. }
  58.  
  59. /**
  60.   * Convert from hex to binary
  61.   */
  62. function hex2bin($s){
  63. return pack("H*", $s);
  64. }
  65.  
  66. /**
  67.   * loads key and iv from a file.
  68.   * Key is on line 1 in hex.
  69.   * IV is on line 2 in hex.
  70.   */
  71. function loadKeysFromFile($filename){
  72. return $this->loadKeysFromString(file_get_contents($filename));
  73. }
  74.  
  75. /**
  76.   * String is the key and the iv separates by a newline.
  77.   * Use this when storing the key in a file.
  78.   * Line 1 would be the key, line 2 is the iv.
  79.   */
  80. function loadKeysFromString($string){
  81. $keys = explode("\n", $string);
  82. $key = $keys[0];
  83. $iv = pack("H*", $keys[1]);
  84. $this->key = $key;
  85. $this->iv = $iv;
  86. return array($key, $iv);
  87. }
  88.  
  89. function showKeys(){
  90. return sprintf("%s %s", $this->key, $this->bin2hex($this->iv));
  91. }
  92. }
  93.  
  94. --- put the following 2 lines in a file names 'keys' to get the demo to work ---
  95. 4987bf894deh898efae46384efffeabcc387da8d7e8af893783745d1
  96. 4fecabab1234bcbc
  97. ----- end keys ------
  98.  
  99. <?php
  100. require_once 'Encrypter.php';
  101. class MyApp {
  102.  
  103.  
  104. public function run() {
  105. $e = new Encrypter;
  106. $e->loadKeysFromFile('keys');
  107.  
  108. echo $e->showKeys() . "\n";
  109. $hex = $e->encryptToHex("4646123412341234");
  110. print $hex . "\n";
  111. print $e->decryptFromHex($hex) . "\n";
  112. }
  113. }
  114. $app = new MyApp;
  115. $app->run();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.