Fundamental difference between Hashing and Encryption algorithms

前端 未结 13 2051
日久生厌
日久生厌 2020-11-21 06:35

I see a lot of confusion between hashes and encryption algorithms and I would like to hear some more expert advice about:

  1. When to use hashes vs encryptions<

相关标签:
13条回答
  • 2020-11-21 07:05

    Use hashes when you don't want to be able to get back the original input, use encryption when you do.

    Hashes take some input and turn it into some bits (usually thought of as a number, like a 32 bit integer, 64 bit integer, etc). The same input will always produce the same hash, but you PRINCIPALLY lose information in the process so you can't reliably reproduce the original input (there are a few caveats to that however).

    Encryption principally preserves all of the information you put into the encryption function, just makes it hard (ideally impossible) for anyone to reverse back to the original input without possessing a specific key.

    Simple Example of Hashing

    Here's a trivial example to help you understand why hashing can't (in the general case) get back the original input. Say I'm creating a 1-bit hash. My hash function takes a bit string as input and sets the hash to 1 if there are an even number of bits set in the input string, else 0 if there were an odd number.

    Example:

    Input    Hash
    0010     0
    0011     1
    0110     1
    1000     0
    

    Note that there are many input values that result in a hash of 0, and many that result in a hash of 1. If you know the hash is 0, you can't know for sure what the original input was.

    By the way, this 1-bit hash isn't exactly contrived... have a look at parity bit.

    Simple Example of Encryption

    You might encrypt text by using a simple letter substitution, say if the input is A, you write B. If the input is B, you write C. All the way to the end of the alphabet, where if the input is Z, you write A again.

    Input   Encrypted
    CAT     DBU
    ZOO     APP
    

    Just like the simple hash example, this type of encryption has been used historically.

    0 讨论(0)
  • 2020-11-21 07:08

    Cryptography deals with numbers and strings. Basically every digital thing in the entire universe are numbers. When I say numbers, its 0 & 1. You know what they are, binary. The images you see on screen, the music that you listen through your earphone, everything are binaries. But our ears and eyes will not understand binaries right? Only brain could understand that, and even if it could understand binaries, it can’t enjoy binaries. So we convert the binaries to human understandable formats such as mp3,jpg,etc. Let’s term the process as Encoding. It’s two way process and can be easily decoded back to its original form.

    Hashing

    Hashing is another cryptography technique in which a data once converted to some other form can never be recovered back. In Layman’s term, there is no process called de-hashing. There are many hash functions to do the job such as sha-512, md5 and so on.

    If the original value cannot be recovered, then where do we use this? Passwords! When you set up a password for your mobile or PC, a hash of your password is created and stored in a secure place. When you make a login attempt next time, the entered string is again hashed with the same algorithm (hash function) and the output is matched with the stored value. If it’s the same, you get logged in. Otherwise you are thrown out.

    Credits: wikimedia By applying hash to the password, we can ensure that an attacker will never get our password even if he steal the stored password file. The attacker will have the hash of the password. He can probably find a list of most commonly used passwords and apply sha-512 to each of it and compare it with the value in his hand. It is called the dictionary attack. But how long would he do this? If your password is random enough, do you think this method of cracking would work? All the passwords in the databases of Facebook, Google and Amazon are hashed, or at least they are supposed to be hashed.

    Then there is Encryption

    Encryption lies in between hashing and encoding. Encoding is a two way process and should not be used to provide security. Encryption is also a two way process, but original data can be retrieved if and only if the encryption key is known. If you don’t know how encryption works, don’t worry, we will discuss the basics here. That would be enough to understand the basics of SSL. So, there are two types of Encryption namely Symmetric and Asymmetric encryption.

    Symmetric Key Encryption

    I am trying to keep things as simple as I could. So, let’s understand the symmetric encryption by means of a shift algorithm. This algorithm is used to encrypt alphabets by shifting the letters to either left or right. Let’s take a string CRYPTO and consider a number +3. Then, the encrypted format of CRYPTO will be FUBSWR. That means each letter is shifted to right by 3 places. Here, the word CRYPTO is called Plaintext, the output FUBSWR is called the Ciphertext, the value +3 is called the Encryption key (symmetric key) and the whole process is a cipher. This is one of the oldest and basic symmetric key encryption algorithm and its first usage was reported during the time of Julius Caesar. So, it was named after him and it is the famous Caesar Cipher. Anyone who knows the encryption key and can apply the reverse of Caesar’s algorithm and retrieve the original Plaintext. Hence it is called a Symmetric Encryption.

    Asymmetric Key Encryption

    We know that, in Symmetric encryption same key is used for both encryption and decryption. Once that key is stolen, all the data is gone. That’s a huge risk and we need more complex technique. In 1976, Whitfield Diffie and Martin Hellman first published the concept of Asymmetric encryption and the algorithm was known as Diffie–Hellman key exchange. Then in 1978, Ron Rivest, Adi Shamir and Leonard Adleman of MIT published the RSA algorithm. These can be considered as the foundation of Asymmetric cryptography.

    As compared to Symmetric encryption, in Asymmetric encryption, there will be two keys instead of one. One is called the Public key, and the other one is the Private key. Theoretically, during initiation we can generate the Public-Private key pair to our machine. Private key should be kept in a safe place and it should never be shared with anyone. Public key, as the name indicates, can be shared with anyone who wish to send encrypted text to you. Now, those who have your public key can encrypt the secret data with it. If the key pair were generated using RSA algorithm, then they should use the same algorithm while encrypting the data. Usually the algorithm will be specified in the public key. The encrypted data can only be decrypted with the private key which is owned by you.

    Source: SSL/TLS for dummies part 1 : Ciphersuite, Hashing,Encryption | WST (https://www.wst.space/ssl-part1-ciphersuite-hashing-encryption/)

    0 讨论(0)
  • 2020-11-21 07:10

    A Hash function turns a variable-sized amount of text into a fixed-sized text.

    Hash

    Source: https://en.wikipedia.org/wiki/Hash_function


    Hash functions in PHP

    A hash turns a string to a hashed string. See below.

    HASH:

    $str = 'My age is 29';
    $hash = hash('sha1', $str);
    echo $hash; // OUTPUT: 4d675d9fbefc74a38c89e005f9d776c75d92623e
    

    Passwords are usually stored in their hashed representation instead as readable text. When an end-user wants gain access to an application protected with a password then a password must be given during authentication. When the user submits his password, then the valid authentication system receives the password and hashes this given password. This password hash is compared to the hash known by the system. Access is granted in case of equality.

    DEHASH:

    SHA1 is a one-way hash. Which means that you can't dehash the hash.

    However, you can brute-force the hash. Please see: https://hashkiller.co.uk/sha1-decrypter.aspx.

    MD5, is another hash. A MD5 dehasher can be found on this website: https://www.md5online.org/.

    To hamper brute-force attacks on hashes a salt can be given. In php you can use password_hash() for creating a password hash. The function password_hash() automatically creates a salt. To verify a password on a password hash (with a salt) use password_verify().

    // Invoke this little script 3 times, and it will give you everytime a new hash
    $password = '1234';  
    $hash = password_hash($password, PASSWORD_DEFAULT);  
    
    echo $hash; 
    // OUTPUT 
    
    $2y$10$ADxKiJW/Jn2DZNwpigWZ1ePwQ4il7V0ZB4iPeKj11n.iaDtLrC8bu 
    
    $2y$10$H8jRnHDOMsHFMEZdT4Mk4uI4DCW7/YRKjfdcmV3MiA/WdzEvou71u 
    
    $2y$10$qhyfIT25jpR63vCGvRbEoewACQZXQJ5glttlb01DmR4ota4L25jaW
    

    One password can be represented by more then one hash. When you verify the password with different password hashes by using password_verify(), then the password will be accepted as a valid password.

    $password = '1234';  
    
    $hash = '$2y$10$ADxKiJW/Jn2DZNwpigWZ1ePwQ4il7V0ZB4iPeKj11n.iaDtLrC8bu';  
    var_dump( password_verify($password, $hash) );  
    
    $hash = '$2y$10$H8jRnHDOMsHFMEZdT4Mk4uI4DCW7/YRKjfdcmV3MiA/WdzEvou71u';  
    var_dump( password_verify($password, $hash) );  
    
    $hash = '$2y$10$qhyfIT25jpR63vCGvRbEoewACQZXQJ5glttlb01DmR4ota4L25jaW';  
    var_dump( password_verify($password, $hash) );
    
    // OUTPUT 
    
    boolean true 
    
    boolean true 
    
    boolean true
    




    An Encryption function transforms a text into a nonsensical ciphertext by using an encryption key, and vice versa. enter image description here

    Source: https://en.wikipedia.org/wiki/Encryption


    Encryption in PHP

    Let's dive into some PHP code that handles encryption.

    --- The Mcrypt extention ---

    ENCRYPT:

    $cipher = MCRYPT_RIJNDAEL_128;
    $key = 'A_KEY';
    $data = 'My age is 29';
    $mode = MCRYPT_MODE_ECB;
    
    $encryptedData = mcrypt_encrypt($cipher, $key , $data , $mode);
    var_dump($encryptedData);
    
    //OUTPUT:
    string '„Ùòyªq³¿ì¼üÀpå' (length=16)
    

    DECRYPT:

    $decryptedData = mcrypt_decrypt($cipher, $key , $encryptedData, $mode);
    $decryptedData = rtrim($decryptedData, "\0\4"); // Remove the nulls and EOTs at the END
    var_dump($decryptedData);
    
    //OUTPUT:
    string 'My age is 29' (length=12)
    

    --- The OpenSSL extention ---

    The Mcrypt extention was deprecated in 7.1. and removed in php 7.2. The OpenSSL extention should be used in php 7. See the code snippets below:

    $key = 'A_KEY';
    $data = 'My age is 29';
    
    // ENCRYPT
    $encryptedData = openssl_encrypt($data , 'AES-128-CBC', $key, 0, 'IV_init_vector01');
    var_dump($encryptedData);
    
    // DECRYPT    
    $decryptedData = openssl_decrypt($encryptedData, 'AES-128-CBC', $key, 0, 'IV_init_vector01');
    var_dump($decryptedData);
    
    //OUTPUT
    string '4RJ8+18YkEd7Xk+tAMLz5Q==' (length=24)
    string 'My age is 29' (length=12)
    
    0 讨论(0)
  • 2020-11-21 07:13
    1. Use hashes when you only need to go one way. For example, for passwords in a system, you use hashing because you will only ever verify that the value a user entered, after hashing, matches the value in your repository. With encryption, you can go two ways.

    2. hashing algorithms and encryption algorithms are just mathematical algorithms. So in that respect they are not different -- its all just mathematical formulas. Semantics wise, though, there is the very big distinction between hashing (one-way) and encryption(two-way). Why are hashes irreversible? Because they are designed to be that way, because sometimes you want a one-way operation.

    0 讨论(0)
  • 2020-11-21 07:15

    A hash function could be considered the same as baking a loaf of bread. You start out with inputs (flour, water, yeast, etc...) and after applying the hash function (mixing + baking), you end up with an output: a loaf of bread.

    Going the other way is extraordinarily difficult - you can't really separate the bread back into flour, water, yeast - some of that was lost during the baking process, and you can never tell exactly how much water or flour or yeast was used for a particular loaf, because that information was destroyed by the hashing function (aka the oven).

    Many different variants of inputs will theoretically produce identical loaves (e.g. 2 cups of water and 1 tsbp of yeast produce exactly the same loaf as 2.1 cups of water and 0.9tsbp of yeast), but given one of those loaves, you can't tell exactly what combo of inputs produced it.

    Encryption, on the other hand, could be viewed as a safe deposit box. Whatever you put in there comes back out, as long as you possess the key with which it was locked up in the first place. It's a symmetric operation. Given a key and some input, you get a certain output. Given that output, and the same key, you'll get back the original input. It's a 1:1 mapping.

    0 讨论(0)
  • 2020-11-21 07:15

    when it comes to security for transmitting data i.e Two way communication you use encryption.All encryption requires a key

    when it comes to authorization you use hashing.There is no key in hashing

    Hashing takes any amount of data (binary or text) and creates a constant-length hash representing a checksum for the data. For example, the hash might be 16 bytes. Different hashing algorithms produce different size hashes. You obviously cannot re-create the original data from the hash, but you can hash the data again to see if the same hash value is generated. One-way Unix-based passwords work this way. The password is stored as a hash value, and to log onto a system, the password you type is hashed, and the hash value is compared against the hash of the real password. If they match, then you must've typed the correct password

    why is hashing irreversible :

    Hashing isn't reversible because the input-to-hash mapping is not 1-to-1. Having two inputs map to the same hash value is usually referred to as a "hash collision". For security purposes, one of the properties of a "good" hash function is that collisions are rare in practical use.

    0 讨论(0)
提交回复
热议问题