Skip to content

Commit

Permalink
* Moved string to encrypt from constructor to encrypt_data method
Browse files Browse the repository at this point in the history
* Added unit test to check encryption and decryption methods
  • Loading branch information
Glen Scott committed Feb 9, 2012
1 parent 7aaca56 commit 27c0b22
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README
Expand Up @@ -6,7 +6,7 @@ require_once 'SimpleCrypt.php';
print_r( SimpleCrypt::get_available_algorithms() );

// encrypt a string using Triple DES (CBC mode)
$crypt = new SimpleCrypt( 'tripledes', 'mysecretkey', 'stringtoencrypt' );
$encrypted_data = $crypt->encrypt_data();
$crypt = new SimpleCrypt( 'tripledes', 'mysecretkey' );
$encrypted_data = $crypt->encrypt_data( 'stringtoencrypt' );
echo "Encrypted data: " . bin2hex( $encrypted_data ) . "\n";
echo "Decrypted data: " . $crypt->decrypt_data( $encrypted_data ) . "\n";
11 changes: 7 additions & 4 deletions SimpleCrypt.php
Expand Up @@ -32,10 +32,10 @@ class SimpleCrypt {
private $iv;


public function __construct( $algorithm, $key, $input ) {
public function __construct( $algorithm, $key ) {
$this->algorithm = $algorithm;
$this->key = $key;
$this->input = $input;


$this->open_module();
$this->create_init_vector();
Expand All @@ -57,7 +57,9 @@ private function generic_init() {
return mcrypt_generic_init( $this->td, $this->key, $this->iv );
}

public function encrypt_data() {
public function encrypt_data( $input ) {
$this->input = $input;

$this->generic_init();
$enc = mcrypt_generic( $this->td, $this->input );
mcrypt_generic_deinit( $this->td );
Expand All @@ -69,7 +71,8 @@ public function encrypt_data() {
* @param $enc string
*/
public function decrypt_data( $enc ) {
$this->generic_init();
$ret = $this->generic_init();

$dec = mdecrypt_generic( $this->td, $enc );
mcrypt_generic_deinit( $this->td );

Expand Down
14 changes: 9 additions & 5 deletions SimpleCryptTest.php
Expand Up @@ -5,19 +5,23 @@

class SimpleCryptTest extends PHPUnit_Framework_TestCase {
protected $simplecrypt;
private $original_string;

protected function setUp() {
$this->simplecrypt = new SimpleCrypt( 'tripledes', 'key', 'string' );
$this->original_string = 'very important data';
$this->simplecrypt = new SimpleCrypt( 'tripledes', 'mysecretkey' );
}

public function testObjectInstantiation() {
$this->assertTrue( is_a( $this->simplecrypt, 'SimpleCrypt' ) );
}

public function testEncryptedStringIsIdentitalAfterDecryption() {
$this->markTestIncomplete(
'This test has not been completed yet.'
);
$this->assertEquals( 'string', $this->simplecrypt->decrypt_data( $this->simplecrypt->encrypt_data() ) );
$enc = $this->simplecrypt->encrypt_data( $this->original_string );
$this->assertTrue( is_string( $enc ) );

$dec = $this->simplecrypt->decrypt_data( $enc );
$this->assertTrue( is_string( $dec ) );
$this->assertTrue( strncmp( $dec, $this->original_string, strlen($this->original_string)) == 0 );
}
}
4 changes: 2 additions & 2 deletions example.php
Expand Up @@ -6,7 +6,7 @@
print_r( SimpleCrypt::get_available_algorithms() );

// encrypt a string using Triple DES (CBC mode)
$crypt = new SimpleCrypt( 'tripledes', 'mysecretkey', 'stringtoencrypt' );
$encrypted_data = $crypt->encrypt_data();
$crypt = new SimpleCrypt( 'tripledes', 'mysecretkey' );
$encrypted_data = $crypt->encrypt_data( 'very important data' );
echo "Encrypted data: " . bin2hex( $encrypted_data ) . "\n";
echo "Decrypted data: " . $crypt->decrypt_data( $encrypted_data ) . "\n";

0 comments on commit 27c0b22

Please sign in to comment.