AES 128 bit Encryption and Decryption algorithm excuted purely on PHP with no external libraries.

Overview

AES128 Executed with PHP

Advanced Encryption Standard (AES) is a specification for the encryption of electronic data established by the U.S National Institute of Standards and Technology (NIST) in 2001. AES is widely used today as it is a much stronger than DES and triple DES despite being harder to implement.

The used secret key size is 128 bits (16 bytes) which means it takes 10 rounds to encrypt/decrypt the plain text. Image 1

1- AES key expansion:

We first split the key into four blocks then on the fourth block “w[3]” we perform:

  • Circular byte left shift
  • Byte Substitution (S-Box)
  • XOR with round constant table = g(w[3])

Image 2

Then we perform an XOR operation to generate 4 new blocks from the resulting g(w[4]) block, to form a new key:

Image 2

by combing all of the blocks from w[4] to w[7] we form the round key.

In encryption: we generate new round keys along side each iteration of the state matrix to XOR them by each generated round key.

In decryption: we generate all the round keys first, then we XOR the state matrix with the round keys in reverse order in each iteration (10 iterations since we’re using 128 bits).

2- Add round key to state matrix:

The state matrix is the plaintext (converted to HEX) in a 2d array form.

We simply perform XOR operation with current round key to create a new state matrix each round until a cipher is produced on the 10th round.

3- S-Box and Inverse S-Box:

We simply substitute the bytes using Rijndael’ S-box aquired from (https://en.wikipedia.org/wiki/Rijndael_S-box)

Image 3

4- ShiftRow and Inverse Shiftrow:

This step is just as it sounds. Each row is shifted a particular number of times.

  • The first row is not shifted
  • The second row is shifted once to the left.
  • The third row is shifted twice to the left.
  • The fourth row is shifted thrice to the left.

5- Mix Columns and Inverse Mix Columns:

(https://en.wikipedia.org/wiki/Rijndael_MixColumns)

Each row in the state matrix is treated as a four-term polynomial, which are elements within Galois Field GF(2^8).

  • we multiply by this matrix..

Image 4

  • while in the inverse mix columns we multiply by the matrix..

Image 5

After encryption/decryption:

the outputted text will be converted to a string of hexadecimals then converted back to binary text.

the way I implemented inputting bigger data isn't the most ideal way but it gets the job done!

$strinput="Two One Nine Two";
$secret_key="SECRET";

$strinput = str_split($strinput,16);                                    //spliting string into 16 bytes (128 bit) per block.
for($i=0; $i<count($strinput); $i++)
{
    $strinput[$i] = str_pad($strinput[$i],16,'#',STR_PAD_LEFT);         //pads string into 16 bytes (128 bit) per blocks.
}

$ciphertext="";

$finaltext ="";
for($i=0 ; $i<count($strinput) ; $i++)
{
    $cipher = AES_ENCTYPT($strinput[$i], $secret_key);                 //sends the blocks to be encrypted indivisually all with the Key
    $cipher = hex2bin($cipher);
    $ciphertext= $ciphertext . $cipher;                                //adds each resulting cipher to a string.
}

echo "<br><br>----------------------------------------- Decryption<br>";

$ciphertext = str_split($ciphertext,16);                                //spliting cipher string into 16 bytes (128 bit) per block.

for($i=0 ; $i<count($ciphertext) ; $i++)
{
    $plain = AES_DECRYPT($ciphertext[$i], $secret_key);                 //sends the blocks to be Dencrypted indivisually all with the Key

    $plain = hex2bin($plain);
    $goodUrl = str_replace('#', '', $plain);
    $finaltext = $finaltext . $goodUrl;                                 //adds each resulting text to a string.
}
echo "<br> plainText: $finaltext";

Image 5

You might also like...
A multitool library offering access to recommended security related libraries, standardised implementations of security defences, and secure implementations of commonly performed tasks.

SecurityMultiTool A multitool library offering access to recommended security related libraries, standardised implementations of security defences, an

PHPIDS (PHP-Intrusion Detection System) is a simple to use, well structured, fast and state-of-the-art security layer for your PHP based web application

PHPIDS PHPIDS (PHP-Intrusion Detection System) is a simple to use, well structured, fast and state-of-the-art security layer for your PHP based web ap

Automatic Encrypt and Decrypt your database data. Tested and used on Laravel 8

Laravel Encrypt Database Automatic Encrypt and Decrypt your database data. Tested and used on Laravel 8. I'm yet building the tests. Important Note th

Laravel Security was created by, and is maintained by Graham Campbell, and is a voku/anti-xss wrapper for Laravel, using graham-campbell/security-core
Laravel Security was created by, and is maintained by Graham Campbell, and is a voku/anti-xss wrapper for Laravel, using graham-campbell/security-core

Laravel Security Laravel Security was created by, and is maintained by Graham Campbell, and is a voku/anti-xss wrapper for Laravel, using graham-campb

PHP 5.x support for random_bytes() and random_int()

random_compat PHP 5.x polyfill for random_bytes() and random_int() created and maintained by Paragon Initiative Enterprises. Although this library sho

TCrypto is a simple and flexible PHP 5.3+ in-memory key-value storage library

About TCrypto is a simple and flexible PHP 5.3+ in-memory key-value storage library. By default, a cookie will be used as a storage backend. TCrypto h

Ransomware with automatic Coinbase Commerce integration created in C# (Console) and PHP
Ransomware with automatic Coinbase Commerce integration created in C# (Console) and PHP

AWare — C# Ransomware Ransomware with automatic Coinbase Commerce integration created in C# (Console) and PHP PD: AWare is just a proof of concept, wi

A (unofficial) WordPress plugin reporting PHP and JavaScript errors to Sentry.

A (unofficial) WordPress plugin to report PHP and JavaScript errors to Sentry.

A simple PHP web backdoor allows you to retrieve directory/file contents and upload file(s) from the local machine or remote URL.
A simple PHP web backdoor allows you to retrieve directory/file contents and upload file(s) from the local machine or remote URL.

Simple PHP Web Backdoor A simple PHP web backdoor allows you to retrieve directory/file contents and upload file(s) from the local machine or remote U

Owner
Ahmed Mohamed Mostafa
Game developer, 22, Egyptian.
Ahmed Mohamed Mostafa
A simple php (lumen) app for sharing sensitive text (basically like onetimesecret), but with full end-to-end AES-256-GCM encryption so even the server has no access to the data, and developed with very simple deployment in mind.

A simple php (lumen) app for sharing sensitive text (basically like onetimesecret), but with full end-to-end AES-256-GCM encryption so even the server has no access to the data, and developed with very simple deployment in mind.

Alan Woo 51 Nov 21, 2022
A cryptography API wrapping the Sodium library, providing a simple object interface for symmetrical and asymmetrical encryption, decryption, digital signing and message authentication.

PHP Encryption A cryptography API wrapping the Sodium library, providing a simple object interface for symmetrical and asymmetrical encryption, decryp

null 19 Dec 31, 2022
Replaces Laravel's built-in encryption with an encryption based on AWS KMS

Laravel Kms Encryption Introduction This package replaces Laravel's built-in encryption with an encryption based on AWS KMS. Two major features provid

Arnaud Becher 3 Oct 26, 2021
A minimalist, open source online pastebin where the server has zero knowledge of pasted data. Data is encrypted/decrypted in the browser using 256 bits AES.

Current version: 1.3.5 PrivateBin is a minimalist, open source online pastebin where the server has zero knowledge of pasted data. Data is encrypted a

null 4.6k Jan 7, 2023
JSON Object Signing and Encryption library for PHP.

NAMSHI | JOSE Deprecation notice Hi there, as much as we'd like to be able to work on all of the OSS in the world, we don't actively use this library

Namshi 1.7k Dec 22, 2022
Pretty Good Privacy (PGP) is an encryption program that provides cryptographic privacy and authentication for data communication.

Pretty Good Privacy (PGP) is an encryption program that provides cryptographic privacy and authentication for data communication. PGP is used for signing, encrypting, and decrypting texts, e-mails, files, directories, and whole disk partitions and to increase the security of e-mail communications. Phil Zimmermann developed PGP in 1991.

[sCRiPTz-TEAM] 3 Dec 31, 2021
Password manager featuring client-side encryption, vaults, folders and more.

vaults is a password manager featuring client side AES-256 encryption, PBKDF2 hashing, vaults, password generation & more. Features Technical overview

null 27 Nov 18, 2022
Simple Encryption in PHP.

php-encryption composer require defuse/php-encryption This is a library for encrypting data with a key or password in PHP. It requires PHP 5.6 or new

Taylor Hornby 3.6k Jan 3, 2023
A petite library of encryption functions for PHP

?? dcrypt A petite library of essential encryption functions for PHP 7.1+. For legacy PHP version support, look here. If you need a dcrypt inspired en

null 96 Oct 6, 2022
Encryption-free Private Messaging For Flarum

Whisper - Private Messaging for Flarum A Flarum extension. Add private messaging functionality to your Flarum Community! Simple to install, no setting

Charlie 4 Dec 7, 2021