use OPENSSL_RAW_DATA, in which openssl adds/strips padding https://en.wikipedia.org/wiki/Padding_%28cryptography%29#PKCS7

This commit is contained in:
tom--
2015-02-11 11:14:11 -05:00
parent 1590cb8d68
commit e049e9b117

View File

@@ -172,11 +172,14 @@ class Security extends Component
$key = $this->hkdf(self::KDF_HASH, $secret, $keySalt, $info, self::KEY_SIZE); $key = $this->hkdf(self::KDF_HASH, $secret, $keySalt, $info, self::KEY_SIZE);
} }
$data = $this->addPadding($data);
$ivSize = 16; $ivSize = 16;
$iv = $this->generateRandomKey($ivSize); $iv = $this->generateRandomKey($ivSize);
$encrypted = openssl_encrypt($data, $this->opensslCipher(), $key, OPENSSL_ZERO_PADDING, $iv);
$encrypted = base64_decode($encrypted); // $data = $this->addPadding($data);
// $encrypted = openssl_encrypt($data, $this->opensslCipher(), $key, OPENSSL_ZERO_PADDING, $iv);
// $encrypted = base64_decode($encrypted);
$encrypted = openssl_encrypt($data, $this->opensslCipher(), $key, OPENSSL_RAW_DATA, $iv);
$authKey = $this->hkdf(self::KDF_HASH, $key, null, self::AUTH_KEY_INFO, self::KEY_SIZE); $authKey = $this->hkdf(self::KDF_HASH, $key, null, self::AUTH_KEY_INFO, self::KEY_SIZE);
$hashed = $this->hashData($iv . $encrypted, $authKey); $hashed = $this->hashData($iv . $encrypted, $authKey);
@@ -216,10 +219,15 @@ class Security extends Component
$ivSize = 16; $ivSize = 16;
$iv = StringHelper::byteSubstr($data, 0, $ivSize); $iv = StringHelper::byteSubstr($data, 0, $ivSize);
$encrypted = base64_encode(StringHelper::byteSubstr($data, $ivSize, null)); $encrypted = StringHelper::byteSubstr($data, $ivSize, null);
$decrypted = openssl_decrypt($encrypted, $this->opensslCipher(), $key, OPENSSL_ZERO_PADDING, $iv);
return $this->stripPadding($decrypted); // $encrypted = base64_encode($encrypted);
// $decrypted = openssl_decrypt($encrypted, $this->opensslCipher(), $key, OPENSSL_ZERO_PADDING, $iv);
// $decrypted = $this->stripPadding($decrypted);
$decrypted = openssl_decrypt($encrypted, $this->opensslCipher(), $key, OPENSSL_RAW_DATA, $iv);
return $decrypted;
} }
/** /**