AES加解密(java android vue互通)
本文用的加密模式为CBC,补码方式为PKCS5Padding(Vue对应的是PKCS7);
注意:不要导错包了,安卓端的base64导入的包是import android.util.Base64;
Java服务器端导入的base64包是import org.apache.commons.codec.binary.Base64;
Android端:
import android.util.Base64;
import android.util.Log;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class UUIDUtils {
// 编码方式
private final static String ENCODE = "UTF-8";
// 秘钥,长度为16,和服务器端保持一致
private final static String defaultKey = "password12345678";
//AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式(要和服务端保持一致)
private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
//填充值,16位的字符串,和服务器端保持一致
private static final String zeroIv = "1234567812345678";
// 前缀,可要可不要。
private final static String defaultPrefix = "";
/**
* AES加密后再使用BASE64加密
* 增加前缀
* 1.辨识正常数据,使其不进行解密
* 2.提高安全度
*
* @param content
* @return
* @throws Exception
*/
public static String encrypt(String content) {
String value = "";
try {
if (!isEmpty(content)) {
value = defaultPrefix + base64Encode(aesEncryptToBytes(content));
Log.e("bm", "加密后字符串" + value);
}
} catch (Exception e) {
System.out.println("EncryptAndDecrypt(加密错误)");
e.printStackTrace();
}
return value;
}
/**
* 使用BASE64解密后再用AES解密
* 增加前缀
* 1.辨识正常数据,使其不进行解密
* 2.提高安全度
*
* @param encryptStr
* @return
* @throws Exception
*/
public static String decrypt(String encryptStr) {
String value = "";
try {
int length = defaultPrefix.length();
if (encryptStr.length() > length) {
String val = encryptStr.substring(0, length);
if (val.equals(defaultPrefix)) {
value = aesDecryptByBytes(base64Decode(encryptStr.substring(length)));
} else {
value = encryptStr;
}
} else {
value = encryptStr;
}
} catch (Exception e) {
System.out.println("EncryptAndDecrypt(解密错误)");
e.printStackTrace();
}
Log.e("bm", "解密后字符串" + value);
return value;
}
/**
* AES加密
*
* @param content
* @return
* @throws Exception
*/
public static byte[] aesEncryptToBytes(String content) throws Exception {
SecretKeySpec sks = new SecretKeySpec(defaultKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, sks, new IvParameterSpec(zeroIv.getBytes()));
return cipher.doFinal(content.getBytes(ENCODE));
}
/**
* AES解密
*
* @param encryptBytes
* @return
* @throws Exception
*/
public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {
SecretKeySpec sks = new SecretKeySpec(defaultKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
cipher.init(Cipher.DECRYPT_MODE, sks, new IvParameterSpec(zeroIv.getBytes()));
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
/**
* BASE64 加密
*
* @return
* @throws Exception
*/
public static String base64Encode(byte[] bytes) {
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
/**
* BASE64 解密
*
* @return
* @throws Exception
*/
public static byte[] base64Decode(String base64Code) throws Exception {
return isEmpty(base64Code) ? null : Base64.decode(base64Code, Base64.DEFAULT);
}
public static boolean isEmpty(String str) {
return null == str || "".equals(str.trim());
}
}
java服务端:
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AESUtils {
// 编码方式
private final static String ENCODE = "UTF-8";
// 秘钥,长度为16,自己随意定义
private final static String defaultKey = "password12345678";
//AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
//填充值,16位的字符串,自己随意定义
private static final String zeroIv = "9769475569322011";
// 前缀,可要可不要。
private final static String defaultPrefix = "";
/**
* AES加密后再使用BASE64加密
* 增加前缀
* 1.辨识正常数据,使其不进行解密
* 2.提高安全度
*
* @param content
* @return
* @throws Exception
*/
public static String encrypt(String content) {
String value = "";
try {
if (!isEmpty(content)) {
value = defaultPrefix + base64Encode(aesEncryptToBytes(content));
System.out.println("AESUtils加密后字符串"+value);
}
} catch (Exception e) {
System.out.println("AESUtilsEncryptAndDecrypt(加密错误)");
e.printStackTrace();
}
return value;
}
/**
* 使用BASE64解密后再用AES解密
* 增加前缀
* 1.辨识正常数据,使其不进行解密
* 2.提高安全度
*
* @param encryptStr
* @return
* @throws Exception
*/
public static String decrypt(String encryptStr) {
String value = "";
try {
int length = defaultPrefix.length();
if (encryptStr.length() > length) {
String val = encryptStr.substring(0, length);
if (val.equals(defaultPrefix)) {
value = aesDecryptByBytes(base64Decode(encryptStr.substring(length)));
} else {
value = encryptStr;
}
} else {
value = encryptStr;
}
} catch (Exception e) {
System.out.println("AESUtilsEncryptAndDecrypt(解密错误)");
e.printStackTrace();
}
System.out.println("AESUtils解密后字符串"+value);
return value;
}
/**
* AES加密
*
* @param content
* @return
* @throws Exception
*/
public static byte[] aesEncryptToBytes(String content) throws Exception {
SecretKeySpec sks = new SecretKeySpec(defaultKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, sks, new IvParameterSpec(zeroIv.getBytes()));
return cipher.doFinal(content.getBytes(ENCODE));
}
/**
* AES解密
*
* @param encryptBytes
* @return
* @throws Exception
*/
public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {
SecretKeySpec sks = new SecretKeySpec(defaultKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
cipher.init(Cipher.DECRYPT_MODE, sks, new IvParameterSpec(zeroIv.getBytes()));
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
/**
* BASE64 加密
*
* @return
* @throws Exception
*/
public static String base64Encode(byte[] bytes) {
return Base64.encodeBase64String(bytes);
}
/**
* BASE64 解密
*
* @return
* @throws Exception
*/
public static byte[] base64Decode(String base64Code) throws Exception {
return isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code);
}
public static boolean isEmpty(String str) {
return null == str || "".equals(str.trim());
}
}
Vue端:
1.安装crypto-js :
npm install crypto-js --save-dev
2.代码:
import CryptoJS from "crypto-js";
import CryptoJS from '../utils/CryptoJS'
// 加密
encrypt(word, keyStr, ivStr) {
// 秘钥,长度为16,和服务器端保持一致
keyStr = keyStr ? keyStr : "password12345678";
//填充值,16位的字符串,和服务器端保持一致
ivStr = ivStr ? ivStr : "9769475569322011";
let key = CryptoJS.enc.Utf8.parse(keyStr);
let iv = CryptoJS.enc.Utf8.parse(ivStr);
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
},
// 解密
decrypt(word, keyStr, ivStr) {
// 秘钥,长度为16,和服务器端保持一致
keyStr = keyStr ? keyStr : "password12345678";
//填充值,16位的字符串,和服务器端保持一致
ivStr = ivStr ? ivStr : "9769475569322011";
var key = CryptoJS.enc.Utf8.parse(keyStr);
let iv = CryptoJS.enc.Utf8.parse(ivStr);
var decrypt = CryptoJS.AES.decrypt(word, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypt.toString(CryptoJS.enc.Utf8);
}
以上是 AES加解密(java android vue互通) 的全部内容, 来源链接: utcz.com/z/375348.html