【问题标题】:Encrypt passwords in a WebApp在 WebApp 中加密密码
【发布时间】:2019-07-24 16:31:55
【问题描述】:

我正在使用 JavaEE(Servlets + JSP)开发一个 WebApp。

当我想在我的应用程序中写入一些密码时,我发现了一个问题,例如 SMTP 密码。在调试时,我已经在代码中或属性文件中以普通方式编写它,但我想以某种方式对其进行加密。

我在发展阶段做什么:

private static final String SMTP_PASS = "my_pass";

我怎么能这样做?有什么想法/例子吗?

【问题讨论】:

  • 加密密码只是踢罐子;现在您必须保护加密密钥而不是密码。你想保护什么?加密真的能解决这个问题吗?
  • A similar question 有一些建议。但就这一点而言,@Peter 只是在某种程度上解决了问题。像 AWS 这样的服务有更好的管理方式,但归根结底,您需要明文密码。
  • @Peter 问题是......我必须把我的 SMTP 密码放在任何地方,那么,写下它的最佳位置在哪里?

标签: servlets encryption password-encryption


【解决方案1】:
private static final String SMTP_PASS = "my_pass_identifier"; //here my_pass_identifier is not the actual password its just an identifier to identify the SMTP password

创建一个属性文件,用于以密钥/val 对的加密形式存储密码。请注意,您可以使用下面提到的 EncryptDecrypt 类对密码进行加密,并在属性文件中传递加密密码

SMTP_PASS = nPDHgg/DYzcL2+HsvYZruw==javaxMQyYxBZUsf7c0gh+vkisQA==javax0w+9tvuLzY04TA5FyTVSPw==

创建一个类 CredentialUtilities,它将通过读取 password.properties 文件来解密密码

public class CredentialUtilities {
    static PasswordEncrypt pe = new PasswordEncrypt();
    public static String  getCredentials(String identifier) throws Exception{

        String credential = "";
        Properties prop = new Properties();
        InputStream input = null;

        try {
           String filename = "password.properties";
            input = CredentialUtilities.class.getClassLoader().getResourceAsStream(filename);
            prop.load(input);
            String property = prop.getProperty(identifier);
            credential = pe.decrypt(property); 
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally{
            if(input!=null){
                try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            }
        }
        return credential;

    }
}

创建一个将为您加密/解密密码的类

import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EncryptDecrypt {
    public static String ALGORITHM = "AES";
    private static String AES_CBS_PADDING = "AES/CBC/PKCS5Padding";
    private static int AES_128 = 128;


    private static byte[] encryptDecrypt(final int mode, final byte[] key, final byte[] IV, final byte[] message)
            throws Exception {
        final Cipher cipher = Cipher.getInstance(AES_CBS_PADDING);
        final SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
        final IvParameterSpec ivSpec = new IvParameterSpec(IV);
        cipher.init(mode, keySpec, ivSpec);
        return cipher.doFinal(message);
    }

    public static Map<String, SecretKey> keyGenerator() throws NoSuchAlgorithmException{
        Map<String, SecretKey> map = new HashMap<String, SecretKey>();
         KeyGenerator keyGenerator = KeyGenerator.getInstance(EncryptDecrypt.ALGORITHM);
         keyGenerator.init(AES_128);
         SecretKey key = keyGenerator.generateKey();
         map.put("key", key);
         SecretKey IV = keyGenerator.generateKey();
         map.put("iv", IV);
         return map;

    }


    public static String encrypt(String message) throws Exception{
        Map<String , SecretKey> map = keyGenerator();
        SecretKey key = map.get("key");
        SecretKey IV = map.get("iv");
        byte[] cipherText = encryptDecrypt(Cipher.ENCRYPT_MODE, key.getEncoded(), IV.getEncoded(), message.getBytes());
        String encrypted_message =  Base64.getEncoder().encodeToString(cipherText);
        String encodedKey = Base64.getEncoder().encodeToString(map.get("key").getEncoded());
        String encodedIV = Base64.getEncoder().encodeToString(map.get("iv").getEncoded());

        return encrypted_message+"javax"+encodedIV+"javax"+encodedKey;



    }

    public static String decrypt(String encryptedMessage) throws Exception{
        String[] result = encryptedMessage.split("javax");
        byte[] decodedIV = Base64.getDecoder().decode(result[1]);
        byte[] decodedKey = Base64.getDecoder().decode(result[2]);
        byte[] cipher_text = Base64.getDecoder().decode(result[0]);
        SecretKey IV = new SecretKeySpec(decodedIV, 0, decodedIV.length, "AES");
        SecretKey key = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");    
        byte[] decryptedString = encryptDecrypt(Cipher.DECRYPT_MODE, key.getEncoded(), IV.getEncoded(), cipher_text);
        String decryptedMessage = new String(decryptedString);
        return decryptedMessage;

    }


    public static void main(String[] args) throws Exception {
        EncryptDecrypt cu = new EncryptDecrypt();
        String encryptedmessage =  cu.encrypt("usrpswd");
        System.out.println(encryptedmessage);
        String decryptedMessage = cu.decrypt(encryptedmessage);
        System.out.println(decryptedMessage);
    }

}

您现在可以在任何您想使用的地方获取解密后的密码。

String SMTP_PASSWORD = new CredentialUtilities().getCredentials(SMTP_PASS);

【讨论】:

  • Rajendra Gupta,您知道如何提高该算法的安全性吗?它是 128 位的,如果我修改数量会引发错误。
猜你喜欢
  • 2012-02-09
  • 2011-02-21
  • 2021-06-10
  • 2021-01-21
  • 2011-07-28
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多