1 配置文件引入 加密包 package.json
"jsencrypt": "^3.0.0-rc.1",
2 加密公钥配置
import { JSEncrypt } from 'jsencrypt'
import request from "@/utils/request" ; const RSA_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJVol0cJoJKA36lRJoCOe7IZNhWm/BY+zOumtVQ/uhw4+oIJQqbJ+xuAYED5VjUWGsKjqJWKAS1QuBpQNij+nUXUV1v7AB9r0ihBTwvvvVJSLOfEau6eeKEqnAW6X4bZvaexFKTxlAb7IumnlBoo8uP5bCyRt93BuseuHynoYrmwIDAQAB" ;
export function encryptedData ( data ) { let encryptor = new JSEncrypt ( ) ; encryptor. setPublicKey ( RSA_PUBLIC_KEY ) ; return encryptor. encrypt ( data) ;
} ;
3 前端请求 加密传输敏感数据
import { encryptedData } from "@/utils/auth" ; handleSubmit ( e ) { const { $store } = this ; e. preventDefault ( ) ; this . form. validateFields ( async ( err, values ) => { if ( ! err) { await $store. dispatch ( { type : "login/sumbitLogin" , params : { userInfo : encryptedData ( values. userName) , password : encryptedData ( values. password) } } ) ; this . $router. push ( { path : "/home" } ) ; } } ) ; }
4 后端接口 解密敏感数据
final String loginName = RSAUtil . decrypt ( userInfo, RSAUtil . DEFAULT_PRIVATE_KEY ) ;
后端解密工具包
package c ; import org. apache. commons. codec. binary. Base64 ; import javax. crypto. Cipher ;
import java. security. * ;
import java. security. interfaces. RSAPrivateKey ;
import java. security. interfaces. RSAPublicKey ;
import java. security. spec. PKCS8EncodedKeySpec ;
import java. security. spec. X509EncodedKeySpec ;
import java. util. HashMap ;
import java. util. Map ;
public class RSAUtil { public static String DEFAULT_PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIlWiXRwmgkoDfqVEmgI57shk2Fab8Fj7M66a1VD+6HDj6gglCpsn7G4BgQPlWNRYawqOolYoBLVC4GlA2KP6dRdRXW/sAH2vSKEFPC++9UlIs58Rq7p54oSqcBbpfhtm9p7EUpPGUBvsi6aeUGijy4/lsLJG33cG6x64fKehiubAgMBAAECgYB1JrxZ9zNuXByQhFkPJYmGtz1g7iQmc3atYZftQ/CCLK9+W0wIZGBU2QVIsYJdFi6bDeqg/liNt+34ySn6CYkLrtEPhJ9WvTg81v1b2aMun91rL+HvnP3mx5xUqrzBZeY7YHaNAnIiX8XoYo1LgvpQlJqSeFZzMQIAKAwqsh7CAQJBAP8rO5O4ooGoyYLqj9d4GxVtj4/3Waf8ysNC7xc55jFJWFk2c/MU1f8ZXp3fduNSP7GMsDElIz3uAJGXBcbZwdMCQQCJyQ2r+LaEGq0MCSzHR+bjQI1CadvIhcQe4jf1EN6keaC6UVLyqT9sKbM6DAbArTxxtOb8TJyGfSmYDgbJkAoZAkEA3qd4ywOXOZBwlb3bWebBq0b4uGTbZMwemZd0TU4GYQdhAieTsY6C1J/sgdGWSfFhBqp+q8N+JvHgqSmgrs0gZQJAajhHxOrA8ciK3Tt5fMuucEUEYU2wdL7CubhVwSRXL17gjKJDGvVlAmKHW9mHnyQt886tP5VkGb4FwcYWb28OiQJAEPAasVoq+Cls2VFiFY3kZ8eEOYHxdamIIyNH9hFv07eZEyq63ZTXcbFhOwMeE5W2JowIbTVwmMkWT7pPSM9sXw==" ; public static String DEFALUT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJVol0cJoJKA36lRJoCOe7IZNhWm/BY+zOumtVQ/uhw4+oIJQqbJ+xuAYED5VjUWGsKjqJWKAS1QuBpQNij+nUXUV1v7AB9r0ihBTwvvvVJSLOfEau6eeKEqnAW6X4bZvaexFKTxlAb7IumnlBoo8uP5bCyRt93BuseuHynoYrmwIDAQAB" ; public static Map < Integer , String > genKeyPair ( ) throws NoSuchAlgorithmException { KeyPairGenerator keyPairGen = KeyPairGenerator . getInstance ( "RSA" ) ; keyPairGen. initialize ( 1024 , new SecureRandom ( ) ) ; KeyPair keyPair = keyPairGen. generateKeyPair ( ) ; RSAPrivateKey privateKey = ( RSAPrivateKey ) keyPair. getPrivate ( ) ; RSAPublicKey publicKey = ( RSAPublicKey ) keyPair. getPublic ( ) ; String publicKeyString = new String ( Base64 . encodeBase64 ( publicKey. getEncoded ( ) ) ) ; String privateKeyString = new String ( Base64 . encodeBase64 ( ( privateKey. getEncoded ( ) ) ) ) ; Map < Integer , String > keyMap = new HashMap < > ( ) ; keyMap. put ( 0 , publicKeyString) ; keyMap. put ( 1 , privateKeyString) ; return keyMap; } public static String encrypt ( String str, String publicKey ) throws Exception { byte [ ] decoded = Base64 . decodeBase64 ( publicKey) ; RSAPublicKey pubKey = ( RSAPublicKey ) KeyFactory . getInstance ( "RSA" ) . generatePublic ( new X509EncodedKeySpec ( decoded) ) ; Cipher cipher = Cipher . getInstance ( "RSA" ) ; cipher. init ( Cipher . ENCRYPT_MODE , pubKey) ; return Base64 . encodeBase64String ( cipher. doFinal ( str. getBytes ( "UTF-8" ) ) ) ; } public static String decrypt ( String str, String privateKey) throws Exception { byte [ ] inputByte = Base64 . decodeBase64 ( str. getBytes ( "UTF-8" ) ) ; byte [ ] decoded = Base64 . decodeBase64 ( privateKey) ; RSAPrivateKey priKey = ( RSAPrivateKey ) KeyFactory . getInstance ( "RSA" ) . generatePrivate ( new PKCS8EncodedKeySpec ( decoded) ) ; Cipher cipher = Cipher . getInstance ( "RSA" ) ; cipher. init ( Cipher . DECRYPT_MODE , priKey) ; return new String ( cipher. doFinal ( inputByte) ) ; }
}