📜  Java中的数据库加密

📅  最后修改于: 2022-05-13 01:54:26.798000             🧑  作者: Mango

Java中的数据库加密

基本上,加密是在代码中或更具体地以密文形式对用户数据进行处理或转换,以防止未经授权的访问,因此,加密在当今世界非常重要,我们都在处理存储在数据库中的大型数据集,并且必须保护数据库的凭据,以保护隐私和未经授权的访问。

为了加密我们的数据库凭证,我们将使用Jaspyt api。我们可以从这里下载 jaspyt 库。

Java简化加密

Jasypt 是一个Java库,它允许开发人员以最小的努力为项目添加基本的加密功能,而无需在项目中随处添加一些内容,而无需编写任何代码。 Jasypt 是高度可配置的。

为了加密数据库凭据,我们将执行这些任务-

  1. 创建一个 POJO 类。
  2. 创建一个属性文件。
  3. 创建一个Java类。

第 1 步:创建 POJO 类

因此,我们创建了一个名为 Details 的普通Java类。Java 具有实际的用户名和实际密码以及具有特殊和非特殊字符的用户名和密码的密钥。代码如下——

Java
// Creating a POJO class
  
package com.jdbc;
  
public class details {
  
    // Private fields
    private String key = "@2334dgdfg@#$%dsgdf";
    private String user = "root";
    private String key2 = "@1567sedf#2@";
    private String pass = "root";
  
    // Getter methods for private fields
    public String getKey() { return key; }
    public String getUser() { return user; }
    public String getKey2() { return key2; }
    public String getPass() { return pass; }
}


Java
// Creating a java class
  
package com.jdbc;
  
import java.sql.Connection;
import java.io.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.security.MessageDigest;
import javax.crypto.Cipher;
  
import org.jasypt.util.text.BasicTextEncryptor;
  
public class TestJdbc2 {
    public static void main(String[] args)
        throws ClassNotFoundException, SQLException,
               IOException
    {
  
        // Fethches the system property
        String path = System.getProperty("user.dir");
        System.out.println("Working Directory = " + path);
  
        // Creating a FileReader and specified the
        // name of the file to read from
        FileReader reader = new FileReader(
            path + "/src/config.properties");
  
        Properties p = new Properties();
  
        // Reads a property list from the input byte stream
        p.load(reader);
        details dt = new details();
        BasicTextEncryptor bte = new BasicTextEncryptor();
  
        // Getting key from details class object and
        // set the password for encryption and decryption
        bte.setPassword(dt.getKey());
  
        // Encrypt the message
        String encryptedid = bte.encrypt(dt.getUser());
  
        // Set the system property
        p.setProperty("username", encryptedid);
        BasicTextEncryptor bte1 = new BasicTextEncryptor();
  
        // Setting a password
        bte1.setPassword(dt.getKey2());
  
        // Encrypt the password
        String encryptedps = bte1.encrypt(dt.getPass());
        p.setProperty("password", encryptedps);
  
        // Writes the property list in the properties table
        // to the output character stream in a format
        // suitable for using load method
        p.store(
            new FileWriter(path + "/src/config.properties"),
            " Properties Data");
  
        // Load the driver class into the memory at the
        // runtime
        Class.forName("com.mysql.cj.jdbc.Driver");
  
        // Establishes the connection and decrypt the
        // encryptedid and encryptedps
        Connection conn = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/test",
            bte.decrypt(encryptedid),
            bte1.decrypt(encryptedps));
        System.out.println("Connection successful!!!");
        System.out.println("Done");
    }
}


第 2 步:创建一个空的属性文件

Java 中的数据库加密 - 创建一个空的属性文件

第 3 步- 创建一个名为 TestJDBC2 的MainConnecton类。 Java具有加密和解密过程所需的所有代码行。我们使用了 javax.crypto.Cipher 类、 Java.security.MessageDigest 抽象类、org.jasypt.util.text.BasicTextEncryptor FinalClass ,它们将执行加密和解密过程。

所以。首先,我们将使用详细信息中定义的密钥。用于用户名和密码的加密和解密过程的Java文件,将调用BasicTextEncryptor类的加密和解密方法。

现在让我们看看代码 -

Java

// Creating a java class
  
package com.jdbc;
  
import java.sql.Connection;
import java.io.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.security.MessageDigest;
import javax.crypto.Cipher;
  
import org.jasypt.util.text.BasicTextEncryptor;
  
public class TestJdbc2 {
    public static void main(String[] args)
        throws ClassNotFoundException, SQLException,
               IOException
    {
  
        // Fethches the system property
        String path = System.getProperty("user.dir");
        System.out.println("Working Directory = " + path);
  
        // Creating a FileReader and specified the
        // name of the file to read from
        FileReader reader = new FileReader(
            path + "/src/config.properties");
  
        Properties p = new Properties();
  
        // Reads a property list from the input byte stream
        p.load(reader);
        details dt = new details();
        BasicTextEncryptor bte = new BasicTextEncryptor();
  
        // Getting key from details class object and
        // set the password for encryption and decryption
        bte.setPassword(dt.getKey());
  
        // Encrypt the message
        String encryptedid = bte.encrypt(dt.getUser());
  
        // Set the system property
        p.setProperty("username", encryptedid);
        BasicTextEncryptor bte1 = new BasicTextEncryptor();
  
        // Setting a password
        bte1.setPassword(dt.getKey2());
  
        // Encrypt the password
        String encryptedps = bte1.encrypt(dt.getPass());
        p.setProperty("password", encryptedps);
  
        // Writes the property list in the properties table
        // to the output character stream in a format
        // suitable for using load method
        p.store(
            new FileWriter(path + "/src/config.properties"),
            " Properties Data");
  
        // Load the driver class into the memory at the
        // runtime
        Class.forName("com.mysql.cj.jdbc.Driver");
  
        // Establishes the connection and decrypt the
        // encryptedid and encryptedps
        Connection conn = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/test",
            bte.decrypt(encryptedid),
            bte1.decrypt(encryptedps));
        System.out.println("Connection successful!!!");
        System.out.println("Done");
    }
}

在这段代码中可以清楚地看到加密和解密的过程。这段代码执行后,可以在 Config.properties 文件中看到加密后的用户名和密码。

Java中的数据库加密——看过加解密的过程

正如在控制台进程中可以清楚地看到 Salt Algorithm 处理。

现在,让我们看一下 Config.properties 文件——

Java中的数据库加密 - 可以在配置属性文件中看到加密的用户名和密码

由于可以在 Config 中清楚地看到加密的凭据。属性文件和原始数据库凭据在详细信息中。 Java但连接属性正在以加密形式从属性文件中获取详细信息并对其进行解密以与数据库服务器进行通信。

所以现在数据库加密对我们所有的Java人来说都是一件容易的事。