SM4加密解密
maven項目引入以下依賴
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.59</version>
</dependency>
package com;
import java.security.Key;
import java.security.Security;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
public class Sm4Util {
static {
Security.addProvider(new BouncyCastleProvider());
}
private static final String ENCODING = "UTF-8";
public static final String ALGORITHM_NAME = "SM4";
// PKCS5Padding NoPadding 補位規則,PKCS5Padding缺位補0,NoPadding不補
public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
/**
* ECB加密模式,無向量
* @explain ECB
* @param algorithmName
* @param mode
* @param key
* @return
* @throws Exception
*/
private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
cipher.init(mode, sm4Key);
return cipher;
}
/**
* sm4加密
* @explain 加密模式:ECB 密文長度不固定,會随着被加密字元串長度的變化而變化
* @param hexKey 16進制密鑰(忽略大小寫)
* @param paramStr 待加密字元串
* @return 傳回16進制的加密字元串
* @throws Exception
*/
public static String encryptEcb(String hexKey, String paramStr) throws Exception {
String cipherText = "";
// 16進制字元串-->byte[]
byte[] keyData = ByteUtils.fromHexString(hexKey);
// String-->byte[]
//當加密資料為16進制字元串時使用這行
// byte[] srcData = ByteUtils.fromHexString(hexKey);
byte[] srcData = paramStr.getBytes(ENCODING);
// 加密後的數組
byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
// byte[]-->hexString
cipherText = ByteUtils.toHexString(cipherArray);
return cipherText;
}
/**
* 加密模式之Ecb
* @param key
* @param data
* @return
* @throws Exception
*/
public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception {
Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);//聲稱Ecb暗号,通過第二個參數判斷加密還是解密
return cipher.doFinal(data);
}
//解密****************************************
/**
* sm4解密
* @explain 解密模式:采用ECB
* @param hexKey 16進制密鑰
* @param cipherText 16進制的加密字元串(忽略大小寫)
* @return 解密後的字元串
* @throws Exception
*/
public static String decryptEcb(String hexKey, String cipherText) throws Exception {
// 用于接收解密後的字元串
String decryptStr = "";
// hexString-->byte[]
byte[] keyData = ByteUtils.fromHexString(hexKey);
// hexString-->byte[]
byte[] cipherData = ByteUtils.fromHexString(cipherText);
// 解密
byte[] srcData = decrypt_Ecb_Padding(keyData, cipherData);
// byte[]-->String
decryptStr = new String(srcData, ENCODING);
return decryptStr;
}
/**
* 解密
* @explain
* @param key
* @param cipherText
* @return
* @throws Exception
*/
public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {
Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);//生成Ecb暗号,通過第二個參數判斷加密還是解密
return cipher.doFinal(cipherText);
}
public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception {
// 用于接收校驗結果
boolean flag = false;
// hexString-->byte[]
byte[] keyData = ByteUtils.fromHexString(hexKey);
// 将16進制字元串轉換成數組
byte[] cipherData = ByteUtils.fromHexString(cipherText);
// 解密
byte[] decryptData = decrypt_Ecb_Padding(keyData, cipherData);
// 将原字元串轉換成byte[]
byte[] srcData = paramStr.getBytes(ENCODING);
// 判斷2個數組是否一緻
flag = Arrays.equals(decryptData, srcData);
return flag;
}
public static void main(String[] args) {
try {
System.out.println("開始****************************");
String json = "86C63180C2806ED1F47B859DE501215B";
System.out.println("加密前:"+json);
//自定義的32位16進制秘鑰
String key = "86C63180C2806ED1F47B859DE501215B";
String cipher = encryptEcb(key,json);//sm4加密
System.out.println("加密後:"+cipher);
System.out.println("校驗:"+verifyEcb(key,cipher,json));//校驗加密前後是否為同一資料
json =decryptEcb(key,cipher);//解密
System.out.println("解密後:"+json);
System.out.println("結束****************************");
} catch (Exception e) {
e.printStackTrace();
}
}
}
結果
開始****************************
加密前:86C63180C2806ED1F47B859DE501215B
加密後:0f196060b706500cc9b88d22cee33fd67d0aa493d2a80a72396771b5585a49f81642326904c036af50b50f92e86cb274
校驗:true
解密後:86C63180C2806ED1F47B859DE501215B
結束****************************
JAR包位址
連結:https://pan.baidu.com/s/1mU6WXEWlV_Kbz_OeEAqAPA
提取碼:1ktn