开发者

How do you protect data that you don't want to hash?

开发者 https://www.devze.com 2023-03-22 06:22 出处:网络
Correct me if any of my assumptions are off. 开发者_高级运维When you hash something like with sha1, you can\'t reverse the hashed data to get the original string.

Correct me if any of my assumptions are off.

开发者_高级运维When you hash something like with sha1, you can't reverse the hashed data to get the original string.

Because of this, if I have an email, which I will need to use later, stored in the database, I can't use sha1 on it.

However, I still want to protect in case of a breach, so what do I do?

I'm using django which stores a secret_key in settings.py.

I tried using AES encryption, but noticed that as the string encoded is longer, the encrypted string returned is longer, which makes sense. However, the encryption string is very much longer than the original string. Is there a type of encryption where the string returned is the same size of the original string? Cuz I'm using django user model and the email is limited to 75, so if a user used a 32-75char email, the encrypted string is 128 in length which is > 75, so it can't be stored in the column.


The three key concepts of information security are confidentiality, integrity, and availability. In your case, a cryptographic hash like SHA1 provides integrity: you can always check against the hash value to see if the email has been tampered with. In your case, you want confidentiality, which a hash function will not provide: you want emails to be unreadable in the database in case the database is compromised. While a symmetric encryption algorithm is part of the answer, the bigger question is about key management. Once you have a key to encrypt and decrypt emails, how will you store it? How many people will have access to the key? Will it be kept on the same computer as the database? (That's dangerous.) Will it be kept on the same network? How often will you change the keys? What happens if you lose the key? In all likelihood, your infrastructure will be just as vulnerable to a data breach with unencrypted emails as you would be an encrypted ones. Security is hard, and it's better to focus your efforts on auditing your database setup -- which is something many people have done, has well-known and production-tested solutions -- rather than creating a complicated cryptographic system.


Probably there is no way of such a protection by encryption suiting your needs. Your solution might be to keep your database and the whole system safe (keep an eye on the security mail lists of used software, install security updates, use safe passwords, ...).

Encryption is a process where data is made unreadable by using a secret pass phrase (or a public key) which is needed also to decrypt the data again (or the responding private key). So probably you will have to store then your secret pass phrase or the private key on your system and the data isn't protected more than before.

But you are right hashing is not applicable if the original data has to be restored. Usually hashing is used to obscure passwords.


Encrypt it using any cryptographically secure method.


Hashing algorithms are one way encryption methods. This means that you can encrypt data but not return it. That is its purpose.

You need a two way encryption process, which allows you to do both ways. See this SO question for more details.


Try the blowfish algorithm.

BlowfishCipher sample from this website.

package com.ack.security.jce;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;

/**
 * This program demonstrates how to encrypt/decrypt input
 * using the Blowfish Cipher with the Java Cryptograhpy.
 *
 */
public class BlowfishCipher {

  public static void main(String[] args) throws Exception {

    // create a key generator based upon the Blowfish cipher
    KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");

    // create a key
    SecretKey secretkey = keygenerator.generateKey();

    // create a cipher based upon Blowfish
    Cipher cipher = Cipher.getInstance("Blowfish");

    // initialise cipher to with secret key
    cipher.init(Cipher.ENCRYPT_MODE, secretkey);

    // get the text to encrypt
    String inputText = JOptionPane.showInputDialog("Input your message: ");

    // encrypt message
    byte[] encrypted = cipher.doFinal(inputText.getBytes());

    // re-initialise the cipher to be in decrypt mode
    cipher.init(Cipher.DECRYPT_MODE, secretkey);

    // decrypt message
    byte[] decrypted = cipher.doFinal(encrypted);

    // and display the results
    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                                  "encrypted text: " + new String(encrypted) + "\n" +
                                  "decrypted text: " + new String(decrypted));

    // end example
    System.exit(0);
  }
}


You'll want to encrypt the data. It's WAY simpler than it sounds. Read on! :)

I recommend you use what is called a symmetric algorithm, in which the same secret key is used to encrypt and decrypt the data. The most popular is the latest version of the DES algorith, which is called 3DES, or Triple DES.

If you're using .NET, then use the System.Security.Cryptography library and the TripleDESCryptoServiceProvider class. There are tons of samples out there. When you search for sample code, be sure to use the term 'TripleDESCryptoServiceProvider' and whatever language/system you're programming in (C#, vb, asp.net).

If you're using something other than .NET, then look for that language/framework's built-in or add-on Triple-DES library. Here are some examples:

  • PHP: http://php.net/manual/en/ref.mcrypt.php
  • JAVA: http://www.java2s.com/Code/Java/Security/TripleDES.htm
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号