Java之SM4加密解密的實現(xiàn)
Java SM4加密解密
依賴
<dependency> ? ? <groupId>org.bouncycastle</groupId> ? ? <artifactId>bcprov-jdk15on</artifactId> ? ? <version>1.59</version> </dependency>
SM4工具類
public class Sm4Util { ? ? static { ? ? ? ? Security.addProvider(new BouncyCastleProvider()); ? ? } ? ? private static final String ENCODING = "UTF-8"; ? ? public static final String ALGORITHM_NAME = "SM4"; ? ? // 加密算法/分組加密模式/分組填充方式 ? ? // PKCS5Padding-以8個字節(jié)為一組進(jìn)行分組加密 ? ? // 定義分組加密模式使用:PKCS5Padding ? ? public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding"; ? ? /** ? ? ?* sm4加密 ? ? ?* ? ? ?* @param hexKey ? 16進(jìn)制密鑰(忽略大小寫) ? ? ?* @param paramStr 待加密字符串 ? ? ?* @return 返回16進(jìn)制的加密字符串 ? ? ?* @throws Exception ? ? ?* @explain 加密模式:ECB 密文長度不固定,會隨著被加密字符串長度的變化而變化 ? ? ?*/ ? ? public static String encryptEcb(String hexKey, String paramStr) throws Exception { ? ? ? ? String cipherText = ""; ? ? ? ? // 16進(jìn)制字符串-->byte[] ? ? ? ? byte[] keyData = ByteUtils.fromHexString(hexKey); ? ? ? ? // String-->byte[] ? ? ? ? byte[] srcData = paramStr.getBytes(ENCODING); ? ? ? ? // 加密后的數(shù)組 ? ? ? ? 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暗號,通過第二個參數(shù)判斷加密還是解密 ? ? ? ? return cipher.doFinal(data); ? ? } ? ? /** ? ? ?* 生成ECB暗號 ? ? ?* ? ? ?* @param algorithmName 算法名稱 ? ? ?* @param mode ? ? ? ? ?模式 ? ? ?* @param key ? ? ?* @return ? ? ?* @throws Exception ? ? ?* @explain ECB模式(電子密碼本模式:Electronic codebook) ? ? ?*/ ? ? 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解密 ? ? ?* ? ? ?* @param hexKey ? ? 16進(jìn)制密鑰 ? ? ?* @param cipherText 16進(jìn)制的加密字符串(忽略大小寫) ? ? ?* @return 解密后的字符串 ? ? ?* @throws Exception ? ? ?* @explain 解密模式:采用ECB ? ? ?*/ ? ? 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; ? ? } ? ? /** ? ? ?* 解密 ? ? ?* ? ? ?* @param key ? ? ?* @param cipherText ? ? ?* @return ? ? ?* @throws Exception ? ? ?* @explain ? ? ?*/ ? ? public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception { ? ? ? ? Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);//生成Ecb暗號,通過第二個參數(shù)判斷加密還是解密 ? ? ? ? return cipher.doFinal(cipherText); ? ? } ? ? /** ? ? ?* 校驗加密前后的字符串是否為同一數(shù)據(jù) ? ? ?* ? ? ?* @param hexKey ? ? 16進(jìn)制密鑰(忽略大小寫) ? ? ?* @param cipherText 16進(jìn)制加密后的字符串 ? ? ?* @param paramStr ? 加密前的字符串 ? ? ?* @return 是否為同一數(shù)據(jù) ? ? ?* @throws Exception ? ? ?* @explain ? ? ?*/ ? ? public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception { ? ? ? ? // 用于接收校驗結(jié)果 ? ? ? ? boolean flag = false; ? ? ? ? // hexString-->byte[] ? ? ? ? byte[] keyData = ByteUtils.fromHexString(hexKey); ? ? ? ? // 將16進(jìn)制字符串轉(zhuǎn)換成數(shù)組 ? ? ? ? byte[] cipherData = ByteUtils.fromHexString(cipherText); ? ? ? ? // 解密 ? ? ? ? byte[] decryptData = decrypt_Ecb_Padding(keyData, cipherData); ? ? ? ? // 將原字符串轉(zhuǎn)換成byte[] ? ? ? ? byte[] srcData = paramStr.getBytes(ENCODING); ? ? ? ? // 判斷2個數(shù)組是否一致 ? ? ? ? flag = Arrays.equals(decryptData, srcData); ? ? ? ? return flag; ? ? } }
測試
public static void main(String[] args) { ? ? ? ? try { ? ? ? ? ? ? System.out.println("開始測試SM4加密解密===================="); ? ? ? ? ? ? String json = "{ keubgibzpcax: { dvwvgxsfcq: lvCAhMFcXoK9YB1B},dongubnfea: D9fpTrZOzByv}"; ? ? ? ? ? ? System.out.println("加密前:" + json); ? ? ? ? ? ? //自定義的32位16進(jìn)制秘鑰 ? ? ? ? ? ? String key = "E99567ACC5364D69BA0B0BC83066955F"; ? ? ? ? ? ? //sm4加密 ? ? ? ? ? ? String cipher = Sm4Util.encryptEcb(key, json); ? ? ? ? ? ? System.out.println("加密后:" + cipher); ? ? ? ? ? ? //校驗加密前后是否為同一數(shù)據(jù) ? ? ? ? ? ? System.out.println("校驗:" + Sm4Util.verifyEcb(key, cipher, json)); ? ? ? ? ? ? //解密 ? ? ? ? ? ? json = Sm4Util.decryptEcb(key, cipher); ? ? ? ? ? ? System.out.println("解密后:" + json); ? ? ? ? ? ? System.out.println("結(jié)束==================="); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? }
國密SM4對稱加密Java加解密
SM4.0(原名SMS4.0)是中華人民共和國政府采用的一種分組密碼標(biāo)準(zhǔn),由國家密碼管理局于2012年3月21日發(fā)布。
相關(guān)標(biāo)準(zhǔn)為“GM/T 0002-2012《SM4分組密碼算法》(原SMS4分組密碼算法)”。
SM4是什么?
使用步驟
1.引入庫
代碼如下(示例):
<!--國密--> ? ? ? ? ? ? <dependency> ? ? ? ? ? ? ? ? <groupId>org.bouncycastle</groupId> ? ? ? ? ? ? ? ? <artifactId>bcprov-jdk15on</artifactId> ? ? ? ? ? ? ? ? <version>1.56</version> ? ? ? ? ? ? </dependency>
package cn.china.sm4; /** ?* @Description: Description ?* @Package cn.china.sm4 ?* @Date 2023-01-10 ?* @Author admin ?* @Since 3.0 ?*/ import org.bouncycastle.jce.provider.BouncyCastleProvider; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.security.Security; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.spec.SecretKeySpec; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.pqc.math.linearalgebra.ByteUtils; import java.security.Security; /** ?* sm4加密算法工具類 ?* @explain sm4加密、解密與加密結(jié)果驗證 ?* ? ? ? ? ?可逆算法 ?* @author Marydon ?* @creationTime 2018年7月6日上午11:46:59 ?* @version 1.0 ?* @since ?* @email marydon20170307@163.com ?*/ public class SM4Util { ? ? static { ? ? ? ? Security.addProvider(new BouncyCastleProvider()); ? ? } ? ? private static final String ENCODING = "UTF-8"; ? ? public static final String ALGORITHM_NAME = "SM4"; ? ? // 加密算法/分組加密模式/分組填充方式 ? ? // PKCS5Padding-以8個字節(jié)為一組進(jìn)行分組加密 ? ? // 定義分組加密模式使用:PKCS5Padding ? ? public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding"; ? ? // 128-32位16進(jìn)制;256-64位16進(jìn)制 ? ? public static final int DEFAULT_KEY_SIZE = 128; ? ? /** ? ? ?* 生成ECB暗號 ? ? ?* @explain ECB模式(電子密碼本模式:Electronic codebook) ? ? ?* @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; ? ? } ? ? /** ? ? ?* 自動生成密鑰 ? ? ?* @explain ? ? ?* @return ? ? ?* @throws NoSuchAlgorithmException ? ? ?* @throws NoSuchProviderException ? ? ?*/ ? ? public static byte[] generateKey() throws Exception { ? ? ? ? return generateKey(DEFAULT_KEY_SIZE); ? ? } ? ? /** ? ? ?* @explain ? ? ?* @param keySize ? ? ?* @return ? ? ?* @throws Exception ? ? ?*/ ? ? public static byte[] generateKey(int keySize) throws Exception { ? ? ? ? KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME); ? ? ? ? kg.init(keySize, new SecureRandom()); ? ? ? ? return kg.generateKey().getEncoded(); ? ? } ? ? /** ? ? ?* sm4加密 ? ? ?* @explain 加密模式:ECB ? ? ?* ? ? ? ? ?密文長度不固定,會隨著被加密字符串長度的變化而變化 ? ? ?* @param hexKey ? ? ?* ? ? ? ? ? ?16進(jìn)制密鑰(忽略大小寫) ? ? ?* @param paramStr ? ? ?* ? ? ? ? ? ?待加密字符串 ? ? ?* @return 返回16進(jìn)制的加密字符串 ? ? ?* @throws Exception ? ? ?*/ ? ? public static String encryptEcb(String hexKey, String paramStr) throws Exception { ? ? ? ? String cipherText = ""; ? ? ? ? // 16進(jìn)制字符串-->byte[] ? ? ? ? byte[] keyData = ByteUtils.fromHexString(hexKey); ? ? ? ? // String-->byte[] ? ? ? ? byte[] srcData = paramStr.getBytes(ENCODING); ? ? ? ? // 加密后的數(shù)組 ? ? ? ? byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData); ? ? ? ? // byte[]-->hexString ? ? ? ? cipherText = ByteUtils.toHexString(cipherArray); ? ? ? ? return cipherText; ? ? } ? ? /** ? ? ?* 加密模式之Ecb ? ? ?* @explain ? ? ?* @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); ? ? ? ? return cipher.doFinal(data); ? ? } ? ? /** ? ? ?* sm4解密 ? ? ?* @explain 解密模式:采用ECB ? ? ?* @param hexKey ? ? ?* ? ? ? ? ? ?16進(jìn)制密鑰 ? ? ?* @param cipherText ? ? ?* ? ? ? ? ? ?16進(jìn)制的加密字符串(忽略大小寫) ? ? ?* @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); ? ? ? ? return cipher.doFinal(cipherText); ? ? } ? ? /** ? ? ?* 校驗加密前后的字符串是否為同一數(shù)據(jù) ? ? ?* @explain ? ? ?* @param hexKey ? ? ?* ? ? ? ? ? ?16進(jìn)制密鑰(忽略大小寫) ? ? ?* @param cipherText ? ? ?* ? ? ? ? ? ?16進(jìn)制加密后的字符串 ? ? ?* @param paramStr ? ? ?* ? ? ? ? ? ?加密前的字符串 ? ? ?* @return 是否為同一數(shù)據(jù) ? ? ?* @throws Exception ? ? ?*/ ? ? public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception { ? ? ? ? // 用于接收校驗結(jié)果 ? ? ? ? boolean flag = false; ? ? ? ? // hexString-->byte[] ? ? ? ? byte[] keyData = ByteUtils.fromHexString(hexKey); ? ? ? ? // 將16進(jìn)制字符串轉(zhuǎn)換成數(shù)組 ? ? ? ? byte[] cipherData = ByteUtils.fromHexString(cipherText); ? ? ? ? // 解密 ? ? ? ? byte[] decryptData = decrypt_Ecb_Padding(keyData, cipherData); ? ? ? ? // 將原字符串轉(zhuǎn)換成byte[] ? ? ? ? byte[] srcData = paramStr.getBytes(ENCODING); ? ? ? ? // 判斷2個數(shù)組是否一致 ? ? ? ? flag = Arrays.equals(decryptData, srcData); ? ? ? ? return flag; ? ? } ? ? public static void main(String[] args) { ? ? ? ? try { ? ? ? ? ? ? String json = "13800138000"; ? ? ? ? ? ? // 自定義的32位16進(jìn)制密鑰 ? ? ? ? ? ? String key = "86C63180C2806ED1F47B859DE501215B"; ? ? ? ? ? ? String cipher = SM4Util.encryptEcb(key, json); ? ? ? ? ? ? System.out.println(cipher); ? ? ? ? ? ? System.out.println(SM4Util.verifyEcb(key, cipher, json));// true ? ? ? ? ? ? json = SM4Util.decryptEcb(key, cipher); ? ? ? ? ? ? System.out.println(json); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } }
注意
在密碼學(xué)中,分組加密(英語:Block cipher),又稱分塊加密或塊密碼,是一種對稱密鑰算法。它將明文分成多個等長的模塊(block),使用確定的算法和對稱密鑰對每組分別加密解密。
分組加密是極其重要的加密協(xié)議組成,其中典型的如DES和AES作為美國政府核定的標(biāo)準(zhǔn)加密算法,應(yīng)用領(lǐng)域從電子郵件加密到銀行交易轉(zhuǎn)帳,非常廣泛。
國密即國家密碼局認(rèn)定的國產(chǎn)密碼算法。
主要有SM1,SM2,SM3,SM4。密鑰長度和分組長度均為128位。
- SM1為對稱加密。其加密強度與AES相當(dāng)。該算法不公開,調(diào)用該算法時,需要通過加密芯片的接口進(jìn)行調(diào)用。
- SM2為非對稱加密,基于ECC。該算法已公開。由于該算法基于ECC,故其簽名速度與秘鑰生成速度都快于RSA。ECC 256位(SM2采用的就是ECC 256位的一種)安全強度比RSA 2048位高,但運算速度快于RSA。
- SM3消息摘要??梢杂肕D5作為對比理解。該算法已公開。校驗結(jié)果為256位。
- SM4無線局域網(wǎng)標(biāo)準(zhǔn)的分組數(shù)據(jù)算法。對稱加密,密鑰長度和分組長度均為128位。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java操作SSH2實現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令
這篇文章主要為大家詳細(xì)介紹了Java如何操作SSH2實現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-01-01java?環(huán)境配置(2023年詳細(xì)教程)
這篇文章首先為了完善我的知識體系,今后一些軟件的安裝教程也可能會用到想寫一個更加詳細(xì)的,因為這并不僅僅是寫給?IT?行業(yè)的,其它行業(yè)可能也需要配置java環(huán)境2023-06-06java中switch條件語句的三種語法、用法及支持的參數(shù)類型
Java中的switch語句是一種多分支選擇結(jié)構(gòu),可以一個變量的值改變程序的控制流,這篇文章主要給大家介紹了關(guān)于java中switch條件語句的三種語法、用法及支持的參數(shù)類型的相關(guān)資料,需要的朋友可以參考下2024-06-06Mybatis傳list參數(shù)調(diào)用oracle存儲過程的解決方法
怎么利用MyBatis傳List類型參數(shù)到數(shù)據(jù)庫存儲過程中實現(xiàn)批量插入數(shù)據(jù)?接下來通過本文給大家介紹Mybatis傳list參數(shù)調(diào)用oracle存儲過程,需要的朋友可以參考下2017-03-03SpringBoot如何配置獲取request中body的json格式參數(shù)
這篇文章主要介紹了SpringBoot如何配置獲取request中body的json格式參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境搭建及配置
Elastic-Job在配置中提供了JobEventConfiguration,支持?jǐn)?shù)據(jù)庫方式配置,會在數(shù)據(jù)庫中自動創(chuàng)建JOB_EXECUTION_LOG和JOB_STATUS_TRACE_LOG兩張表以及若干索引,來記錄作業(yè)的相關(guān)信息2023-02-02SpringBoot配置文件中系統(tǒng)環(huán)境變量存在特殊字符的處理方式
這篇文章主要介紹了SpringBoot配置文件中系統(tǒng)環(huán)境變量存在特殊字符的處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02