1. Home
  2. Explore
  3. Computer Science
  4. Securing Stored Passwords

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Securing Stored Passwords

Speaker 1Speaker 2

Securing Stored Passwords

19:21
0:00-19:21
  1. Maria

    Welcome to CS50’s Introduction to Cybersecurity. This episode is about securing data. We’ll start with passwords, then build toward encryption, digital signatures, passkeys, and ways to protect data while it travels or sits on a device.

  2. Michael

    Let’s begin with the server side of an account. When you sign up with a username and password, the service has to keep some information so it can authenticate you later—that is, check that you are who you claim to be. A dangerously simple approach would be to store usernames and passwords together in a text file.

  3. Maria

    For example, imagine a file containing “Alice: Apple” and “Bob: banana.” If an attacker gets that file, the passwords are exposed directly. And those passwords may be useful elsewhere, too, if people reused them—an attacker could try them on other websites in a credential-stuffing attack.

  4. Michael

    So a service should try to limit the damage if its database is compromised. One common technique is hashing. A hash function takes an input, such as a password, and produces a hash value: typically a fixed-length string of letters and numbers that doesn’t look like the original password.

  5. Maria

    You can picture a hash function as a black box, or as a mathematical function. The password goes in, and a hash comes out. Instead of storing “Apple” in the database, the service stores the hash of “Apple.” That way, a stolen database doesn’t immediately reveal a list of passwords in plain text.

  6. Michael

    A hash that simply returned the first letter’s position in the alphabet would be a poor example. Apple might become 1, and banana might become 2, but so would any other input starting with those letters. It would also leak an obvious clue about the password. A useful cryptographic hash looks much less revealing.

  7. Maria

    How can the service check your password later if it doesn’t store it? When you first register, it hashes the password and stores the username and hash, then discards the password. At your next login, it hashes what you typed and compares the new hash with the stored one. If they match, the service treats you as authenticated.

  8. Michael

    That does add some work, but it’s practical. In real systems, developers generally use established libraries rather than writing their own hash functions. And hashing is not the same as reversing a password: the service can recompute a hash to compare it, but the hash is designed not to reveal the original input.

  9. Maria

    Hashing makes a system more secure, not perfectly secure. If an attacker steals a password-hash file, they can still guess passwords. They can hash dictionary words, try likely choices such as fruit names, and compare each result with the stolen hashes.

  10. Michael

    Or they can use brute force: try sequences such as zeros, then other numbers and letters, hash each attempt, and compare. The attacker has to do more work than when passwords are stored in plain text, but the work can still be feasible, particularly if the passwords are short or predictable.

  11. Maria

    There’s also a way to do some of that work in advance. A rainbow table is a precomputed collection of passwords or guesses and their hash values. An attacker who has one can compare stolen hashes with values already calculated instead of doing all the calculations at the time of the attack.

  12. Michael

    Precomputing and storing a large table takes resources, so it isn’t equally practical for every hash function or every set of guesses. But it can speed up attacks against likely passwords. Once again, hashing raises the bar; it doesn’t guarantee that nobody can discover a password.

  13. Maria

    Here’s another issue. If Carol and Charlie independently choose the same password, a basic deterministic hash gives them the same hash. An attacker who sees the database can tell that the two accounts share a password, even without knowing what it is.

  14. Michael

    That pattern can leak clues. The attacker might use what they know about the people or their shared interests to make better guesses. A common password used by many people can also make it easier to identify which accounts are likely to be vulnerable.

  15. Maria

    The standard way to reduce that problem is salting. A salt is an additional value provided to the hash function along with the password. The same password paired with different salts produces different hash values, making it harder to spot matching passwords just by looking at the database.

  16. Michael

    The salt doesn’t need to be secret. It’s generated by the system and stored with the hash, so the system can use it again at login. If Carol’s stored value indicates her salt, the server hashes her submitted password using that salt and compares the result with Carol’s stored hash.

  17. Maria

    Different users should have different salts, though salts can occasionally repeat if there are enough users and a limited set of possible values. The goal is not to make attacks impossible, but to reduce information leakage and make password guessing more difficult.

  18. Michael

    Modern stored password hashes are typically longer and more structured than the simple examples we’ve been using. Their format can include an identifier for the hash method and information about the salt. That lets the system know how to check a password later.

  19. Maria

    Length matters because a hash value has a finite number of possibilities. A hash function can take inputs of varying lengths but produces a fixed-length output. More possible hash values can make guessing much harder, though a weak password can still be guessed early.

  20. Michael

    The key mathematical idea is that the input space is larger than the fixed output space. Different inputs must sometimes produce the same output. So a hash cannot be reliably reversed to identify one unique original input; there may be multiple inputs that yield the same hash.

  21. Maria

    That leads to a theoretical caveat: if two different inputs produce the same hash, both might be accepted by a system that checks only the hash. But with suitable modern hash functions, the chance of finding such an alternative input is intended to be extremely low.

  22. Michael

    A practical warning sign is a website that emails you your original password. If it can retrieve and send that password, it must have access to it in a form that can be recovered. That’s not how a properly one-way password hash is supposed to work.

  23. Maria

    If that happens, don’t reuse that password elsewhere, and consider stopping use of the service. You could also tell the company why you’re concerned. More broadly, organizations may have ethical and legal responsibilities after a breach, but the legal requirements depend on the country, industry, and rules that apply.

  24. Michael

    The best-practice principle is captured by guidance from NIST: stored memorized secrets should resist offline attacks and be salted and hashed using a suitable one-way key-derivation function. The aim is to make each password guess expensive for an attacker who has obtained the hash file.

  25. Maria

    That’s also why security professionals rely on well-studied standards instead of inventing their own cryptographic systems. Algorithms such as SHA-2 and SHA-3 are examples of cryptographic hash families, but password storage calls for an appropriate password-hashing method, not just any hash used carelessly.

  26. Michael

    And a company generally shouldn’t depend on hiding its hash algorithm. The algorithms should be public and scrutinized; the data meant to remain secret—such as a password or encryption key—should be protected. Keeping the algorithm secret is not a substitute for using it correctly.

  27. Maria

    Now let’s broaden the subject from hashes to cryptography: the practice and study of securing data, whether it is stored on a device or sent from one place to another. One historical approach is a code, which maps words or phrases to other words or phrases using a codebook.

  28. Michael

    To encode is to turn a plaintext message into codetext using that mapping. To decode is to use the codebook to recover the original message. A codebook can make a message hard to understand for someone who lacks it, but it’s cumbersome, and if the book is stolen, messages can be exposed.

  29. Maria

    A cipher is different: it applies an algorithmic procedure, often to individual letters or bits. To encipher—or encrypt—is to transform plaintext into ciphertext. To decipher—or decrypt—is to apply the reverse process to recover the plaintext.

  30. Michael

    A cipher is configured with a key. In digital systems, a key is a value—ultimately represented by bits—that controls how the algorithm operates. The algorithm can be public; the key is what must be kept secret in many forms of encryption.

  31. Maria

    A simple example is the Caesar cipher, which rotates letters in the alphabet. With a key of one, A becomes B and B becomes C. A key of thirteen gives a different rotation. This is useful for illustrating the idea, but it is not secure: an attacker can try the small number of possible keys.

  32. Michael

    A rotation of twenty-six would leave English letters unchanged, because it goes all the way around the alphabet. ROT13 is sometimes used to obscure things such as spoilers, but it’s easy to undo and is not intended to secure sensitive information.

  33. Maria

    Modern secret-key encryption is much more sophisticated. It’s also called symmetric-key encryption because the sender and recipient use the same key. AES and triple DES are examples of algorithms used in this category.

  34. Michael

    The sender supplies plaintext and the shared key to the encryption algorithm and gets ciphertext. The recipient uses the same key with the decryption algorithm to recover the plaintext. The key should be large and chosen according to the standard’s recommendations, rather than something tiny like the Caesar cipher’s one-to-twenty-five choices.

  35. Maria

    But symmetric encryption creates a chicken-and-egg problem: how do two people who haven’t communicated before agree on a secret key without exposing it? You can’t securely send someone a key if you don’t already have a secure way to communicate.

  36. Michael

    Public-key cryptography, also called asymmetric-key cryptography, offers a different approach. Each person has a mathematically related public key and private key. The public key can be shared; the private key must stay private.

  37. Maria

    If someone wants to send you a confidential message, they can encrypt it with your public key. Your private key is the one that can decrypt it. That way, the sender doesn’t need to share a secret key with you in advance.

  38. Michael

    RSA is a well-known public-key algorithm. At a high level, it uses very large prime numbers. The computer combines two primes into a value used in the rest of the mathematics; the difficulty of recovering the original primes from their product is part of the security idea.

  39. Maria

    You don’t need to memorize the RSA formulas to understand the purpose. The algorithm uses related public and private values so that one key can encrypt and the other can decrypt. Real implementations involve more detail, and they should use established, well-tested standards.

  40. Michael

    Another public-key technique, Diffie-Hellman, is used for key exchange. It lets two parties calculate a shared secret even though the secret itself is never sent across the network in its entirety. They exchange values derived from their private numbers and agreed-upon public values.

  41. Maria

    Once Alice and Bob have calculated the same shared secret, they can use it with a symmetric cipher such as AES. The mathematics is sophisticated, which is another reason not to design your own cryptographic system: standards have been analyzed by researchers and practitioners.

  42. Michael

    Public and private keys can also support digital signatures. To sign a document, the signer hashes it to get a fixed-length representation, then uses their private key with that hash to produce a signature.

  43. Maria

    The signer sends the document and the signature to the recipient. The recipient hashes the document independently, then uses the signer’s public key to verify the signature. If the verification produces the same hash, that supports the conclusion that the document was signed with the corresponding private key and has not changed.

  44. Michael

    A digital signature is not just an image of a handwritten signature. Its security depends on keeping the private key private and on trusting the public key really belongs to the person it names. Public keys might be distributed through a registry or shared through channels such as email or a website, with trust depending on how that information is verified.

  45. Maria

    Digital signatures are useful for checking authenticity and integrity: who signed something, and whether it changed. They don’t, by themselves, make the document confidential. Also, a signature should be extremely difficult to forge when the standards are sound and the private key has not been compromised.

  46. Michael

    This gives us a useful contrast. Hashing is generally one-way: it condenses data into a fixed-size value and discards information, so you can’t reliably recover the original. Encryption is meant to be reversible with the right key, because the recipient needs to recover the protected message.

  47. Maria

    Passkeys use these public-key ideas to reduce dependence on passwords. When you register with a passkey, your device may ask you to confirm access with a fingerprint, face scan, or PIN. Then it creates a public-private key pair for that particular website or app.

  48. Michael

    The website receives your public key and identifying information, but not your private key. Later, when you log in, the website sends a randomly generated challenge. Your device signs that challenge with your private key and returns the signature.

  49. Maria

    The website checks the signature using the public key it stored when you registered. If it verifies against the challenge, the site has evidence that the device has the corresponding private key. That can authenticate you without asking you to create, remember, or type a password.

  50. Michael

    Passkeys are tied to the website or app they were created for, and they depend on retaining access to the relevant keys. Losing a device can create a problem, though services may synchronize passkeys across devices. Availability is growing, but passkeys are not offered everywhere yet.

  51. Maria

    Next, distinguish encryption in transit from end-to-end encryption. Encryption in transit protects data as it travels over a connection, such as your connection to an email service. But if both Alice and Bob connect securely to a service in the middle, that does not necessarily mean their messages are encrypted from Alice all the way to Bob.

  52. Michael

    The service itself might be able to access the messages. The same distinction can matter for services such as video conferencing: having an encrypted connection to the service doesn’t necessarily prevent the service from seeing or hearing the content.

  53. Maria

    End-to-end encryption aims to protect the message from the sender to the recipient, even if it passes through servers in between. Those servers should see ciphertext rather than the readable message, provided the system implements end-to-end encryption correctly and the relevant keys remain protected.

  54. Michael

    Whether you get that protection depends on the service and the features you use. Some services offer end-to-end encryption; others may encrypt connections to their servers while retaining the technical ability to access the data they store.

  55. Maria

    Let’s turn to files on your own device. When you move a file to the Recycle Bin or Trash, and even when you empty it, the operating system usually doesn’t immediately erase every bit. It often removes the file’s entry from its index and marks the space as available for reuse.

  56. Michael

    That means the data may remain on the drive until other data overwrites it. Some parts of a deleted file can linger in the meantime. So ordinary deletion is not necessarily secure deletion, especially if the file contained sensitive information.

  57. Maria

    Secure deletion is intended to replace the old data with something else—for example, zeros, ones, or random-looking bits—so the original pattern is no longer there. But storage devices can complicate this, and waiting for ordinary reuse doesn’t guarantee that every remnant disappears when you want it to.

  58. Michael

    That’s one reason full-disk encryption is valuable. It is a form of encryption at rest: data stored on the device is encrypted, and the system decrypts it when you unlock it with a password or another approved method, such as a fingerprint or face scan.

  59. Maria

    If a locked device is stolen, full-disk encryption is meant to keep the data unreadable to someone who lacks the means to unlock it. It can also help when you dispose of a device: if the data has always been encrypted and the new owner lacks the needed credentials, the old data should remain inaccessible.

  60. Michael

    It’s especially useful to enable encryption from the start. Some storage devices can wear out, and their firmware may stop allowing changes to certain areas. If data was encrypted before that happens, even older remnants are protected by encryption rather than left in plain form.

  61. Maria

    There is a trade-off. If you lose the password or other means of unlocking an encrypted device, you may lose access to your own data too. Security measures can protect against others while making recovery harder for you.

  62. Michael

    Encryption can also be used maliciously in ransomware attacks. An attacker who gets into a poorly protected system may encrypt its data and demand payment for the key. This has affected individuals and organizations, including companies, universities, hospitals, and municipalities.

  63. Maria

    Paying a ransom does not guarantee that the attacker will provide the right key or restore the data. The same powerful technique that protects your files when you control the key can be turned against you when an attacker controls it.

  64. Michael

    Finally, there’s a longer-term concern: quantum computing. Ordinary computers represent information with bits that are either zero or one. A quantum bit, or qubit, can be used to represent combinations of states, and collections of qubits can represent many possibilities at once.

  65. Maria

    That could change the cost of attacks that rely on trying an enormous number of possibilities. If adversaries gain powerful quantum computers, some of the security assumptions behind today’s cryptography could be challenged. The field will need to evolve its methods as computing capabilities change.

  66. Michael

    So the common thread is that security is about choosing the right tools and using them correctly: salted password hashes to limit damage from stolen databases, encryption to protect data, signatures to check authenticity, and secure storage practices to protect information at rest.

  67. Maria

    And none of these is a reason to invent your own cryptography or assume that one measure solves every problem. Use well-established standards, understand what each technique does—and doesn’t do—and remember that security is about reducing risk, not promising that risk disappears.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent