最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java之SM4加密解密的實(shí)現(xiàn)

 更新時(shí)間:2023年06月19日 16:50:19   作者:樂得屁顛兒。  
這篇文章主要介紹了Java之SM4加密解密的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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個(gè)字節(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 密文長(zhǎng)度不固定,會(huì)隨著被加密字符串長(zhǎng)度的變化而變化
? ? ?*/
? ? 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暗號(hào),通過第二個(gè)參數(shù)判斷加密還是解密
? ? ? ? return cipher.doFinal(data);
? ? }
? ? /**
? ? ?* 生成ECB暗號(hào)
? ? ?*
? ? ?* @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暗號(hào),通過第二個(gè)參數(shù)判斷加密還是解密
? ? ? ? return cipher.doFinal(cipherText);
? ? }
? ? /**
? ? ?* 校驗(yàn)加密前后的字符串是否為同一數(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 {
? ? ? ? // 用于接收校驗(yàn)結(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個(gè)數(shù)組是否一致
? ? ? ? flag = Arrays.equals(decryptData, srcData);
? ? ? ? return flag;
? ? }
}

測(cè)試

public static void main(String[] args) {
? ? ? ? try {
? ? ? ? ? ? System.out.println("開始測(cè)試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);
? ? ? ? ? ? //校驗(yàn)加密前后是否為同一數(shù)據(jù)
? ? ? ? ? ? System.out.println("校驗(yàn):" + Sm4Util.verifyEcb(key, cipher, json));
? ? ? ? ? ? //解密
? ? ? ? ? ? json = Sm4Util.decryptEcb(key, cipher);
? ? ? ? ? ? System.out.println("解密后:" + json);
? ? ? ? ? ? System.out.println("結(jié)束===================");
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

國(guó)密SM4對(duì)稱加密Java加解密

SM4.0(原名SMS4.0)是中華人民共和國(guó)政府采用的一種分組密碼標(biāo)準(zhǔn),由國(guó)家密碼管理局于2012年3月21日發(fā)布。

相關(guān)標(biāo)準(zhǔn)為“GM/T 0002-2012《SM4分組密碼算法》(原SMS4分組密碼算法)”。

SM4是什么?

SM4

使用步驟

1.引入庫

代碼如下(示例):

<!--國(guó)密-->
? ? ? ? ? ? <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é)果驗(yàn)證
?* ? ? ? ? ?可逆算法
?* @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個(gè)字節(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暗號(hào)
? ? ?* @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;
? ? }
? ? /**
? ? ?* 自動(dòng)生成密鑰
? ? ?* @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
? ? ?* ? ? ? ? ?密文長(zhǎng)度不固定,會(huì)隨著被加密字符串長(zhǎng)度的變化而變化
? ? ?* @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);
? ? }
? ? /**
? ? ?* 校驗(yàn)加密前后的字符串是否為同一數(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 {
? ? ? ? // 用于接收校驗(yàn)結(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個(gè)數(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),又稱分塊加密或塊密碼,是一種對(duì)稱密鑰算法。它將明文分成多個(gè)等長(zhǎng)的模塊(block),使用確定的算法和對(duì)稱密鑰對(duì)每組分別加密解密。

分組加密是極其重要的加密協(xié)議組成,其中典型的如DES和AES作為美國(guó)政府核定的標(biāo)準(zhǔn)加密算法,應(yīng)用領(lǐng)域從電子郵件加密到銀行交易轉(zhuǎn)帳,非常廣泛。

國(guó)密即國(guó)家密碼局認(rèn)定的國(guó)產(chǎn)密碼算法。

主要有SM1,SM2,SM3,SM4。密鑰長(zhǎng)度和分組長(zhǎng)度均為128位。

  • SM1為對(duì)稱加密。其加密強(qiáng)度與AES相當(dāng)。該算法不公開,調(diào)用該算法時(shí),需要通過加密芯片的接口進(jìn)行調(diào)用。
  • SM2為非對(duì)稱加密,基于ECC。該算法已公開。由于該算法基于ECC,故其簽名速度與秘鑰生成速度都快于RSA。ECC 256位(SM2采用的就是ECC 256位的一種)安全強(qiáng)度比RSA 2048位高,但運(yùn)算速度快于RSA。
  • SM3消息摘要??梢杂肕D5作為對(duì)比理解。該算法已公開。校驗(yàn)結(jié)果為256位。
  • SM4無線局域網(wǎng)標(biāo)準(zhǔn)的分組數(shù)據(jù)算法。對(duì)稱加密,密鑰長(zhǎng)度和分組長(zhǎng)度均為128位。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot集成netty實(shí)現(xiàn)websocket通信功能

    SpringBoot集成netty實(shí)現(xiàn)websocket通信功能

    Netty是一個(gè)高性能、異步事件驅(qū)動(dòng)的網(wǎng)絡(luò)應(yīng)用框架,用于快速開發(fā)可維護(hù)的高性能協(xié)議服務(wù)器和客戶端,WebSocket 是一種網(wǎng)絡(luò)通信協(xié)議,相比傳統(tǒng)的HTTP協(xié)議,本文給大家介紹了SpringBoot集成netty實(shí)現(xiàn)websocket通信功能,需要的朋友可以參考下
    2024-03-03
  • 一次mybatis連接查詢遇到的坑實(shí)戰(zhàn)記錄

    一次mybatis連接查詢遇到的坑實(shí)戰(zhàn)記錄

    這篇文章主要給大家介紹了關(guān)于一次mybatis連接查詢遇到的坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 最新評(píng)論

    延津县| 贵定县| 北碚区| 游戏| 剑阁县| 墨江| 莲花县| 梁河县| 永兴县| 澄城县| 大竹县| 弥勒县| 上蔡县| 张掖市| 芜湖市| 睢宁县| 庆阳市| 民权县| 哈巴河县| 曲阜市| 虹口区| 阳山县| 吴桥县| 定日县| 平顶山市| 工布江达县| 泽普县| 积石山| 广西| 天镇县| 惠安县| 深泽县| 眉山市| 钦州市| 和田市| 丹寨县| 昌吉市| 荔波县| 高密市| 张家港市| 海南省|