Example #1
0
 /**
  * Test that encryption works between AES and mcrypt instances
  *
  * @dataProvider keysAndMessages
  */
 public function testCrossEncryption(\phpaes\AES $engine, $key, $iv, $message)
 {
     $engos = new phpaes\AES_CBC_OpenSSL();
     $engmc = new phpaes\AES_CBC_Mcrypt(new phpaes\PKCS7());
     $engos->setKey($key);
     $engos->setIv($iv);
     $engmc->setKey($key);
     $engmc->setIv($iv);
     $a = $engmc->decrypt($engos->encrypt($message));
     $b = $engos->decrypt($engmc->encrypt($message));
     $this->assertSame($b, $a);
 }
Example #2
0
<?php

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/shared-data.php';
$aesM = new phpaes\AES_CBC_Mcrypt(new phpaes\PKCS7());
//$aesM = new AES_CBC_Mcrypt(new NoPad()); // Try it with this to see what goes wrong!
$aesO = new phpaes\AES_CBC_OpenSSL();
$util = new phpaes\Util();
foreach ($keys as $keylen => $key) {
    $aesM->setKey($key);
    $aesM->setIv($iv);
    $aesO->setKey($key);
    $aesO->setIv($iv);
    foreach ($testTexts as $plainText) {
        // each encrypt
        $cipherTextM = $aesM->encrypt($plainText);
        $cipherTextO = $aesO->encrypt($plainText);
        // each decrypt own content
        $decodedTextM = $aesM->decrypt($cipherTextM);
        $decodedTextO = $aesO->decrypt($cipherTextO);
        // each decrypt the OTHER's ciphertext
        $decodedTextMx = $aesM->decrypt($cipherTextO);
        $decodedTextOx = $aesO->decrypt($cipherTextM);
        echo "-------------------------------------------------------\n";
        echo "                    Key: ({$keylen}) {$key}\n";
        echo "              Plaintext: {$plainText}\n";
        echo "       Same Cipher Text: " . ($cipherTextM === $cipherTextO ? 'TRUE' : 'FALSE') . "\n";
        //        echo "    mcrypt cipher bytes: " . $util->bytestring($cipherTextM) . "\n";
        //        echo "   openssl cipher bytes: " . $util->bytestring($cipherTextO) . "\n";
        echo "      Same Decoded Text: " . ($decodedTextM === $decodedTextO ? 'TRUE' : 'FALSE') . "\n";
        //        echo "    mcrypt decode bytes: " . $util->bytestring($decodedTextM) . "\n";
Example #3
0
<?php

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/shared-data.php';
$pkcs7 = new phpaes\PKCS7();
//$pkcs7 = new phpaes\NoPad();
$aescbc = new phpaes\AES_CBC_Mcrypt($pkcs7);
$util = new phpaes\Util();
foreach ($keys as $keylen => $key) {
    $aescbc->setKey($key);
    $aescbc->setIv($iv);
    foreach ($testTexts as $plainText) {
        $cipherText = $aescbc->encrypt($plainText);
        $padded = $pkcs7->pad($plainText, 16);
        $decodedText = $aescbc->decrypt($cipherText);
        echo "-------------------------------------------------------\n";
        echo "               Key: ({$keylen}) {$key}\n";
        echo "         Plaintext: {$plainText}\n";
        echo " Plaintext (bytes): " . $util->bytestring($plainText) . "\n";
        echo "    Padded (bytes): " . $util->bytestring($padded) . "\n";
        echo "      Padded (hex): " . bin2hex($padded) . "\n";
        echo "      Padded (len): " . strlen($padded) . "\n";
        echo "Ciphertext (bytes): " . $util->bytestring($cipherText) . "\n";
        echo "  Ciphertext (hex): " . bin2hex($cipherText) . "\n";
        echo "  Ciphertext (len): " . strlen($cipherText) . "\n";
        echo "     Decryptedtext: {$decodedText}\n";
    }
}