FOF30\Encrypt\Aes::encryptString PHP Method

encryptString() public method

Encrypts a string using AES
public encryptString ( string $stringToEncrypt, boolean $base64encoded = true ) : string
$stringToEncrypt string The plaintext to encrypt
$base64encoded boolean Should I Base64-encode the result?
return string The cryptotext. Please note that the first 16 bytes of the raw string is the IV (initialisation vector) which is necessary for decoding the string.
    public function encryptString($stringToEncrypt, $base64encoded = true)
    {
        if (strlen($this->keyString) != 32) {
            $key = hash('sha256', $this->keyString, true);
        } else {
            $key = $this->keyString;
        }
        // Set up the IV (Initialization Vector)
        $iv_size = mcrypt_get_iv_size($this->cipherType, $this->cipherMode);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);
        if (empty($iv)) {
            $iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_RANDOM);
        }
        if (empty($iv)) {
            $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        }
        // Encrypt the data
        $cipherText = mcrypt_encrypt($this->cipherType, $key, $stringToEncrypt, $this->cipherMode, $iv);
        // Prepend the IV to the ciphertext
        $cipherText = $iv . $cipherText;
        // Optionally pass the result through Base64 encoding
        if ($base64encoded) {
            $cipherText = base64_encode($cipherText);
        }
        // Return the result
        return $cipherText;
    }

Usage Example

示例#1
0
文件: AesTest.php 项目: Joal01/fof
 /**
  * @covers FOF30\Encrypt\Aes
  *
  * @return  void
  */
 public function testCryptProcessEcb()
 {
     if (function_exists('mcrypt_module_open')) {
         $this->aes = new Aes('The quick brown fox jumped over the lazy dog', 256, 'ecb');
         // Regular string
         $str = 'THATISINSANE';
         $es = $this->aes->encryptString($str, true);
         $ds = $this->aes->decryptString($es, true);
         $ds = rtrim($ds, "");
         $this->assertNotEquals($str, $es);
         $this->assertEquals($str, $ds);
         // UTF-8 data
         $str = 'Χρησιμοποιώντας μη λατινικούς χαρακτήρες';
         $es = $this->aes->encryptString($str, false);
         $ds = $this->aes->decryptString($es, false);
         $ds = rtrim($ds, "");
         $this->assertNotEquals($str, $es);
         $this->assertEquals($str, $ds);
         // Using an odd sized keystring (using sha256 to convert it to a key)
         $this->aes = new Aes('The quick brown fox jumped over the lazy dog');
         $str = 'This is some very secret stuff that you are not supposed to transmit in clear text';
         $es = $this->aes->encryptString($str, true);
         $ds = $this->aes->decryptString($es, true);
         $ds = rtrim($ds, "");
         $this->assertNotEquals($str, $es);
         $this->assertEquals($str, $ds);
     } else {
         $this->markTestSkipped('mcrypt is not supported on this system');
     }
 }
All Usage Examples Of FOF30\Encrypt\Aes::encryptString