数据库账户加密

在我们日常的开发中,必须要连接数据库,但是数据库的账户和密码有么有加密呢?在这里我们使用DES对称加密算法

步骤

  1. 编写一个DESUtil,我从网上搞了一个过来,贴在这里
    import java.security.Key;
    import java.security.SecureRandom;
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    @SuppressWarnings("restriction")
    public class DESUtils {
    
        private static Key key;
        private static String KEY_STR = "myKey";
        private static String CHARSETNAME = "UTF-8";
        private static String ALGORITHM = "DES";
    
        static {
            try {
                KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
                secureRandom.setSeed(KEY_STR.getBytes());
                generator.init(secureRandom);
                key = generator.generateKey();
                generator = null;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        public static String encrypt(String str) {
            BASE64Encoder base64encoder = new BASE64Encoder();
            try {
                byte[] bytes = str.getBytes(CHARSETNAME);
                Cipher cipher = Cipher.getInstance(ALGORITHM);
                cipher.init(Cipher.ENCRYPT_MODE, key);
                byte[] doFinal = cipher.doFinal(bytes);
                return base64encoder.encode(doFinal);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        public static String decrypt(String str) {
            BASE64Decoder base64decoder = new BASE64Decoder();
            try {
                byte[] bytes = base64decoder.decodeBuffer(str);
                Cipher cipher = Cipher.getInstance(ALGORITHM);
                cipher.init(Cipher.DECRYPT_MODE, key);
                byte[] doFinal = cipher.doFinal(bytes);
                return new String(doFinal, CHARSETNAME);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
    }
    
  2. 使用上面的类加密自己的账户和密码,并把结果填写到配置文件中
  3. 编写配置类
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    
    public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
        
        private String[] encryptPropNames = { "jdbc.username", "jdbc.password" };
    
        @Override
        protected String convertProperty(String propertyName, String propertyValue) {
            if (isEncrypted(propertyName)) {
                String decryptValue = DESUtil.decrypt(propertyValue);
                return decryptValue;
            } else {
                return propertyValue;
            }
        }
    
        private boolean isEncrypted(String propertyName) {
            for (String encryptpropertyName : encryptPropNames) {
                if (encryptpropertyName.equals(propertyName)) {
                    return true;
                }
            }
            return false;
        }
        
    }
    
  4. 使用上面编写的类替换context:property-placeholder
    <bean class="org.sherekr.dfo.core.util.EncryptPropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
        <property name="fileEncoding" value="UTF-8" />
    </bean>
    
  5. 完成编写,启动测试
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • 1、不安全的随机数生成,在CSRF TOKEN生成、password reset token生成等,会造成toke...
    nightmare丿阅读 3,752评论 0 1
  • 概述 之前一直对加密相关的算法知之甚少,只知道类似DES、RSA等加密算法能对数据传输进行加密,且各种加密算法各有...
    Henryzhu阅读 3,055评论 0 14
  • AES加密算法:高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密...
    kuen_zhang阅读 1,448评论 1 4
  • 人世间,我们在努力的活着! 小时候会想着尽快长大,可以自由自在的选择想要的生活,可以脱离父母的约束,可以为所欲为。...
    初入区块链阅读 136评论 0 0