PHP Classes

PHP Keyak: Encrypt and decrypt authenticated data using Keyak

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 107 All time: 9,675 This week: 571Up
Version License PHP version Categories
keyak 1.0.0The PHP License5PHP 5, Cryptography


This class can encrypt and decrypt authenticated data using Keyak algorithm.

It can take as parameters secret key value and metadata values that are authenticated and string value that is encrypted by the class to generate cipher text and an authenticating tag.

The class can also use the secret key to decrypt the generated cipher text and the authenticating tag to verify the data is authentic.

Innovation Award
PHP Programming Innovation award nominee
January 2021
Number 6
There are many encryption algorithms that can and encrypt and decrypt data.

The Keyak algorithm goes farther than many other algorithms by providing means to also authenticate the encrypted data.

This allows the recipient of the encrypted data to verify the authenticity of that data when it tries to decrypt the encrypted data that was received.

This package implements a pure PHP based solution to implement the Keyak algorithm to encrypt and decrypt data.

Manuel Lemos
Picture of Jose Luis Lucas
Name: Jose Luis Lucas <contact>
Classes: 10 packages by
Country: Spain Spain
Age: ???
All time rank: 250364 in Spain Spain
Week rank: 258 Up7 in Spain Spain Up
Innovation award
Innovation award
Nominee: 7x


include "keyakv2.php";
LakeKeyak can absorb up to 192 bytes of metadata per call to f
or up to 168 of plaintext, with additionally 24 bytes of metadata

, up to 150 bytes of nonce

class Sender
__construct ($K, $N, $tagFlag, $decryptFlag, $forgetFlag)
$this->T = new stringStream;
$this->k = new Keyak("Lake");
$status = $this->k->StartEngine($K, $N, $tagFlag, $this->T, $decryptFlag, $forgetFlag);
sendAEADMsg($message, $metadata, $decryptFlag, $forgetFlag)
$I = new stringStream($message);
$A = new stringStream($metadata);
$O = new stringStream;
$status = $this->k->Wrap($I, $O, $A, $this->T, $decryptFlag, $forgetFlag);
        return [
bin2hex($O->getvalue()), bin2hex($this->T->getvalue()), $status];
__construct($K, $N, $tagFlag, $unwrapFlag, $forgetFlag)
$this->k = new Keyak("Lake");
$this->T = new stringStream;
$status = $this->k->StartEngine($K, $N, $tagFlag, $this->T, $unwrapFlag, $forgetFlag);
recvAEADMsg($cipher, $metadata, $unwrapFlag_W, $forgetFlag, $T="")
$O = new stringStream;
$I = new stringStream($cipher);
$A = new stringStream($metadata);
    if (
$this->T = new stringStream($T);
$this->T=new stringStream;
$status = $this->k->Wrap($I, $O, $A, $this->T, $unwrapFlag_W, $forgetFlag);
        return [
bin2hex($O->getvalue()), $status];

$bool = ["False"=>0,"false"=>0,"True"=>1];
$vectors = array_slice(explode('*** Keyak',file_get_contents("")),1);

foreach (
$vectors as $vector)
$K = pack("H*",explode(']',explode('> K: [',$vector)[1])[0]);
$N = pack("H*",explode(']',explode('> N: [',$vector)[1])[0]);
$T_valid = explode(']',@explode('< T (tag): [',explode('Wrap',$vector)[0])[1])[0];
$engine = explode(')',explode('StartEngine(K, N,',$vector)[1])[0];
$tagFlag = $bool[explode(',',explode('tagFlag=',$engine)[1])[0]];
$unwrapFlag = $bool[explode(',',explode('unwrapFlag=',$engine)[1])[0]];
$forgetFlag = $bool[explode('forgetFlag=',$engine)[1]];

$sender = new Sender($K, $N, $tagFlag, $unwrapFlag, $forgetFlag);

$wraps = array_slice(explode('Wrap(I, O, A, T,',$vector),1);

foreach (
$wraps as $wrap)
$A = pack("H*",explode(']',explode('> A (metadata): [',$wrap)[1])[0]);
$I = pack("H*",explode(']',explode('> I (plaintext): [',$wrap)[1])[0]);
$O_valid = explode(']',explode('< O (ciphertext): [',$wrap)[1])[0];
$T_valid_wrap = explode(']',explode('< T (tag): [',$wrap)[1])[0];
$wrap = explode(")",$wrap)[0];
$unwrapFlag_W = $bool[explode(',',explode('unwrapFlag=',$wrap)[1])[0]];
$forgetFlag_W = $bool[explode('forgetFlag=',$wrap)[1]];
"\nCiphering ".bin2hex($I)."\n";
$O,$T,$status] = $sender->sendAEADMsg($I, $A, $unwrapFlag_W, $forgetFlag_W);
"Cipher $O\nExpected $O_valid\nTag $T\nExpected $T_valid_wrap\n\n";
    if (
$O == $O_valid and $T == $T_valid_wrap) echo "Ok\n";
"\n Bad Cipher");



Keyakv2 PHP Based on


  Files folder image Files  
File Role Description
Plain text file keyakv2.php Class Class source
Accessible without login Plain text file Doc. Documentation
Accessible without login Plain text file test_LakeKeyak.php Example Example script

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:9,675
This week:571Up