最近突然想到前後端敏感資料加密,就先寫了個簡單加密demo。是基於註解和aop來實現加密解密。
定義兩個註解,encrypt對響應資料加密。decode對請求資料解密。
/**
* @description: 方法引數上加上此註解表示請求資料解密
* @author: wenwen
* @date: 2020/3/3 15:18
* @version: 1.0
*/@target
(elementtype.parameter}
)@retention
(retentionpolicy.runtime)
public @inte***ce
decode
/**
* @description: 方法上加上此註解表示響應資料加密
* @author: wenwen
* @date: 2020/3/3 17:34
* @version: 1.0
*/@target()
@retention
(retentionpolicy.runtime)
public @inte***ce
encrypt
這個加密方式不安全,因為金鑰前端使用者其實也有。
/**
* @description: 雙向加密工具類
* @author: wenwen
* @date: 2020/3/3 15:11
* @version: 1.0
*/public
class
aesutils
catch
(exception e)
}/**
* @description: 獲取加密後的字元
** @param str
* @return:
* @date: 2020/3/4 16:17
* @auther: wenwen
**/public
static string getencryptstring
(string str)
catch
(exception e)
}/**
* @param str
* @return
* @description: 對aes加密字串進行解密
*/public
static string getdecryptstring
(string str)
catch
(exception e)
}/**
* 對字串做編碼
** @param param
* @return
*/public
static string stringuncode
(string param)
)", "%25"); //測試%轉義
param = urlencoder.
encode
(param,
"utf-8");
//轉碼
// param = urldecoder.decode(param, "utf-8");//解碼
}catch
(unsupportedencodingexception e)
}return param;
}}
/**
* @description:
* @author: wenwen
* @date: 2020/3/3 15:34
* @version: 1.0
*/@aspect
@component
@slf4j
public
class
encryptaspect
for(annotation annotation : paramann)}}
// 執行目標方法
// object proceed = pjp.proceed(); // 如果呼叫此方法,引數還是傳進來未解密的資料去呼叫的方法
object proceed = pjp.
proceed
(params)
;// 將改變後的引數發起呼叫
// 獲取方法上的所有註解
annotation[
] annotations1 = signature.
getmethod()
.getannotations()
;for
(annotation annotation : annotations1)
}return proceed;
}}
啟動類加上@enableaspectjautoproxy。可以實現。
/**
* @description:
* @author: wenwen
* @date: 2020/3/3 15:29
* @version: 1.0
*/@restcontroller
public
class
testcontroller
}
使用RSA證書加密敏感資料
x.509證書標準支援三種不對稱加密演算法 rsa,dsa,diffie hellman algorithms。最常用的是rsa演算法。所以本文就以前面章節使用makecert工具生成的生成的mytestcert證書進行加密解密,這個證書具有rsa演算法1024位的金鑰對。figure 12.rsa...
暴露敏感資料
威脅載體 可以獲得你的敏感資料和任何資料備份訪問許可權的人。包括靜止資料,在傳輸過程中,甚至在你的自定義瀏覽器中,包括內部和外部欺騙。攻擊媒介 攻擊者通常不直接破壞加密。他們破壞其他的東西,例如偷取秘鑰,做中間人攻擊,或者從伺服器,在傳輸當中或者從使用者的瀏覽器中,偷取明文資料。安全弱點 最普通的漏...
對敏感備份資料加密
不管是資料庫備份還是檔案備份,其中都有可能會有敏感資料,所以對備份加密是對敏感資料保護的最後防線。使用gnupg加密 gpg c test.txt 提示要求輸入2次密碼,即生成加密後的檔案test.txt.gpg 加密後的檔案是無法直接讀取其中資料的 解密gpg d test.txt.gpg tes...