Encryption
Published: 2023-04-24 15:04:56

I'm sure you've heard this term before. Most likely in a spy movie, as the characters try to hack a security system, or maybe in a history lesson about the famous German Enigma code. Many people know what encryption is, but few understand how encryptions function and their use in our day-to-day life. In this post, I will discuss the types of encryption, how they work, and their use in programming.

## What is encryption?
For those who haven't heard this term before, encryption is the process of converting information or data into a secret code in an attempt to prevent unauthorized access. Remember when you were little, and adults would spell words out to each other to prevent you from understanding their conversation? Encryption can be thought of just like that, but instead of spelling words out, encryptions use a key.

## How does encryption work?
Before understanding how encryption works, their keys need to be understood first. A key's purpose is to translate your data into code or your code into data. Keys use an algorithm that predictably transforms data. Say you want to create an encryption that ciphers messages. Here's a simple example of how this can be achieved.

JavaScipt has a method (charCodeAt) that takes in a string and an index and returns a number representative of the character at that index. Using this method, a message can be encrypted by iterating over an input string to find the next charCode of each character. To decrypt a message, the same method can be used to find the preceding (or previous) charCode of each character. Let's look at an implementation below.

```
const encryptKey = (str) => {
  // message represents the encrypted string that we will later ouput
  let message = '';
  // iterate over the input string
  for (let i = 0; i < str.length; i++) {
    // nextLetter is the next character's charCode
    let nextLetter = String.fromCharCode((str[i]).charCodeAt(0) + 1);
    // add the next letter to the output message
    message += nextLetter;
  }
  // after the loop is finished, return the encrypted message
  return message;
}

const decryptKey = (str) => {
  // message represents the decrypted string that we will late ouput
  let message = '';
  // iterate over the encrypted string
  for (let i = 0; i < str.length; i++) {
    // nextLetter is the preceeding character's charCode
    let previousLetter = String.fromCharCode((str[i]).charCodeAt(0) - 1);
    // add the next letter to the output message
    message += previousLetter;
  }
  // after the loop is finished, return the encrypted message
  return message;
}

let message = 'this message is classified';
let encryptedMessage = encryptKey(message);
let decryptedMessage = decryptKey(encryptedMessage);

console.log(message); // logs ==> this message is classified
console.log(encryptedMessage); // logs ==> uijt!nfttbhf!jt!dmbttjgjfe
console.log(decryptedMessage); // logs ==> this message is classified
```

## Types of encryption
Notice that the example above uses a key to encrypt a message and a separate key to decrypt a message. When separate keys are used to encrypt and decrypt data, the encryption is referred to as asymmetric. Some encryptions use the same key to encrypt and decrypt data. These encryptions are referred to as symmetric. 

Both types of encryptions carry unique costs and benefits. Symmetric encryptions are generally easier to crack as the key needs to be passed to the user (usually via the internet) which runs the risk of interception and makes keeping the key secure substantially more difficult. Furthermore, if the key is decyphered, both the user and the business can fall victim to attacks using malicious data. Although less secure, symmetric encryption is easier to implement and performs more quickly. When using an asymmetric system, the decrypting key can be held in private, while the encrypting key can be made public for anyone to use, without the worry of your code being deciphered. Asymmetric encryption also allows recipients to authenticate incoming data, making certain it isn't from an unauthorized sender.

## How is encryption used?
Encryption is used throughout the internet, from banking to retail to social media. It's easy to see concern when entering sensitive information into a website. No one wants their credit card number or banking information to be easily accessed. Yet, we often need to send them to online vendors to make purchases or pay bills. These are more evident cases that necessitate encryption, but there are also many less apparent cases. Having your favorite social media account hijacked isn't pleasant either. Whether the account is used for business or pleasure disappointment is always assured.

A cryptographer is a programmer that specializes in encrypting data. Cryptographers are some of the highest-paid developers, making an annual salary of $154,545 on average. Cybersecurity is a lucrative and highly demanding market. Novice and expert developers alike should certainly consider this field if they can hack it.


## Work Cited
- https://www.ziprecruiter.com/Salaries/Cryptography-Salary
- https://ico.org.uk/for-organisations/guide-to-data-protection/guide-to-the-general-data-protection-regulation-gdpr/encryption/what-types-of-encryption-are-there/#:~:text=There%20are%20two%20types%20of,used%20for%20encryption%20and%20decryption.
- https://www.techrepublic.com/article/asymmetric-vs-symmetric-encryption/#:~:text=Symmetric%20encryption%20can%20take%20128,RSA%202048%2Dbit%20or%20more.&text=Symmetric%20encryption%20is%20considered%20less,keys%20in%20encryption%20and%20decryption.