sm4解密优化

This commit is contained in:
2024-12-16 10:12:59 +08:00
parent f1d0e7df42
commit 8f4949af80

View File

@@ -10,8 +10,7 @@ import java.util.regex.Pattern;
/** /**
* @author yexibao * @author yexibao
*/ */
public class Sm4Utils public class Sm4Utils {
{
//全局秘钥 //全局秘钥
public final static String globalSecretKey = "11HDESaAhiHHug2z"; public final static String globalSecretKey = "11HDESaAhiHHug2z";
@@ -37,26 +36,20 @@ public class Sm4Utils
this.iv = iv; this.iv = iv;
} }
public Sm4Utils(String secretKey) public Sm4Utils(String secretKey) {
{
this.secretKey = secretKey; this.secretKey = secretKey;
} }
public String encryptData_ECB(String plainText) public String encryptData_ECB(String plainText) {
{ try {
try
{
Sm4Context ctx = new Sm4Context(); Sm4Context ctx = new Sm4Context();
ctx.isPadding = true; ctx.isPadding = true;
ctx.mode = Sm4.SM4_ENCRYPT; ctx.mode = Sm4.SM4_ENCRYPT;
byte[] keyBytes; byte[] keyBytes;
if (HEX_STRING) if (HEX_STRING) {
{
keyBytes = util.hexStringToBytes(secretKey); keyBytes = util.hexStringToBytes(secretKey);
} } else {
else
{
keyBytes = secretKey.getBytes(); keyBytes = secretKey.getBytes();
} }
@@ -64,66 +57,59 @@ public class Sm4Utils
sm4.sm4_setkey_enc(ctx, keyBytes); sm4.sm4_setkey_enc(ctx, keyBytes);
byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes("GBK")); byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes("GBK"));
String cipherText = new BASE64Encoder().encode(encrypted); String cipherText = new BASE64Encoder().encode(encrypted);
if (cipherText != null && cipherText.trim().length() > 0) if (cipherText != null && cipherText.trim().length() > 0) {
{
Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(cipherText); Matcher m = p.matcher(cipherText);
cipherText = m.replaceAll(""); cipherText = m.replaceAll("");
} }
return cipherText; return cipherText;
} } catch (Exception e) {
catch (Exception e)
{
return null; return null;
} }
} }
public String decryptData_ECB(String cipherText) public String decryptData_ECB(String cipherText) {
{ try {
try
{
Sm4Context ctx = new Sm4Context(); Sm4Context ctx = new Sm4Context();
ctx.isPadding = true; ctx.isPadding = true;
ctx.mode = Sm4.SM4_DECRYPT; ctx.mode = Sm4.SM4_DECRYPT;
byte[] keyBytes; byte[] keyBytes;
if (HEX_STRING) if (HEX_STRING) {
{
keyBytes = util.hexStringToBytes(secretKey); keyBytes = util.hexStringToBytes(secretKey);
} } else {
else
{
keyBytes = secretKey.getBytes(); keyBytes = secretKey.getBytes();
} }
Sm4 sm4 = new Sm4(); Sm4 sm4 = new Sm4();
sm4.sm4_setkey_dec(ctx, keyBytes); sm4.sm4_setkey_dec(ctx, keyBytes);
byte[] decrypted = sm4.sm4_crypt_ecb(ctx, new BASE64Decoder().decodeBuffer(cipherText)); byte[] decrypted = sm4.sm4_crypt_ecb(ctx, new BASE64Decoder().decodeBuffer(cipherText));
return new String(decrypted, "GBK"); String tempText = new String(decrypted, "GBK");
String finalText = "";
for (int i = 0; i < tempText.length(); i++) {
char c = tempText.charAt(i);
if ((int) c != 0) {
finalText += c;
} }
catch (Exception e) }
{ return finalText;
} catch (Exception e) {
return null; return null;
} }
} }
public String encryptData_CBC(String plainText) public String encryptData_CBC(String plainText) {
{ try {
try
{
Sm4Context ctx = new Sm4Context(); Sm4Context ctx = new Sm4Context();
ctx.isPadding = true; ctx.isPadding = true;
ctx.mode = Sm4.SM4_ENCRYPT; ctx.mode = Sm4.SM4_ENCRYPT;
byte[] keyBytes; byte[] keyBytes;
byte[] ivBytes; byte[] ivBytes;
if (HEX_STRING) if (HEX_STRING) {
{
keyBytes = util.hexStringToBytes(secretKey); keyBytes = util.hexStringToBytes(secretKey);
ivBytes = util.hexStringToBytes(iv); ivBytes = util.hexStringToBytes(iv);
} } else {
else
{
keyBytes = secretKey.getBytes(); keyBytes = secretKey.getBytes();
ivBytes = iv.getBytes(); ivBytes = iv.getBytes();
} }
@@ -132,37 +118,29 @@ public class Sm4Utils
sm4.sm4_setkey_enc(ctx, keyBytes); sm4.sm4_setkey_enc(ctx, keyBytes);
byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes("GBK")); byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes("GBK"));
String cipherText = new BASE64Encoder().encode(encrypted); String cipherText = new BASE64Encoder().encode(encrypted);
if (cipherText != null && cipherText.trim().length() > 0) if (cipherText != null && cipherText.trim().length() > 0) {
{
Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(cipherText); Matcher m = p.matcher(cipherText);
cipherText = m.replaceAll(""); cipherText = m.replaceAll("");
} }
return cipherText; return cipherText;
} } catch (Exception e) {
catch (Exception e)
{
return null; return null;
} }
} }
public String decryptData_CBC(String cipherText) public String decryptData_CBC(String cipherText) {
{ try {
try
{
Sm4Context ctx = new Sm4Context(); Sm4Context ctx = new Sm4Context();
ctx.isPadding = true; ctx.isPadding = true;
ctx.mode = Sm4.SM4_DECRYPT; ctx.mode = Sm4.SM4_DECRYPT;
byte[] keyBytes; byte[] keyBytes;
byte[] ivBytes; byte[] ivBytes;
if (HEX_STRING) if (HEX_STRING) {
{
keyBytes = util.hexStringToBytes(secretKey); keyBytes = util.hexStringToBytes(secretKey);
ivBytes = util.hexStringToBytes(iv); ivBytes = util.hexStringToBytes(iv);
} } else {
else
{
keyBytes = secretKey.getBytes(); keyBytes = secretKey.getBytes();
ivBytes = iv.getBytes(); ivBytes = iv.getBytes();
} }
@@ -171,22 +149,22 @@ public class Sm4Utils
sm4.sm4_setkey_dec(ctx, keyBytes); sm4.sm4_setkey_dec(ctx, keyBytes);
byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, new BASE64Decoder().decodeBuffer(cipherText)); byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, new BASE64Decoder().decodeBuffer(cipherText));
return new String(decrypted, "GBK"); return new String(decrypted, "GBK");
} } catch (Exception e) {
catch (Exception e)
{
return null; return null;
} }
} }
public static void main(String[] args) throws IOException public static void main(String[] args) throws IOException {
{ String plainText = "@#001njcnpqs";
String plainText ="@#001njcnpqs";
//11HDESaAhiHHugDz //11HDESaAhiHHugDz
String secretKey = "11HDESaAhiHHug2z"; String secretKey = "11HDESaAhiHHug2z";
Sm4Utils sm4 = new Sm4Utils(secretKey); Sm4Utils sm4 = new Sm4Utils(secretKey);
String cipherText = sm4.encryptData_ECB(plainText); String cipherText = sm4.encryptData_ECB(plainText);
System.out.println("加密后:" + cipherText);
String cipherText1 = sm4.decryptData_ECB(cipherText); String cipherText1 = sm4.decryptData_ECB(cipherText);
System.out.println("解密后:" + cipherText1);
cipherText = cipherText + "11HDESaAhiHHug2z"; cipherText = cipherText + "11HDESaAhiHHug2z";
sm4.setSecretKey("11HDESaAhiHHug2z"); sm4.setSecretKey("11HDESaAhiHHug2z");
cipherText = sm4.encryptData_ECB(cipherText); cipherText = sm4.encryptData_ECB(cipherText);