説明なし

Cryptor.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Simple example of using the openssl encrypt/decrypt functions that
  4. * are inadequately documented in the PHP manual.
  5. *
  6. * Available under the MIT License
  7. *
  8. * The MIT License (MIT)
  9. * Copyright (c) 2016 ionCube Ltd.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  12. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  13. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  14. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  17. * the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  20. * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  21. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
  22. * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. namespace ioncube\phpOpensslCryptor;
  25. class Cryptor {
  26. private $cipher_algo;
  27. private $hash_algo;
  28. private $iv_num_bytes;
  29. private $format;
  30. const FORMAT_RAW = 0;
  31. const FORMAT_B64 = 1;
  32. const FORMAT_HEX = 2;
  33. /**
  34. * Construct a Cryptor, using aes256 encryption, sha256 key hashing and base64 encoding.
  35. * @param string $cipher_algo The cipher algorithm.
  36. * @param string $hash_algo Key hashing algorithm.
  37. * @param [type] $fmt Format of the encrypted data.
  38. */
  39. public function __construct( $cipher_algo = 'aes-256-ctr', $hash_algo = 'sha256', $fmt = Cryptor::FORMAT_B64 ) {
  40. $this->cipher_algo = $cipher_algo;
  41. $this->hash_algo = $hash_algo;
  42. $this->format = $fmt;
  43. if ( ! in_array( $cipher_algo, openssl_get_cipher_methods( true ) ) ) {
  44. throw new \Exception( "Cryptor:: - unknown cipher algo {$cipher_algo}" );
  45. }
  46. if ( ! in_array( $hash_algo, openssl_get_md_methods( true ) ) ) {
  47. throw new \Exception( "Cryptor:: - unknown hash algo {$hash_algo}" );
  48. }
  49. $this->iv_num_bytes = openssl_cipher_iv_length( $cipher_algo );
  50. }
  51. /**
  52. * Encrypt a string.
  53. * @param string $in String to encrypt.
  54. * @param string $key Encryption key.
  55. * @param int $fmt Optional override for the output encoding. One of FORMAT_RAW, FORMAT_B64 or FORMAT_HEX.
  56. * @return string The encrypted string.
  57. */
  58. public function encryptString( $in, $key, $fmt = null ) {
  59. if ( $fmt === null ) {
  60. $fmt = $this->format;
  61. }
  62. // Build an initialisation vector
  63. $iv = openssl_random_pseudo_bytes( $this->iv_num_bytes, $isStrongCrypto );
  64. if ( ! $isStrongCrypto ) {
  65. throw new \Exception( "Cryptor::encryptString() - Not a strong key" );
  66. }
  67. // Hash the key
  68. $keyhash = openssl_digest( $key, $this->hash_algo, true );
  69. // and encrypt
  70. $opts = OPENSSL_RAW_DATA;
  71. $encrypted = openssl_encrypt( $in, $this->cipher_algo, $keyhash, $opts, $iv );
  72. if ( $encrypted === false ) {
  73. throw new \Exception( 'Cryptor::encryptString() - Encryption failed: ' . openssl_error_string() );
  74. }
  75. // The result comprises the IV and encrypted data
  76. $res = $iv . $encrypted;
  77. // and format the result if required.
  78. if ( $fmt == Cryptor::FORMAT_B64 ) {
  79. $res = base64_encode( $res );
  80. } else if ( $fmt == Cryptor::FORMAT_HEX ) {
  81. $unp = unpack( 'H*', $res );
  82. $res = $unp[ 1 ];
  83. }
  84. return $res;
  85. }
  86. /**
  87. * Decrypt a string.
  88. * @param string $in String to decrypt.
  89. * @param string $key Decryption key.
  90. * @param int $fmt Optional override for the input encoding. One of FORMAT_RAW, FORMAT_B64 or FORMAT_HEX.
  91. * @return string The decrypted string.
  92. */
  93. public function decryptString( $in, $key, $fmt = null ) {
  94. if ( $fmt === null ) {
  95. $fmt = $this->format;
  96. }
  97. $raw = $in;
  98. // Restore the encrypted data if encoded
  99. if ( $fmt == Cryptor::FORMAT_B64 ) {
  100. $raw = base64_decode( $in );
  101. } else if ( $fmt == Cryptor::FORMAT_HEX ) {
  102. $raw = pack( 'H*', $in );
  103. }
  104. // and do an integrity check on the size.
  105. if ( strlen( $raw ) < $this->iv_num_bytes ) {
  106. throw new \Exception( 'Cryptor::decryptString() - ' .
  107. 'data length ' . strlen( $raw ) . " is less than iv length {$this->iv_num_bytes}" );
  108. }
  109. // Extract the initialisation vector and encrypted data
  110. $iv = substr( $raw, 0, $this->iv_num_bytes );
  111. $raw = substr( $raw, $this->iv_num_bytes );
  112. // Hash the key
  113. $keyhash = openssl_digest( $key, $this->hash_algo, true );
  114. // and decrypt.
  115. $opts = OPENSSL_RAW_DATA;
  116. $res = openssl_decrypt( $raw, $this->cipher_algo, $keyhash, $opts, $iv );
  117. if ( $res === false ) {
  118. throw new \Exception( 'Cryptor::decryptString - decryption failed: ' . openssl_error_string() );
  119. }
  120. return $res;
  121. }
  122. /**
  123. * Static convenience method for encrypting.
  124. * @param string $in String to encrypt.
  125. * @param string $key Encryption key.
  126. * @param int $fmt Optional override for the output encoding. One of FORMAT_RAW, FORMAT_B64 or FORMAT_HEX.
  127. * @return string The encrypted string.
  128. */
  129. public static function Encrypt( $in, $key, $fmt = null ) {
  130. $c = new Cryptor();
  131. return $c->encryptString( $in, $key, $fmt );
  132. }
  133. /**
  134. * Static convenience method for decrypting.
  135. * @param string $in String to decrypt.
  136. * @param string $key Decryption key.
  137. * @param int $fmt Optional override for the input encoding. One of FORMAT_RAW, FORMAT_B64 or FORMAT_HEX.
  138. * @return string The decrypted string.
  139. */
  140. public static function Decrypt( $in, $key, $fmt = null ) {
  141. $c = new Cryptor();
  142. return $c->decryptString( $in, $key, $fmt );
  143. }
  144. }