Basic password encryption
Lesson 7 of 8 in Coddy's Register Login System Project course.
Passwords are dangerous. If someone not authorized can get your passwords he can do malicious things to your account. To prevent people of doing such things we need to encrypt the passwords.
Encrypted password is when it is converted into secret code that only the system can understand.
For example let's assume that my password is abc and let's create a secret language: the letter a is actually the letter x and the letter b is actually the letter a and the letter c is actually the letter v:
a -> x
b -> c
c -> v Finally our encrypted password is: xcv. This is how we save it in the system. Now if someone managed to hack our system and steal all the stored passwords, he will find only encrypted password that don't mean anything to him.
Now, when the user will try to log in to the system he will type his normal password: abc, the system then will convert it to an encrypted password: xcv and check if the encrypted password that the user typed in matches the stored encrypted password in the system.
Note: Never use this kind of encryption in real life scenario! This type of encryption is too weak and easy to break.
Challenge
MediumCreate a self variable called mapping when initializing (__init__) the system that stores the encryption language:
mapping = {
"a" : "i", "b" : "l", "c" : "q", "d" : "f", "e" : "z", "f" : "s",
"g" : "a", "h" : "g", "i" : "e", "j" : "p", "k" : "w", "l" : "o",
"m" : "v", "n" : "u", "o" : "b", "p" : "j", "q" : "k", "r" : "n",
"s" : "x", "t" : "d", "u" : "h", "v" : "y", "w" : "t", "x" : "m",
"y" : "r", "z" : "c"
}Create a method called encrypt(self, password) that returns the encrypted password using the mapping dictionary.
Finally when someone tries to register store the encrypted password instead of the normal one, and when someone tries to log in do the necessary checking.
To make it happen follow the steps:
- Create a
mappingvariable that is given in this challenge and save it somewhere in the class so that you could access it via theencryptmethod - Create the
encrypt(self, password)method and then use it for the next steps - When registering a user save his password as an encrypted password in the
usersdictionary - When a user tries to log in make sure to encrypt the given input password and check if it matches the saved password in the
usersdictionary
Try it yourself
class LoginSystem:
def __init__(self):
self.users = {}
self.logged_users = set()
def register(self, username, password):
if username not in self.users:
self.users[username] = password
print("user registered successfuly")
else:
print("user already exists")
def login(self, username, password):
if username in self.users:
if password == self.users[username]:
self.logged_users.add(username)
print("user logged in successfully")
else:
print("password doesn't match")
else:
print("user isn't in the system")
def sign_out(self, username):
if username not in self.users:
print("user is not in the system")
elif username not in self.logged_users:
print("user is not logged in")
else:
self.logged_users.remove(username)
print("user signed out successfully")