如何加密Java Properties敏感信息

发布时间:2025-03-25 08:39:26 作者:小樊
来源:亿速云 阅读:106

在Java中,Properties文件通常用于存储配置信息,如数据库连接字符串、用户名和密码等敏感信息。为了保护这些敏感信息,可以使用以下方法对它们进行加密:

  1. 使用Java加密扩展(Java Cryptography Extension, JCE)库对敏感信息进行加密和解密。JCE提供了对称加密算法(如AES)和非对称加密算法(如RSA)。

  2. 在Properties文件中,将敏感信息替换为加密后的字符串。例如,将数据库密码从password=mysecretpassword更改为password=ENC(encrypted_password)

  3. 在应用程序启动时,使用自定义的PropertyPlaceholderConfigurer或类似的方法,对加密的敏感信息进行解密,并将其设置回Properties对象。

以下是一个简单的示例,说明如何使用AES算法对敏感信息进行加密和解密:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AESUtil {
    private static final String ALGORITHM = "AES";
    private static final String KEY = "your-secret-key"; // 16字节长度的密钥

    public static String encrypt(String value) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(value.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedValue) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decodedBytes = Base64.getDecoder().decode(encryptedValue);
        byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        return new String(decryptedBytes);
    }
}

在应用程序启动时,使用自定义的PropertyPlaceholderConfigurer或类似的方法,对加密的敏感信息进行解密,并将其设置回Properties对象:

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class EncryptedPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    @Override
    protected Properties mergeProperties(Properties propsFromResources) throws IOException {
        Properties properties = super.mergeProperties(propsFromResources);
        decryptProperties(properties);
        return properties;
    }

    private void decryptProperties(Properties properties) {
        properties.forEach((key, value) -> {
            if (value instanceof String && ((String) value).startsWith("ENC(")) {
                try {
                    String decryptedValue = AESUtil.decrypt((String) value.substring(4, value.length() - 1));
                    properties.put(key.toString(), decryptedValue);
                } catch (Exception e) {
                    throw new RuntimeException("Failed to decrypt property: " + key, e);
                }
            }
        });
    }

    @Override
    protected InputStream getResourceAsStream(String location) throws IOException {
        return new ClassPathResource(location).getInputStream();
    }
}

在Spring配置文件中,使用自定义的EncryptedPropertyPlaceholderConfigurer替换默认的PropertyPlaceholderConfigurer:

<bean id="propertyConfigurer" class="com.example.EncryptedPropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
        </list>
    </property>
</bean>

现在,当应用程序启动时,它将自动解密Properties文件中的加密敏感信息。请注意,这个示例仅用于演示目的,实际应用中可能需要更多的安全措施,例如使用更强的加密算法、安全的密钥管理和存储等。

推荐阅读:
  1. python实现对服务器脚本敏感信息的加密解密功能
  2. 利用Java如何实现对敏感信息进行加密处理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:如何优化Java Properties文件读取速度

下一篇:网站内容更新频率

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》