package org. jeecg. common. util. encryption ;
import org. apache. shiro. codec. Base64 ;
import javax. crypto. Cipher ;
import javax. crypto. spec. IvParameterSpec ;
import javax. crypto. spec. SecretKeySpec ;
public class AesEncryptUtil {
private static String KEY = EncryptedString . key;
private static String IV = EncryptedString . iv;
public static String encrypt ( String data, String key, String iv) throws Exception {
try {
Cipher cipher = Cipher . getInstance ( "AES/CBC/NoPadding" ) ;
int blockSize = cipher. getBlockSize ( ) ;
byte [ ] dataBytes = data. getBytes ( ) ;
int plaintextLength = dataBytes. length;
if ( plaintextLength % blockSize != 0 ) {
plaintextLength = plaintextLength + ( blockSize - ( plaintextLength % blockSize) ) ;
}
byte [ ] plaintext = new byte [ plaintextLength] ;
System . arraycopy ( dataBytes, 0 , plaintext, 0 , dataBytes. length) ;
SecretKeySpec keyspec = new SecretKeySpec ( key. getBytes ( ) , "AES" ) ;
IvParameterSpec ivspec = new IvParameterSpec ( iv. getBytes ( ) ) ;
cipher. init ( Cipher . ENCRYPT_MODE, keyspec, ivspec) ;
byte [ ] encrypted = cipher. doFinal ( plaintext) ;
return Base64 . encodeToString ( encrypted) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
return null ;
}
}
public static String desEncrypt ( String data, String key, String iv) throws Exception {
try {
byte [ ] encrypted1 = Base64 . decode ( data) ;
Cipher cipher = Cipher . getInstance ( "AES/CBC/NoPadding" ) ;
SecretKeySpec keyspec = new SecretKeySpec ( key. getBytes ( ) , "AES" ) ;
IvParameterSpec ivspec = new IvParameterSpec ( iv. getBytes ( ) ) ;
cipher. init ( Cipher . DECRYPT_MODE, keyspec, ivspec) ;
byte [ ] original = cipher. doFinal ( encrypted1) ;
String originalString = new String ( original) ;
return originalString;
} catch ( Exception e) {
e. printStackTrace ( ) ;
return null ;
}
}
public static String encrypt ( String data) throws Exception {
return encrypt ( data, KEY, IV) ;
}
public static String desEncrypt ( String data) throws Exception {
return desEncrypt ( data, KEY, IV) ;
}
public static void main ( String args[ ] ) throws Exception {
String test1 = "sa" ;
String test = new String ( test1. getBytes ( ) , "UTF-8" ) ;
String data = null ;
String key = KEY;
String iv = IV;
data = encrypt ( test, key, iv) ;
System . out. println ( "数据:" + test) ;
System . out. println ( "加密:" + data) ;
String jiemi = desEncrypt ( data, key, iv) . trim ( ) ;
System . out. println ( "解密:" + jiemi) ;
}
}
文件2
package org. jeecg. common. util. encryption ;
import lombok. Data ;
@Data
public class EncryptedString {
public static String key = "1234567890adbcde" ;
public static String iv = "1234567890hjlkew" ;
}
前端 aesEncrypt加密与解密
import { encryption } from '@/utils/encryption/aesEncrypt'
前端加密
toDetail ( scope ) {
let id = encryption ( scope. id, '1234567890adbcde' , '1234567890hjlkew' )
this . $router. push ( { path: '/shopManage/shopDetail' , query: { id: id} } )
} ,
后端解密
shopId = AesEncryptUtil . desEncrypt ( id) ;