'value'] ); $cipherBuider = new AesCtrCipherBuilder($cipher); $cihperData = $cipherBuider->createCipherData(); $this->assertNotNull($cihperData); $this->assertEquals(16, strlen($cihperData->iv)); $this->assertEquals(32, strlen($cihperData->key)); $this->assertEquals(128, strlen($cihperData->encryptedIv)); $this->assertEquals(128, strlen($cihperData->encryptedKey)); $this->assertEquals('{"key":"value"}', $cihperData->matDesc); $this->assertEquals('RSA/NONE/PKCS1Padding', $cihperData->wrapAlgorithm); $this->assertEquals('AES/CTR/NoPadding', $cihperData->cekAlgorithm); } public function testFromEnvelope() { $cipher = new MasterRsaCipher( self::RSA_PUBLIC_KEY, self::RSA_PRIVATE_KEY, ['key' => 'value'] ); $cipherBuider = new AesCtrCipherBuilder($cipher); $envelope = new Envelope( 'De/S3T8wFjx7QPxAAFl7h7TeI2EsZlfCwox4WhLGng5DK2vNXxULmulMUUpYkdc9umqmDilgSy5Z3Foafw+v4JJThfw68T/9G2gxZLrQTbAlvFPFfPM9Ehk6cY4+8WpY32uN8w5vrHyoSZGr343NxCUGIp6fQ9sSuOLMoJg7hNw=', 'nyXOp7delQ/MQLjKQMhHLaT0w7u2yQoDLkSnK8MFg/MwYdh4na4/LS8LLbLcM18m8I/ObWUHU775I50sJCpdv+f4e0jLeVRRiDFWe+uo7Puc9j4xHj8YB3QlcIOFQiTxHIB6q+C+RA6lGwqqYVa+n3aV5uWhygyv1MWmESurppg=', 'AES/CTR/NoPadding', ); $cihperData = $cipherBuider->createCipherData(); $this->assertNotNull($cihperData); $this->assertEquals(16, strlen($cihperData->iv)); $this->assertEquals(32, strlen($cihperData->key)); $this->assertEquals(128, strlen($cihperData->encryptedIv)); $this->assertEquals(128, strlen($cihperData->encryptedKey)); $this->assertEquals('{"key":"value"}', $cihperData->matDesc); $this->assertEquals('RSA/NONE/PKCS1Padding', $cihperData->wrapAlgorithm); $this->assertEquals('AES/CTR/NoPadding', $cihperData->cekAlgorithm); } public function testEncrypt() { $cipherMaster = new MasterRsaCipher( self::RSA_PUBLIC_KEY, self::RSA_PRIVATE_KEY, ['key' => 'value'] ); $cipherBuider = new AesCtrCipherBuilder($cipherMaster); $base64_encrypted_key = 'nyXOp7delQ/MQLjKQMhHLaT0w7u2yQoDLkSnK8MFg/MwYdh4na4/LS8LLbLcM18m8I/ObWUHU775I50sJCpdv+f4e0jLeVRRiDFWe+uo7Puc9j4xHj8YB3QlcIOFQiTxHIB6q+C+RA6lGwqqYVa+n3aV5uWhygyv1MWmESurppg='; $base64_encrypted_iv = 'De/S3T8wFjx7QPxAAFl7h7TeI2EsZlfCwox4WhLGng5DK2vNXxULmulMUUpYkdc9umqmDilgSy5Z3Foafw+v4JJThfw68T/9G2gxZLrQTbAlvFPFfPM9Ehk6cY4+8WpY32uN8w5vrHyoSZGr343NxCUGIp6fQ9sSuOLMoJg7hNw='; $encrypted_key = base64_decode($base64_encrypted_key); $encrypted_iv = base64_decode($base64_encrypted_iv); $key = $cipherMaster->decrypt($encrypted_key); $iv = $cipherMaster->decrypt($encrypted_iv); $cipherData = new CipherData( $iv, $key, $encrypted_iv, $encrypted_key, '', 'RSA/NONE/PKCS1Padding', 'AES/CTR/NoPadding' ); $content = file_get_contents(self::getDataPath() . DIRECTORY_SEPARATOR . 'example.jpg'); $enc_content = file_get_contents(self::getDataPath() . DIRECTORY_SEPARATOR . 'enc-example.jpg'); $cipher = new AesCtr($cipherData, 0); foreach([16, 32, 64]as $key => $step) { $total = 0; for ($i = 0; $i < strlen($content); $i += $step) { $data = substr($content, $i, $step); $edata_pat = substr($enc_content, $i, $step); $edata = $cipher->encrypt($data); $this->assertEquals($edata_pat, $edata); $total += strlen($edata); } $this->assertEquals($total, strlen($content)); $cipher->reset(); } $cipher->reset(); $this->assertEquals($enc_content, $cipher->encrypt($content)); foreach([16, 32, 64] as $setp) { $total = 0; $cipher = new AesCtr($cipherData, 0); for ($i = 0; $i < strlen($content); $i += $setp) { $edata = substr($enc_content, $i, $setp); $data_pat = substr($content, $i, $setp); $dedata = $cipher->decrypt($edata); $this->assertEquals($data_pat, $dedata); $total += strlen($dedata); } $this->assertEquals($total, strlen($content)); } $cipher = new AesCtr($cipherData, 0); $this->assertEquals($content, $cipher->decrypt($enc_content)); } public function testEncrypt2() { $cipherMaster = new MasterRsaCipher( self::RSA_PUBLIC_KEY, self::RSA_PRIVATE_KEY, ['key' => 'value'] ); $cipherBuider = new AesCtrCipherBuilder($cipherMaster); $base64_encrypted_key = 'nyXOp7delQ/MQLjKQMhHLaT0w7u2yQoDLkSnK8MFg/MwYdh4na4/LS8LLbLcM18m8I/ObWUHU775I50sJCpdv+f4e0jLeVRRiDFWe+uo7Puc9j4xHj8YB3QlcIOFQiTxHIB6q+C+RA6lGwqqYVa+n3aV5uWhygyv1MWmESurppg='; $base64_encrypted_iv = 'De/S3T8wFjx7QPxAAFl7h7TeI2EsZlfCwox4WhLGng5DK2vNXxULmulMUUpYkdc9umqmDilgSy5Z3Foafw+v4JJThfw68T/9G2gxZLrQTbAlvFPFfPM9Ehk6cY4+8WpY32uN8w5vrHyoSZGr343NxCUGIp6fQ9sSuOLMoJg7hNw='; $encrypted_key = base64_decode($base64_encrypted_key); $encrypted_iv = base64_decode($base64_encrypted_iv); $key = $cipherMaster->decrypt($encrypted_key); $iv = $cipherMaster->decrypt($encrypted_iv); $cipherData = new CipherData( $iv, $key, $encrypted_iv, $encrypted_key, '', 'RSA/NONE/PKCS1Padding', 'AES/CTR/NoPadding' ); $content = file_get_contents(self::getDataPath() . DIRECTORY_SEPARATOR . 'example.jpg'); $enc_content = file_get_contents(self::getDataPath() . DIRECTORY_SEPARATOR . 'enc-example.jpg'); $cipher = new AesCtr($cipherData, 0); foreach([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]as $key => $step) { $total = 0; for ($i = 0; $i < strlen($content); $i += $step) { $data = substr($content, $i, $step); $edata_pat = substr($enc_content, $i, $step); $edata = $cipher->encrypt($data); $this->assertEquals($edata_pat, $edata); $total += strlen($edata); } $this->assertEquals($total, strlen($content)); $cipher->reset(); } $cipher->reset(); $this->assertEquals($enc_content, $cipher->encrypt($content)); foreach([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] as $setp) { $total = 0; $cipher = new AesCtr($cipherData, 0); for ($i = 0; $i < strlen($content); $i += $setp) { $edata = substr($enc_content, $i, $setp); $data_pat = substr($content, $i, $setp); $dedata = $cipher->decrypt($edata); $this->assertEquals($data_pat, $dedata); $total += strlen($dedata); } $this->assertEquals($total, strlen($content)); } $cipher = new AesCtr($cipherData, 0); $this->assertEquals($content, $cipher->decrypt($enc_content)); } }