添加国密sm4加解密
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -84,6 +84,11 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk18on</artifactId>
|
||||
<version>1.78.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
@@ -137,4 +142,4 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
</project>
|
||||
104
src/main/java/com/labelsys/backend/util/SM4Util.java
Normal file
104
src/main/java/com/labelsys/backend/util/SM4Util.java
Normal file
@@ -0,0 +1,104 @@
|
||||
package com.labelsys.backend.util;
|
||||
|
||||
import org.bouncycastle.crypto.engines.SM4Engine;
|
||||
import org.bouncycastle.crypto.modes.CBCBlockCipher;
|
||||
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
|
||||
import org.bouncycastle.crypto.params.KeyParameter;
|
||||
import org.bouncycastle.crypto.params.ParametersWithIV;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
public class SM4Util {
|
||||
|
||||
private static final int KEY_LENGTH = 16;
|
||||
private static final int IV_LENGTH = 16;
|
||||
|
||||
public static String encrypt(String plainText, String key, String iv) {
|
||||
try {
|
||||
validateKey(key);
|
||||
validateIV(iv);
|
||||
|
||||
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] ivBytes = iv.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] plainBytes = plainText.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
SM4Engine engine = new SM4Engine();
|
||||
CBCBlockCipher blockCipher = new CBCBlockCipher(engine);
|
||||
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher);
|
||||
|
||||
KeyParameter keyParameter = new KeyParameter(keyBytes);
|
||||
ParametersWithIV parametersWithIV = new ParametersWithIV(keyParameter, ivBytes);
|
||||
cipher.init(true, parametersWithIV);
|
||||
|
||||
byte[] encrypted = new byte[cipher.getOutputSize(plainBytes.length)];
|
||||
int len = cipher.processBytes(plainBytes, 0, plainBytes.length, encrypted, 0);
|
||||
len += cipher.doFinal(encrypted, len);
|
||||
|
||||
return Base64.getEncoder().encodeToString(encrypted);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("SM4加密失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String decrypt(String cipherText, String key, String iv) {
|
||||
try {
|
||||
validateKey(key);
|
||||
validateIV(iv);
|
||||
|
||||
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] ivBytes = iv.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] cipherBytes = Base64.getDecoder().decode(cipherText);
|
||||
|
||||
SM4Engine engine = new SM4Engine();
|
||||
CBCBlockCipher blockCipher = new CBCBlockCipher(engine);
|
||||
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher);
|
||||
|
||||
KeyParameter keyParameter = new KeyParameter(keyBytes);
|
||||
ParametersWithIV parametersWithIV = new ParametersWithIV(keyParameter, ivBytes);
|
||||
cipher.init(false, parametersWithIV);
|
||||
|
||||
byte[] decrypted = new byte[cipher.getOutputSize(cipherBytes.length)];
|
||||
int len = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decrypted, 0);
|
||||
len += cipher.doFinal(decrypted, len);
|
||||
|
||||
return new String(decrypted, 0, len, StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("SM4解密失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String encrypt(String plainText, String key) {
|
||||
return encrypt(plainText, key, key.substring(0, IV_LENGTH));
|
||||
}
|
||||
|
||||
public static String decrypt(String cipherText, String key) {
|
||||
return decrypt(cipherText, key, key.substring(0, IV_LENGTH));
|
||||
}
|
||||
|
||||
private static void validateKey(String key) {
|
||||
if (key == null || key.length() != KEY_LENGTH) {
|
||||
throw new IllegalArgumentException("SM4密钥长度必须为16位");
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateIV(String iv) {
|
||||
if (iv == null || iv.length() != IV_LENGTH) {
|
||||
throw new IllegalArgumentException("SM4 IV长度必须为16位");
|
||||
}
|
||||
}
|
||||
|
||||
static void main() {
|
||||
String key = "1234567890123456";
|
||||
String iv = "abcdefghijklmnop";
|
||||
|
||||
String plainText = "Hello World!";
|
||||
|
||||
String encrypted = SM4Util.encrypt(plainText, key, iv);
|
||||
String decrypted = SM4Util.decrypt(encrypted, key, iv);
|
||||
|
||||
System.out.println("原文: " + plainText);
|
||||
System.out.println("密文: " + encrypted);
|
||||
System.out.println("解密: " + decrypted);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user