开发人员联系方式:251746034@qq.com
代码库:https://github.com/chenjia/vue-desktop
代码库:https://github.com/chenjia/vue-app
代码库:https://github.com/chenjia/lxt
示例:http://47.100.119.102/vue-desktop
示例:http://47.100.119.102/vue-app
目的:前后端传输报文进行加密处理。
一、开发环境
前端技术:vue + axios
后端技术:java
加密算法:AES
为什么选择采用AES加密算法?作者在各种加密算法都进行过尝试,发现AES有以下特点比较符合要求:
1、加密解密执行速度快,相对DES更安全(原来采用的DES,结果部门的安全扫描建议用AES)
2、对称加密
3、被加密的明文长度可以很大,最多测试过10万长度的字符串。
java端AES加密示例,参考 lxt/lxt-common/com/lxt/ms/common/utils/SecurityUtils.java
public class SecurityUtils {
public final static String letters = "abcdefghijklmnopqrstuvwxyz0123456789";
public final static String key = "ed26d4cd99aa11e5b8a4c89cdc776729";
private static String Algorithm = "AES";
private static String AlgorithmProvider = "AES/ECB/PKCS5Padding";
private final static String encoding = "UTF-8";
public static String encrypt(String src) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
SecretKey secretKey = new SecretKeySpec(key.getBytes("utf-8"), Algorithm);
//IvParameterSpec ivParameterSpec = getIv();
Cipher cipher = Cipher.getInstance(AlgorithmProvider);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherBytes = cipher.doFinal(src.getBytes(Charset.forName("utf-8")));
return Base64Utils.encodeToString(cipherBytes);
}
public static String decrypt(String src) throws Exception {
SecretKey secretKey = new SecretKeySpec(key.getBytes("utf-8"), Algorithm);
//IvParameterSpec ivParameterSpec = getIv();
Cipher cipher = Cipher.getInstance(AlgorithmProvider);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] hexBytes = Base64Utils.decodeFromString(src);
byte[] plainBytes = cipher.doFinal(hexBytes);
return new String(plainBytes, "utf-8");
}
public static String md5Encrypt(String str) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte[] byteDigest = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < byteDigest.length; offset++) {
i = byteDigest[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
//32位加密
return buf.toString();
// 16位的加密
//return buf.toString().substring(8, 24);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
public static String encryptKey(String key) throws Exception {
String encryptedKey = "";
String[] array = key.split("");
Random random = new Random();
for (int i = 0; i < array.length; i++) {
encryptedKey += array[i];
for (int j = 0; j < i % 2 + 1; j++) {
int index = random.nextInt(letters.length());
encryptedKey += letters.substring(index, index + 1);
}
}
return Base64Utils.encodeToString(new StringBuilder(encryptedKey).reverse().toString().getBytes(encoding)).replaceAll("\n", "");
}
public static String decryptKey(String encryptedKey) {
encryptedKey = new String(Base64Utils.decodeFromString(encryptedKey));
String key = "";
char[] c = new StringBuilder(encryptedKey).reverse().toString().toCharArray(