DEV Community

Cover image for Stop letting your data unprotected!
Raziel Rodrigues
Raziel Rodrigues

Posted on • Updated on

Stop letting your data unprotected!

Today I will introduce you to the concept of cryptography, an old tool used by the humans, since writing and reading were invented humans also invented a way to keep some messages secrets with only one person can read.

It's a secret!

When was encryption invented, and by whom? 🤔

The earliest written evidence of encryption can be traced to ancient Egypt. Nearly 4,000 years ago, the tomb of nobleman Khnumhotep II contained a script recording his deeds in life. However, some unusual hieroglyphs were used that obscured the original meaning of the text. Ancient encryption in Egypt was used mainly to protect knowledge, as education was a privilege limited to the highest circles of society and was also a way to show one’s skills in writing. It was also used for religious reasons, for example, to discuss taboos.

The first recorded instance of encryption being used for military purposes dates to around 500 BC. Spartan encryption used an invention called the scytale, which allowed secret messages to be sent and received. A narrow strip of parchment was wound around the device, and the text was written along the length of the device. Once the strip was wound off the cylinder, the text became unreadable unless the reader possessed an identical cylinder. This was the first time the concept of a common key, seen even today in modern cryptographic technologies, was used for both encryption and decryption.

Decrypting message gif

Asymmetric encryption 🔐

There are two sides in an encrypted communication: the sender, who encrypts the data, and the recipient, who decrypts it. As the name implies, asymmetric encryption is different on each side; the sender and the recipient use two different keys. Asymmetric encryption, also known as public key encryption, uses a public key-private key pairing: data encrypted with the public key can only be decrypted with the private key.

Descryption schema

Lets build the code with PHP 🐘

<?php

$config = array(
    "digest_alg" => "sha512",
    "private_key_bits" => 4096,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
);

// Create the private and public key
$res = openssl_pkey_new($config);

// Extract the private key from $res to $privKey
openssl_pkey_export($res, $privKey);

// Extract the public key from $res to $pubKey
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];

$data = 'plaintext data goes here';

// Encrypt the data to $encrypted using the public key
openssl_public_encrypt($data, $encrypted, $pubKey);

// Decrypt the data using the private key and store the results in $decrypted
openssl_private_decrypt($encrypted, $decrypted, $privKey);

echo $decrypted;
Enter fullscreen mode Exit fullscreen mode

You can run this code clicking here

I created a small app as well 🖥️

I have created a small app in PHP to try out the encryption you can take a look here

Considerations 🎯

Encryption is a very powerful tool widely used through the internet nowadays specially because of the rules regarding personal data and other co related stuff, this could be really useful inside your project and can level up the safety of any application, one thing which is nice is you can working saving those keys for specific occasions or simple putting the private key in a secure part of your system and work with the decryption.

This example I have used openssl because is a default resource from PHP however you can also use the Sodium library which is more modern and has a layer of abstraction better to work than the openssl, in the end both options are very powerful.

You can also use this combined with ORM such Doctrine or other frameworks such Symfony, the next posts for this series will be the symmetric encryption and how to implement a powerful and useful encryption system with Symfony 7 and Doctrine 😮

no one can encrypt this

References 🔗

https://www.php.net/manual/en/book.openssl.php
https://www.cloudflare.com/learning/ssl/what-is-asymmetric-encryption/
https://en.wikipedia.org/wiki/Public-key_cryptography
https://en.wikipedia.org/wiki/Encryption
https://tresorit.com/blog/the-history-of-encryption-the-roots-of-modern-day-cyber-security/

Top comments (0)