Forem Creators and Builders 🌱

Cover image for Harry Potter and the Secrets of Password Security: Encryption in JavaScript
Mariah Dominique Rucker
Mariah Dominique Rucker

Posted on • Updated on

Harry Potter and the Secrets of Password Security: Encryption in JavaScript

When it comes to Harry Potter’s world, magic is used to safeguard the Room of Requirement. However, it is possible to defend ourselves with just encryption, strong passwords in the world of cybersecurity.

This tutorial will show you password security, encrypting with Javascript on the wizarding world of Harry Potter.

Password Security
Spells are always employed in the wizarding world to guard Hogwarts. There is an example of that; in order to access the grounds of Gryffindor, one needs to change the particular password frequently.

Additionally, in the terrain of cyber security, passwords exist to shield and lock out unwarranted entries from our accounts and electronic gadgets.

Password Security

In order to ensure strong password security when using JavaScript, follow these best practices:

  1. Make your password long and hard to guess.
  2. One should avoid using a single password for several logins and sites as well.
  3. Use a secure hashing algorithm, and store all the password in the server-side.
  4. Ensure that you use libraries such as crypto and bcrypt for JavaScript password creation and storage. Crypto will produce random passwords while bcrypt will hashes and salts the passwords for storage.
  5. Whenever possible, implement two-factor authentication.

Encryption in the wizarding world is a technique of concealing important information and messages from people who are not authorised to view it. For instance, Dumbledore comes up with Protean change and produces magical pennies for the members of Dumbledore’s Army, so that they can communicate in secret. Hermione also employed Salvio Haxia while in the forest avoiding the Death Eaters.

Hermione & Salvio Hexia

Just as, in the realm of cybersecurity, one would use encryption to safeguard vital information and guard against unlawful entry. The message in this article shows that encryption algorithms employ difficult mathematical models to muddle up our data in a way that cannot be deciphered by humans unless properly unlocked with an appropriate encryption key. An instance include AES- 256 which is often used nowadays to secure data in the cloud.

Powerful enchantments like passwords in the world of Harry potter are employed to safeguard crucial data as well as prevent outsiders to get into it. These spells are highly complicated and use intricate magical formulae for encoding information so that it remains ciphered until an appropriate unlocking charm is applied.

Encrypt/Decrypt Diagram

The library called crypto in JavaScript supports encryption and decryption of data with known encryption algorithms including AES.

There are two main types of encryption: symmetric encryption and asymmetric encryption. Symmetric encryption uses a single key that both encrypts and decrypts data. Asymmetric encryption uses a public key to encrypt message and a private one to decipher it.

The Room of Requirement & Neville

It is similar to the room called “Room Of Requirement” that can be found in Harry Potter movies. You will need the same password to get in and out of the room. Nevertheless, one requires a separate password for entering the Room of Requirement as well as exiting thereon. Everyone knows the entrance password, while only trusted personnel know the exit password.

Generate a strong random password in JavaScript using the crypto library:

const crypto = require('crypto');

function generatePassword(length) {
  let password = '';
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+';

  for (let i = 0; i < length; i++) {
    password += characters.charAt(Math.floor(crypto.randomBytes(1)[0] / 255 * (characters.length - 1)));
  }

  return password;
}

// Example
const password = generatePassword(12);
console.log(password);
Enter fullscreen mode Exit fullscreen mode

Include the crypto library and develop a function named generate password which will accept a length value. In the function, create a string of all potential characters for the password and then use a for loop to go through the length of the password as well as pick a random character from the characters string by means of the crypto.randomBytes(1). Afterwards, return the generated password.

Harry Potter & Diagonal Alley

It was as if Harry Potter had to go the Diagon alley to acquire them needed ingredients for creating a new potion. For that, he needed to import the crypto library and declare the functions generatePassword that was taking the length argument. After that, he had to choose a piece of letters from the store called “Weasleys’ Wizard Wheezes” for this brew. Mixing in the chosen characters and generating the password was as simple as mixing of the ingredients to prepare the magical potion.

Calling generatePassword on the input argument of the length would yield a randomly generated password of that specific size.

Use the crypto library to encrypt and decrypt data using the AES encryption algorithm:

const crypto = require('crypto');

// The key and initialization vector (IV) used for encryption and decryption
const key = 'my-secret-key';
const iv = crypto.randomBytes(16);

// The data to be encrypted
const data = 'Hello, world!';

// Create a cipher object using the AES algorithm and the key and IV
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);

// Encrypt the data using the cipher object
let encryptedData = cipher.update(data, 'utf-8', 'hex');
encryptedData += cipher.final('hex');

// Print the encrypted data and IV
console.log('Encrypted data:', encryptedData);
console.log('IV:', iv.toString('hex'));

// Create a decipher object using the AES algorithm and the key and IV
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);

// Decrypt the data using the decipher object
let decryptedData = decipher.update(encryptedData, 'hex', 'utf-8');
decryptedData += decipher.final('utf-8');

// Print the decrypted data
console.log('Decrypted data:', decryptedData);
Enter fullscreen mode Exit fullscreen mode

Create an IV by calling crypto.randomBytes(). Then create a Cipher obj using crypto.createCipheriv(AES, key, IV). The cipher object will help you encrypt the data via cipher.update and cipher.final functions.

It’s like with a Time Turner you are going back into the time and doing some magic that will create the password for you. Within the enchantment, you establish a string of all possible characters which can be used in the password and then have a for loop that will run throughout the length of the password and pick up one character at random using the Time-Turner. The password that has been generated will be enchanted.

Hermione & Time Turner

You could also choose to generate a decipher object using the system.crypto.createDecipheriv() function that will accept the same AES encryption scheme, key, and IV as its arguments and would decrypt the encrypted data with the help of decipher.update() and decipher.

It is important to note that, the key for encryption and decryption must remain confidential and only authorized persons are supposed to possess it.

Password Security Tips

You started off with basic introduction about encryptions and password security while spicing up the concepts by drawing inspiration from wizardry in the Hogwarts world.

GitHub: github.com/mariahrucker
LinkedIn: linkedin.com/in/mariahrucker
Instagram: instagram.com/techmariah
Other: linktr.ee/mariahrucker

Top comments (0)