前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >国密sm4加密算法

国密sm4加密算法

作者头像
全栈程序员站长
发布2022-11-11 15:27:05
5900
发布2022-11-11 15:27:05
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

国密sm4加解密算法工具类,可用于生产环境
代码语言:javascript
复制
package com.example.demo.endecryption.utils;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.Security;
/** * 国密sm4加解密 */
public class Sm4Util { 

public enum Algorithm { 

SM4("SM4","SM4","key长度:16 byte");
private String keyAlgorithm;
private String transformation;
private String description;//描述
Algorithm(String keyAlgorithm, String transformation, String description) { 

this.keyAlgorithm = keyAlgorithm;
this.transformation = transformation;
this.description = description;
}
public String getKeyAlgorithm() { 

return this.keyAlgorithm;
}
public String getTransformation() { 

return this.transformation;
}
public String getDescription() { 

return this.description;
}
}
private static final String PROVIDER_NAME = "BC";//BouncyCastleProvider的名字
static { 

Security.addProvider(new BouncyCastleProvider());
}
/** * 自定字符串产生密钥 * @param algorithm 加解密算法 * @param keyStr 密钥字符串 * @param charset 编码字符集 * @return 密钥 */
public static SecretKey genKeyByStr(Algorithm algorithm, String keyStr, Charset charset) { 

return readKeyFromBytes(algorithm, keyStr.getBytes(charset));
}
/** * 根据指定字节数组产生密钥 * @param algorithm 加解密算法 * @param keyBytes 密钥字节数组 * @return 密钥 */
public static SecretKey readKeyFromBytes(Sm4Util.Algorithm algorithm, byte[] keyBytes) { 

return new SecretKeySpec(keyBytes, algorithm.getKeyAlgorithm());
}
/****************************加密*********************************/
/** * 加密字符串,并进行base64编码 * @param algorithm 加解密算法 * @param key 密钥 * @param data 明文 * @param charset 编码字符集 * @return 密文 * @throws InvalidKeyException 密钥错误 */
public static String encryptBase64(Sm4Util.Algorithm algorithm, SecretKey key, String data, Charset charset) throws InvalidKeyException { 

return Base64.encodeBase64String(encrypt(algorithm, key, data.getBytes(charset)));
}
/** * 加密字节数组 * @param algorithm 加解密算法 * @param key 密钥 * @param data 明文 * @return 密文 * @throws InvalidKeyException 密钥错误 */
public static byte[] encrypt(Sm4Util.Algorithm algorithm, SecretKey key, byte[] data) throws InvalidKeyException { 

try { 

return cipherDoFinal(algorithm, Cipher.ENCRYPT_MODE, key, data);
} catch (BadPaddingException e) { 

throw new RuntimeException(e);//明文没有具体格式要求,不会出错。所以这个异常不需要外部捕获。
}
}
/** * 加解密字节数组 * @param algorithm 加解密算法 * @param opmode 操作:1加密,2解密 * @param key 密钥 * @param data 数据 * @throws InvalidKeyException 密钥错误 * @throws BadPaddingException 解密密文错误(加密模式没有) */
private static byte[] cipherDoFinal(Sm4Util.Algorithm algorithm, int opmode, SecretKey key, byte[] data) throws InvalidKeyException, BadPaddingException { 

Cipher cipher;
try { 

cipher = Cipher.getInstance(algorithm.getTransformation(), PROVIDER_NAME);
} catch (Exception e) { 

//NoSuchAlgorithmException:加密算法名是本工具类提供的,如果错了业务没有办法处理。所以这个异常不需要外部捕获。
//NoSuchProviderException:Provider是本工具类提供的,如果错了业务没有办法处理。所以这个异常不需要外部捕获。
//NoSuchPaddingException:没有特定的填充机制,与环境有关,业务没有办法处理。所以这个异常不需要外部捕获。
throw new RuntimeException(e);
}
cipher.init(opmode, key);
try { 

return cipher.doFinal(data);
} catch (IllegalBlockSizeException e) { 

throw new RuntimeException(e);//业务不需要将数据分块(好像由底层处理了),如果错了业务没有办法处理。所以这个异常不需要外部捕获。
}
}
/****************************解密*********************************/
/** * 对字符串先进行base64解码,再解密 * @param algorithm 加解密算法 * @param key 密钥 * @param data 密文 * @param charset 编码字符集 * @return 明文 * @throws InvalidKeyException 密钥错误 * @throws BadPaddingException 密文错误 */
public static String decryptBase64(Sm4Util.Algorithm algorithm, SecretKey key, String data, Charset charset)
throws InvalidKeyException, BadPaddingException { 

return new String(decrypt(algorithm, key, Base64.decodeBase64(data)), charset);
}
/** * 解密字节数组 * @param algorithm 加解密算法 * @param key 密钥 * @param data 密文 * @return 明文 * @throws InvalidKeyException 密钥错误 * @throws BadPaddingException 密文错误 */
public static byte[] decrypt(Sm4Util.Algorithm algorithm, SecretKey key, byte[] data) throws InvalidKeyException, BadPaddingException { 

return cipherDoFinal(algorithm, Cipher.DECRYPT_MODE, key, data);
}
}
测试
代码语言:javascript
复制
 /** * 国密sm4加解密 */
@Test
public void sm4Test() throws InvalidKeyException, BadPaddingException { 

Sm4Util .Algorithm algorithm = SymEncUtil.Algorithm.SM4;
//16位密钥字符串
String encryptKey = "0123456789ABCDEF";
//编码格式
Charset encryptCharset = StandardCharsets.UTF_8;
//生产密钥
SecretKey key = Sm4Util .genKeyByStr(algorithm, encryptKey, encryptCharset);
//加密
String encryptBase64 = Sm4Util .encryptBase64(algorithm, key, "123456", encryptCharset);
System.out.println("encryptBase64 = " + encryptBase64);//encryptBase64=QtrH8m/aR9x/cySEoUb+Nw==
//解密
String decryptBase64 = Sm4Util .decryptBase64(algorithm, key, "QtrH8m/aR9x/cySEoUb+Nw==", encryptCharset);
System.out.println("decryptBase64 = " + decryptBase64);
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185499.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月5日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 国密sm4加解密算法工具类,可用于生产环境
  • 测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档