All right, so we've taken a look at level one encryption, which is basically just storing the password
as plain text in our database.
So maybe it'll be a little bit difficult for people to get access to our server and access our database.
At least you can't just simply
right-
click on a website to view page source and be able to see it in the HTML.
At least it's stored at server level. But that's not really good enough.
So let's go ahead and see what we can do to improve the security for our users on our website.
So let's increase to level two authentication. And level two authentication involves the use of encryption.
So what exactly is encryption?
Well, basically all it is is just scrambling something so that people can't tell what the original
was unless they were in on the secret and they knew how to unscramble it.
This is exactly the same as if you and your friend were sending each other secret messages and you had
a key to encode the message that you both knew that so that you could decode the message.
Now, on a bigger scale, if you've ever watched The Imitation Game or read about the Enigma machine,
well, that is basically a form of encryption.
And the Enigma machine, if you don't know, is just simply a machine that was used during World War
2 when the Germans would send each other messages,
they would use the machine to encrypt those messages so that when the messages are intercepted, say,
over the radio, unless you had the same Enigma machine and you knew what the decoding key was or what
the settings were for the machine, then you wouldn't be able to tell what it is that they were trying
to communicate with each other.
If you're interested, I really recommend watching two videos that were done by Numberphile on YouTube and
I've linked to it in the course resources list.
But it explains the Enigma machine and it talks about the flaw in the Enigma machine that led Alan Turing
and other people at Bletchley Park to be able to crack the code and create what was very much a specialized
computer to be able to decode those messages and helped the allies win the war.
And if you ever visit London, be sure to go and check out Bletchley Park and they have a computer museum
next to it as well, which is super fascinating.
Anyways, I digress.
So back to
ciphers and encryption, one of the earliest ways of encrypting messages that we know about is the Caesar
cipher.
And this comes from Julius Caesar, who was one of the generals in the Roman Empire.
And what he did is he would send messages to his generals and he would encrypt it
so if his messenger got murdered along the way, then his messages would be kept secret.
And this is one of the simplest forms of encryption we know about
and it's very simple.
Let's say we have the alphabet, right? ABCDEFG. All that the Caesar Cipher does
is a letter substitution cipher.
And the key for the cipher is the number of letters that you would shift by.
So if you knew what the shift pattern was, then you could really quickly decipher the message.
So if we were to encrypt the word hello, there's a really neat tool online that can help us do that.
It's called cryptii.com and it's got two 'i's at the end.
And you can basically choose the kind of cipher or encryption that you want to use
and then you can specify the shift and we're going to say a shift of three, let's say.
So if my text was hello, then it becomes shifted into khoor. And to an unknowing person and a non-cryptographer,
it can be quite difficult to see at a glance what exactly this is trying to say. Now in modern days and
with modern cryptography, this is overly simplistic and it's very, very easy to crack.
But there are other forms of encryption which are a little bit more complicated and it involves a lot
more math to make it more time consuming for somebody to crack.
But essentially all encryption works exactly the same way.
You have a way of scrambling your message and it requires a key to be able to unscramble that message.
All right.
So now it's time to level up to the next level of security.
And in this lesson, we're going to cover something called hashing.
Now, previously, we've already looked at encryption, so taking the user's password and securing it
using an encryption key, and then
using a particular cipher method, be it a Caesar cipher or the Enigma cipher, no matter which way
we chose, we always had a password, a key, and we ended up with some ciphertext which will make it
hard for people to be able to immediately guess what our user's password is.
So, for example, if we took a password like qwerty and we use the Caesar cipher method and we decided to
shift it by one, then our encryption key is the number one.
And that creates the ciphertext where every single letter is shifted up by one.
Now, in order to decrypt this, all you have to do, as long as you know what the key is, then you
can simply shift all of the ciphertext down by one and you end up with the original password.
Now, the Caesar cipher is a very, very weak encryption method.
It's incredibly easy to figure out what the original text was, even if you didn't have a key.
And just to illustrate what bad things can happen when you have a weak encryption system, I'm going
to tell you a story from history that tells us why we should not be using a weak encryption system.
So back in the 1500's on this island that we now call the United Kingdom, there used to be two large
areas.
One was Scotland and the other was England.
And they were ruled over by two Queens.
Scotland was ruled by Mary Queen of Scots, who was a Catholic, and England was ruled over by Queen
Elizabeth the first.
Now, these two ladies between them controlled the land that we now call the UK, but they each wanted
to have more power and more land.
So what did they do?
Well, Mary Queen of Scots who ruled over Scotland decided to plot with her friend, Lord Babington,
to try and assassinate Queen Elizabeth.
That way, she would be the legitimate heir to both the English and Scottish throne,
and it was kind of a Game of Thrones kind of situation going on back then.
But in order to mobilize their forces or try to come up with some sort of secret plan, they decided
to send letters to each other using ciphertext.
So they came up with a system to encrypt their letters to each other such that if it fell into the wrong
hands, the subject of the letter wouldn't be revealed and they wouldn't end up being tried for treason.
But the problem was that the encryption method that they used, which was a letter substitution method
similar to the Caesar cipher, was a very weak form of encryption.
And Queen Elizabeth had a chief decoder who ended up deciphering their letters and figuring out what
their encryption key was.
So he decided to take this encryption key and write a letter back to Lord Babington to try and get him
to reveal all of the co-conspirators.
And what was the end result of having their weak encryption system?
Well, Queen Elizabeth decided to accuse Mary Queen of Scots of treason, and hence she ended up having
her head chopped off.
So this is not what you want to happen to you or your website.
So weak encryption systems can end up putting user passwords at risk and your company might end up metaphorically
decapitated, such as in the case of companies like TalkTalk or Equifax, where they ended up getting
hacked and lost a lot of the trust of their users.
Now, if you're interested in more stories like this and to learn more about cryptography and encryption,
there's a really great book recommendation I would make called The Code Book by Simon Singh.
It contains stories like the one that I just told you and more.
So if you're interested in this, go ahead and read more about it.
Now, how can we make our password more secure Now,
at the moment, the biggest flaw in our authentication method is the fact that we need an encryption
key to encrypt our passwords and decrypt our passwords.
And chances are that if somebody is motivated enough to spend time and hack into your database, then
it's probably not that difficult for them to also be able to get your encryption key, even if you've
saved it in environment variable or somewhere secure on your server.
So how can we address this weakest link, the need for that encryption key?
Well, here is where hashing comes into play. Whereas previously with encryption we needed that encryption
key, hashing takes it away and no longer requires the need for an encryption key.
Well, then you might ask, well, if we don't have an encryption key, how can we decrypt our password
back into
plain text? Well, the secret is you don't. Let's say a user registers on our website and they enter
a password to register with, we use something called a hash function to turn that password into a hash
and we store that hash in our database.
Now, the problem is that hash functions are mathematical equations that are designed to make it almost
impossible to go backwards.
So it's almost impossible to turn a hash back into a password.
How is this possible, you might ask?
How is it possible that you can turn a password into a hash very quickly and easily, but make it almost
impossible to turn that hash back into a password?
Well, here's a question.
Let me ask you, what are the factors of 377 other than one and 377?
So basically, I'm saying 377 is not a prime number.
Not only can you divide 377 by 1 and 377, but there's also two other numbers that you
can divide it by.
Now it's your job to figure out what those numbers are.
So, what might you do?
Well, you might divide it by two.
OK, so that becomes 188.5.
That's not a whole number
so 2 is not a factor.
What if you divide it by three?
Well, that becomes 113.3 recurring, which is also not a whole number.
So three is not a factor either.
And you might go through this process for a long time, tediously going through number by number.
Well, then you might arrive at the point where you divide 377 by 13 and you end up with
29.
So 13 and 29 are the answers to this question. They are the only factors of 377 other than 1 and
377.
And as you can see, that process of getting to this point of finding those two factors took us a while,
right?
It wasn't that easy.
But consider if I asked you a different question.
If I said to you, can you multiply 13 by 29?
Well, you would be able to do that really quickly and easily.
It would take you almost no time at all to figure out that the answer is 377.
So here is a very, very simplified version of a hash function.
So going forward, multiplying 13 by 29 is really quick and easy, but going backward, trying
to get back those numbers
13 and 29 starting from 377 is very, very time consuming.
So this is essentially how a hash function works.
Just add a little bit more complexity and you end up with a real hash function.
So they're designed to be calculated very quickly going forwards, but almost impossible to go backward.
And by almost impossible
I simply mean that using current levels of computing power, it would take far too long to make it worthwhile
for the hacker.
So let's say that to calculate the hash going forward, it takes a millisecond, but to go backward
it takes two years, then that hacker probably has better things to do with his time.
So when a user tries to register on our website, then we ask them for the registration password, which
we turn into a hash using our hash function, and then we store that hash on our database.
Now, at a later point when the user tries to log in and they type in their password, then we again
hash that password that they typed in to produce a hash and then we compare it against the hash that
we have stored in our database.
And if those two hashes match, then that must mean that the login password is the same as the registration
password as well.
And at no point in this process do we have to store their password in plain text or are we able to reverse
the process to figure out their original password?
The only person who knows their password is the user themselves.
Now, previously we saw that by using the Enigma machine, as long as we knew what the settings were
for the Enigma machine, which is basically the encryption key,
right?
As long as we knew what that was, then I can decode it by setting it to the same encryption key.
And we end up being able to retrieve the original text. Now, however, if I was to go and change this
to a hash function instead, then you can see that when we try to decode this using the same hash function,
MD5, we get the error
that decoding step is not defined for hash function because you can't really go back.
That's the whole point of the hash function and this is what will make our authentication more secure.