How to encrypt / decrypt AES with Libsodium-PHP

atlas81887 picture atlas81887 · Dec 27, 2015 · Viewed 15.6k times · Source


I need to encrypt/decrypt data with PHP. I am completely new to this, however I have read that Libsodium-PHP is the best tool for AES encryption. Much like the other PHP encryption libraries I have researched Libsoduim-PHP seemed to offer almost no documentation of how to use the library (that I was able to find). Can anyone that has experience with PHP encryption either point me in the direction of a good learning resource or write a few lines of sample code using Libsoduim-PHP?
Thank you very much for the help,
Atlas

Answer

Scott Arciszewski picture Scott Arciszewski · Dec 27, 2015

Much like the other PHP encryption libraries I have researched Libsoduim-PHP seemed to offer almost no documentation of how to use the library (that I was able to find).

From the libsodium-php Github page you will find a direct link to a free online book that covers everything you need to know to get started with libsodium.

The final chapter contains libsodium recipes, but each chapter contains detailed usage information.

If you specifically need AES, read this.

If you don't have an "AES-or-bust" requirement hanging over your head, where failure to specifically use AES means your department gets axed and your developers face a firing squad, you should consider just using crypto_secretbox which uses Xsalsa20 for encryption and attaches a Poly1305 authentication tag. (This is authenticated encryption, which you want to use almost always.)

Also look into Halite if you want easy-mode.