資料加密的各種姿勢
阿新 • • 發佈:2020-12-07
技術標籤:SpringBoot學習資料庫加密解密java安全
資料加密
資料按加密方式可分為對稱加密和非對稱加密和hash加密。
- 對稱加密:加解密金鑰相同,假如有一把鎖具,鎖具在關閉(加密時)和開啟(解密時)使用的是同一把鑰匙(使用相同的金鑰),則可以將該加密方式稱為對稱加密。常見的對稱加密方式如:DES、3DES、Blowfish、IDEA、RC4、RC5、RC6 和 AES
圖片源自網路
-
非對稱加密:金鑰分為公鑰和私鑰,如果後端返回的明文介面資料使用公鑰進行加密脫敏,前端在解密時則需要私鑰進行解密,使用公鑰則無法對已加密資料進行解密。反之,如果後端介面使用私鑰進行加密,前端在拿到介面資料密文後應先使用公鑰解密。常見的非對稱加密方式如:RSA、Diffie-Hellman、El Gamal、DSA
-
Hash加密
資料明文加密後,無法解密出原資料,所以不適合資料傳輸的場景,但是可以用於登入密碼驗證,一般場景下,我們在新增使用者時,會使用隨機數作為使用者的鹽值(salt)+使用者輸入的密碼拼接使用MD5演算法將資料生成唯一的hash碼,資料庫存取密碼的密文資料,上文知:hash加密無法反向解密,那麼我們如何做到的登入呢?跳出思維的牆,在登入時,使用者會輸入賬戶名和密碼,我們的登入邏輯中可以先使用使用者名稱去資料庫查詢庫中是否存在該使用者資訊(鹽值、密文密碼),如果不存在可按正常業務邏輯丟擲業務異常,正常情況下,我們獲取到使用者資訊,包括在新增使用者時隨機生成的鹽值資料,下面的邏輯就是使用使用者輸入的密碼+查到的資料庫中的鹽值進行hash加密,由於生成的金鑰是唯一的,所以,如果使用者輸入的密碼和註冊時輸入的一致(密碼正確的情況),那麼登入時生成的hashCode和註冊時生成的hashCode是一致的,如果兩個hash碼一致,登入成功。
常見的hash加密:MD5、HAVAL、SHA
AdminUserEntity userDb = adminUserDb.getOne(Wrappers.<AdminUserEntity>lambdaQuery().eq(AdminUserEntity::getUsername, userName) .eq(AdminUserEntity::getStatus, UserConstant.USER_STATUS.USER_ABLEDSTATE)); if (Objects.isNull(userDb)) { //throw new Exception;
對稱加密 AES demo
package com.module.boots.api.de.utils;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.tomcat.util.codec.binary.Base64;
import com.module.boots.exception.CommonRuntimeException;
/**
* AES加密解密
* @author:Ltx
* @date:2020年12月6日
*/
public class AesUtils {
private static final String KEY_ALGORITHM = "AES";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";// 預設的加密演算法
/**
* AES 加密操作
* @author Ltx
* @param content 待加密內容
* @param password 加密密碼
* @return String 返回Base64轉碼後的加密資料
*/
public static String encrypt(String content, String password) {
try {
// 建立密碼器
final Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
// 設定為UTF-8編碼
final byte[] byteContent = content.getBytes("utf-8");
// 初始化為加密模式的密碼器
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));
// 加密
final byte[] result = cipher.doFinal(byteContent);
// 通過Base64轉碼返回
return Base64.encodeBase64String(result);
}
catch (final Exception ex) {
throw new CommonRuntimeException(ex.fillInStackTrace());
}
}
/**
* AES 解密操作
* @author Ltx
* @param content
* @param password
* @return String
*/
public static String decrypt(String content, String password) {
try {
// 例項化
final Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
// 使用金鑰初始化,設定為解密模式
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));
// 執行操作
final byte[] result = cipher.doFinal(Base64.decodeBase64(content));
// 採用UTF-8編碼轉化為字串
return new String(result, "utf-8");
}
catch (final Exception ex) {
throw new CommonRuntimeException(ex.fillInStackTrace());
}
}
/**
* 生成加密祕鑰
* @author Ltx
* @param password 加密的密碼
* @return SecretKeySpec
*/
private static SecretKeySpec getSecretKey(final String password) {
// 返回生成指定演算法金鑰生成器的 KeyGenerator 物件
KeyGenerator kg = null;
try {
kg = KeyGenerator.getInstance(KEY_ALGORITHM);
// AES 要求金鑰長度為 128
kg.init(128, new SecureRandom(password.getBytes()));
// 生成一個金鑰
final SecretKey secretKey = kg.generateKey();
// 轉換為AES專用金鑰
return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
}
catch (final NoSuchAlgorithmException ex) {
throw new CommonRuntimeException(ex.fillInStackTrace());
}
}
public static void main(String[] args) {
final String str = "V9JofCHn02eyXRiDb1VuseRSuOgEQftROwudMPWwMAO2Wk5K7aYZ4Vtm6xiTn5i5";
System.out.println(decrypt(str, "xy934yrn9342u0ry4br8cn-9u2"));
}
}
尚未完成,盡情期待~~~