Encrypting and decrypting messages
Once the basic setup is in place, the normal message flow is built around PlainTextMessage, CipherText, and EncryptedMessage.
Encrypting a plain text message
The sender starts by creating a PlainTextMessage:
use GT\Cipher\Message\PlainTextMessage;
$message = new PlainTextMessage("Launch code: tea and biscuits");
Calling encrypt() produces a CipherText object:
use GT\Cipher\Key;
$key = new Key();
$cipherText = $message->encrypt($key);
At that point:
(string)$cipherTextis the base64 encoded cipher string$message->getIv()returns theInitVectorused for encryption
The CipherText object itself keeps the raw encrypted bytes, but it is usually the base64 form that we transmit.
What needs to be transmitted
To decrypt successfully, the receiving side needs:
- the cipher text
- the IV
- the same shared key
Only the key must remain secret. The IV is part of the normal payload.
Decrypting an incoming message
On the receiving side we construct EncryptedMessage using the incoming cipher text string and IV:
$received = new EncryptedMessage(
$cipherString,
$ivString,
);
Here the IV is shown in its transmitted base64 string form. EncryptedMessage will convert it internally into an InitVector.
Then decrypt using the same key:
$plainText = $received->decrypt($key);
echo $plainText;
The return value is a PlainTextMessage, not a raw string. That keeps the API symmetric and lets us keep using the same message abstraction if needed.
Supplying your own IV
PlainTextMessage creates a fresh IV by default, but we can also provide one explicitly:
use GT\Cipher\InitVector;
use GT\Cipher\Message\PlainTextMessage;
$iv = new InitVector();
$message = new PlainTextMessage("Fixed IV example", $iv);
In normal usage, letting the library generate the IV is the safest and simplest option.
String representations
The library uses string casting consistently:
(string)$messagegives the plain text content(string)$cipherTextgives the base64 cipher text(string)$message->getIv()gives the base64 IV(string)$keygives the base64 key
That makes it straightforward to serialise values into JSON, query strings, logs, or storage layers. We should still take care never to expose the key in logs or browser-visible output.
Receiving from another process
If the receiver is reconstructing the key from a stored base64 string, decode it first:
use GT\Cipher\Key;
$storedBase64Key = "…";
$key = new Key(base64_decode($storedBase64Key));
If the IV was also received as a base64 string, it can usually be passed straight into EncryptedMessage:
$received = new EncryptedMessage($cipherString, $storedBase64Iv);
If you need an InitVector object separately for some reason, you can still reconstruct it manually:
use GT\Cipher\InitVector;
$iv = (new InitVector())->withBytes(base64_decode($storedBase64Iv));
That gives us both options: the simple string-based constructor path for normal receiving code, and the explicit InitVector reconstruction when an object is needed elsewhere.
If your transport mechanism is a URL rather than separate fields, continue to Encrypted URIs.