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? |
Résultat | 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. |
/** * @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'); } }
public function GetTestGetTransparentAuthenticationCredentials() { // Let's do some TOTP encoding $totp = new Totp(); $otp = $totp->getCode(static::$totpKey); $cryptoKey = hash('sha256', static::$totpKey . $otp); $aes = new Aes($cryptoKey); $plainText_right = json_encode(array('username' => 'FOF30test', 'password' => 'dummy')); $plainText_missingPassword = json_encode(array('username' => 'FOF30test')); $plainText_missingUsername = json_encode(array('password' => 'dummy')); $plainText_crap = 'crap_data'; $encoded_right = $aes->encryptString($plainText_right); $encoded_missingPassword = $aes->encryptString($plainText_missingPassword); $encoded_missingUsername = $aes->encryptString($plainText_missingUsername); $encoded_crap = $aes->encryptString($plainText_crap); $otp = $totp->getCode(static::$totpKey, time() - 86400); $cryptoKey = hash('sha256', static::$totpKey . $otp); $aes = new Aes($cryptoKey); $encodedOutdated = $aes->encryptString($plainText_right); // Input data, server globals, do I expect correct username/password return array(array(null, array('PHP_AUTH_USER' => 'FOF30test', 'PHP_AUTH_PW' => 'dummy'), true), array(null, array('PHP_AUTH_PW' => 'dummy'), false), array(null, array('PHP_AUTH_USER' => 'FOF30test'), false), array(array('testAuth' => json_encode(array('username' => 'FOF30test', 'password' => 'dummy'))), null, true), array(array('testAuth' => json_encode(array('password' => 'dummy'))), null, false), array(array('testAuth' => json_encode(array('username' => 'FOF30test'))), null, false), array(array('testAuth' => 'stupid_string_is_no_good_json_data'), null, false), array(array('FOF30Username' => 'FOF30test', 'FOF30Password' => 'dummy'), null, true), array(array('FOF30Password' => 'dummy'), null, false), array(array('FOF30Username' => 'FOF30test'), null, false), array(array('junk' => 'food'), null, false), array(null, array('PHP_AUTH_USER' => 'FOF30user', 'PHP_AUTH_PW' => $encoded_right), true), array(null, array('PHP_AUTH_USER' => 'FOF30user', 'PHP_AUTH_PW' => $encoded_missingUsername), false), array(null, array('PHP_AUTH_USER' => 'FOF30user', 'PHP_AUTH_PW' => $encoded_missingPassword), false), array(null, array('PHP_AUTH_USER' => 'FOF30user', 'PHP_AUTH_PW' => $encoded_crap), false), array(null, array('PHP_AUTH_USER' => 'FOF30user', 'PHP_AUTH_PW' => 'this_is_crap_data'), false), array(null, array('PHP_AUTH_USER' => 'FOF30user', 'PHP_AUTH_PW' => $encodedOutdated), false), array(array('testAuth' => $encoded_right), null, true), array(array('testAuth' => $encoded_missingUsername), null, false), array(null, array('testAuth' => $encoded_missingPassword), false), array(null, array('testAuth' => $encoded_crap), false), array(array('testAuth' => 'this_is_crap_data'), null, false), array(array('testAuth' => $encodedOutdated), null, false)); }