账号密码数据库加密说明

简介:

一、加密算法选择

密码学中两种常见的密码算法为:对称密码算法和非对称密码算法。

对称密钥加密,又称私钥加密,即信息的发送方和接收方用一个密钥去加密和解密数据。它的最大优势是加/解密速度快,适合于对大数据量进行加密,但密钥管理困难。

非对称密钥加密,又称公钥密钥加密。它需要使用一对密钥来分别完成加密和解密操作,一个公开发布,即公开密钥,另一个由用户自己秘密保存,即私用密钥。信息发送者用公开密钥去加密,而信息接收者则用私用密钥去解密。公钥机制灵活,但加密和解密速度却比对称密钥加密慢得多。

对称加密算法用来对敏感数据等信息进行加密,常用的算法包括:
DES(Data Encryption Standard):数据加密标准,速度较快,适用于加密大量数据的场合。
3DES(Triple DES):是基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高。
AES(Advanced Encryption Standard):高级加密标准,是下一代的加密算法标准,速度快,安全级别高;

Mysql官网手册

https://dev.mysql.com/doc/refman/5.7/en/encryption-functions.html#function_aes-encrypt

MySQL的内置加密函数里已经不推荐DES算法,所以选用AES算法来加解密账号密码。

二、加密策略

AES算法加解密都需要同一个key,如果这个key用来加密所有账号的密码,那么密码相同的不同账号得到的加密结果是相同的,这样会降低安全性,所以用可以唯一标识账号的aid来当做key,为了更不容易反向推导出key,实际应用中会用一个内部的salt字符串拼上账号aid来作为key。

 

三、实现

为了灵活和重用,没使用MySQL内置函数,改用Java实现。

AES算法实现与操作系统有关,本实现兼容Windows/Linux。

加密后获得的是byte[]类型结果,因此数据库内相应字段的类型应为BLOB,节省空间用TinyBlob类型就够。

加密算法需要使用外部类库javabase64-1.3.1.jar百度网盘下载链接

Java实现有两个类:

AESUtils:提供加解密静态方法

Base64Utils :辅助AESUtils类

 

AESUtils常用方法说明:

方法

返回

说明

参数

getSecretKey(String seed)

String

生成key

seed: salt+aid

encrypt(byte[] data, String key)

byte[]

加密

data: 密码明文getBytes(“UTF-8”)

key: getScretKey方法结果

decrypt(byte[] data, String key)

byte[]

解密

data: 密码密文

key: getScretKey方法结果

 

 

 AESUtils.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.InputStream;
import  java.io.OutputStream;
import  java.security.Key;
import  java.security.SecureRandom;
 
import  javax.crypto.Cipher;
import  javax.crypto.CipherInputStream;
import  javax.crypto.CipherOutputStream;
import  javax.crypto.KeyGenerator;
import  javax.crypto.SecretKey;
import  javax.crypto.spec.SecretKeySpec;
 
public  class  AESUtils {
 
     private  static  final  String ALGORITHM =  "AES" ;
     private  static  final  int  KEY_SIZE =  128 ;
     private  static  final  int  CACHE_SIZE =  1024 ;
 
     public  static  String getSecretKey()  throws  Exception {
         return  getSecretKey( null );
     }
 
     public  static  String getSecretKey(String seed)  throws  Exception {
         try  {
             KeyGenerator _generator = KeyGenerator.getInstance(ALGORITHM);
             SecureRandom secureRandom = SecureRandom.getInstance( "SHA1PRNG" );
             secureRandom.setSeed(seed.getBytes( "UTF-8" ));
             _generator.init(KEY_SIZE, secureRandom);
             SecretKey secretKey = _generator.generateKey();
             return  Base64Utils.encode(secretKey.getEncoded());
         }  catch  (Exception e) {
             throw  new  RuntimeException( "初始化密钥出现异常" );
         }
 
     }
 
     public  static  byte [] encrypt( byte [] data, String key)  throws  Exception {
         Key k = toKey(Base64Utils.decode(key));
         byte [] raw = k.getEncoded();
         SecretKeySpec secretKeySpec =  new  SecretKeySpec(raw, ALGORITHM);
         Cipher cipher = Cipher.getInstance(ALGORITHM);
         cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
         return  cipher.doFinal(data);
     }
 
     public  static  void  encryptFile(String key, String sourceFilePath, String destFilePath)  throws  Exception {
         File sourceFile =  new  File(sourceFilePath);
         File destFile =  new  File(destFilePath);
         if  (sourceFile.exists() && sourceFile.isFile()) {
             if  (!destFile.getParentFile().exists()) {
                 destFile.getParentFile().mkdirs();
             }
             destFile.createNewFile();
             InputStream in =  new  FileInputStream(sourceFile);
             OutputStream out =  new  FileOutputStream(destFile);
             Key k = toKey(Base64Utils.decode(key));
             byte [] raw = k.getEncoded();
             SecretKeySpec secretKeySpec =  new  SecretKeySpec(raw, ALGORITHM);
             Cipher cipher = Cipher.getInstance(ALGORITHM);
             cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
             CipherInputStream cin =  new  CipherInputStream(in, cipher);
             byte [] cache =  new  byte [CACHE_SIZE];
             int  nRead =  0 ;
             while  ((nRead = cin.read(cache)) != - 1 ) {
                 out.write(cache,  0 , nRead);
                 out.flush();
             }
             out.close();
             cin.close();
             in.close();
         }
     }
 
     public  static  byte [] decrypt( byte [] data, String key)  throws  Exception {
         Key k = toKey(Base64Utils.decode(key));
         byte [] raw = k.getEncoded();
         SecretKeySpec secretKeySpec =  new  SecretKeySpec(raw, ALGORITHM);
         Cipher cipher = Cipher.getInstance(ALGORITHM);
         cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
         return  cipher.doFinal(data);
     }
 
     public  static  void  decryptFile(String key, String sourceFilePath, String destFilePath)  throws  Exception {
         File sourceFile =  new  File(sourceFilePath);
         File destFile =  new  File(destFilePath);
         if  (sourceFile.exists() && sourceFile.isFile()) {
             if  (!destFile.getParentFile().exists()) {
                 destFile.getParentFile().mkdirs();
             }
             destFile.createNewFile();
             FileInputStream in =  new  FileInputStream(sourceFile);
             FileOutputStream out =  new  FileOutputStream(destFile);
             Key k = toKey(Base64Utils.decode(key));
             byte [] raw = k.getEncoded();
             SecretKeySpec secretKeySpec =  new  SecretKeySpec(raw, ALGORITHM);
             Cipher cipher = Cipher.getInstance(ALGORITHM);
             cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
             CipherOutputStream cout =  new  CipherOutputStream(out, cipher);
             byte [] cache =  new  byte [CACHE_SIZE];
             int  nRead =  0 ;
             while  ((nRead = in.read(cache)) != - 1 ) {
                 cout.write(cache,  0 , nRead);
                 cout.flush();
             }
             cout.close();
             out.close();
             in.close();
         }
     }
 
     private  static  Key toKey( byte [] key)  throws  Exception {
         SecretKey secretKey =  new  SecretKeySpec(key, ALGORITHM);
         return  secretKey;
     }
}


Base64Utils.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import  it.sauronsoftware.base64.Base64;
 
import  java.io.ByteArrayInputStream;
import  java.io.ByteArrayOutputStream;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.InputStream;
import  java.io.OutputStream;
 
public  class  Base64Utils {
 
     private  static  final  int  CACHE_SIZE =  1024 ;
 
     public  static  byte [] decode(String base64)  throws  Exception {
         return  Base64.decode(base64.getBytes( "UTF-8" ));
     }
 
     public  static  String encode( byte [] bytes)  throws  Exception {
         return  new  String(Base64.encode(bytes));
     }
 
     public  static  String encodeFile(String filePath)  throws  Exception {
         byte [] bytes = fileToByte(filePath);
         return  encode(bytes);
     }
 
     public  static  void  decodeToFile(String filePath, String base64)  throws  Exception {
         byte [] bytes = decode(base64);
         byteArrayToFile(bytes, filePath);
     }
 
     public  static  byte [] fileToByte(String filePath)  throws  Exception {
         byte [] data =  new  byte [ 0 ];
         File file =  new  File(filePath);
         if  (file.exists()) {
             FileInputStream in =  new  FileInputStream(file);
             ByteArrayOutputStream out =  new  ByteArrayOutputStream( 2048 );
             byte [] cache =  new  byte [CACHE_SIZE];
             int  nRead =  0 ;
             while  ((nRead = in.read(cache)) != - 1 ) {
                 out.write(cache,  0 , nRead);
                 out.flush();
             }
             out.close();
             in.close();
             data = out.toByteArray();
         }
         return  data;
     }
 
     public  static  void  byteArrayToFile( byte [] bytes, String filePath)  throws  Exception {
         InputStream in =  new  ByteArrayInputStream(bytes);
         File destFile =  new  File(filePath);
         if  (!destFile.getParentFile().exists()) {
             destFile.getParentFile().mkdirs();
         }
         destFile.createNewFile();
         OutputStream out =  new  FileOutputStream(destFile);
         byte [] cache =  new  byte [CACHE_SIZE];
         int  nRead =  0 ;
         while  ((nRead = in.read(cache)) != - 1 ) {
             out.write(cache,  0 , nRead);
             out.flush();
         }
         out.close();
         in.close();
     }
 
}


本文转自   zl1030   51CTO博客,原文链接:http://blog.51cto.com/zl1030/1656283
相关文章
|
SQL 数据库 数据安全/隐私保护
数据库数据恢复——sql server数据库被加密的数据恢复案例
SQL server数据库数据故障: SQL server数据库被加密,无法使用。 数据库MDF、LDF、log日志文件名字被篡改。 数据库备份被加密,文件名字被篡改。
|
安全 Java 数据库
Jasypt加密数据库配置信息
本文介绍了使用 Jasypt 对配置文件中的公网数据库认证信息进行加密的方法,以提升系统安全性。主要内容包括:1. 背景介绍;2. 前期准备,如依赖导入及版本选择;3. 生成密钥并实现加解密测试;4. 在配置文件中应用加密后的密码,并通过测试接口验证解密结果。确保密码安全的同时,保障系统的正常运行。
1072 3
Jasypt加密数据库配置信息
|
存储 弹性计算 安全
现有数据库系统中应用加密技术的不同之处
本文介绍了数据库加密技术的种类及其在不同应用场景下的安全防护能力,包括云盘加密、透明数据加密(TDE)和选择列加密。分析了数据库面临的安全威胁,如管理员攻击、网络监听、绕过数据库访问等,并通过能力矩阵对比了各类加密技术的安全防护范围、加密粒度、业务影响及性能损耗。帮助用户根据安全需求、业务改造成本和性能要求,选择合适的加密方案,保障数据存储与传输安全。
|
存储 缓存 Java
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
2974 3
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
|
Linux 数据库 数据安全/隐私保护
GBase 数据库 加密客户端---数据库用户口令非明文存放需求的实现
GBase 数据库 加密客户端---数据库用户口令非明文存放需求的实现
|
安全 算法 Java
数据库信息/密码加盐加密 —— Java代码手写+集成两种方式,手把手教学!保证能用!
本文提供了在数据库中对密码等敏感信息进行加盐加密的详细教程,包括手写MD5加密算法和使用Spring Security的BCryptPasswordEncoder进行加密,并强调了使用BCryptPasswordEncoder时需要注意的Spring Security配置问题。
1766 0
数据库信息/密码加盐加密 —— Java代码手写+集成两种方式,手把手教学!保证能用!
|
SQL 安全 网络安全
"守护数据王国,揭秘SQL权限管理与安全配置秘籍!从创建用户到加密技术,全方位打造铜墙铁壁,让你的数据库安全无忧,远离黑客侵扰!"
【8月更文挑战第31天】数据库是信息系统的核心,存储了大量敏感数据,因此确保其安全至关重要。本文详细介绍了SQL权限管理与安全配置的方法,包括理解权限类型、创建用户和角色、分配权限、实施密码策略、使用加密技术、配置防火墙、定期审计备份及防止SQL注入等,帮助你全面保护数据库安全。通过这些步骤,你可以有效管理和配置数据库权限,防范潜在威胁,确保数据隐私和完整性。
604 1
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
757 158
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1975 152