|
@@ -34,25 +34,26 @@ public class SM4Utils {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static byte[] encrypt(byte[] plainText) throws Exception {
|
|
|
- byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
|
|
- // 密钥必须是16字节(128位)
|
|
|
- SecretKeySpec keySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
|
|
|
-
|
|
|
- // 获取 Cipher 实例,指定算法为 "SM4/ECB/NoPadding"
|
|
|
- Cipher cipher = Cipher.getInstance(ALGORITHM, PROVIDER);
|
|
|
-
|
|
|
- // 初始化 Cipher 为加密模式
|
|
|
- cipher.init(Cipher.ENCRYPT_MODE, keySpec);
|
|
|
-
|
|
|
- //保证加密明文长度为 16 的倍数
|
|
|
- int paddingLength = 16 - (plainText.length % 16);
|
|
|
- if (paddingLength != 16) {
|
|
|
- byte[] fillBytes = new byte[plainText.length + paddingLength];
|
|
|
- System.arraycopy(plainText, 0, fillBytes, 0, plainText.length);
|
|
|
- plainText = fillBytes;
|
|
|
- }
|
|
|
- // 执行加密
|
|
|
- return cipher.doFinal(plainText);
|
|
|
+ return plainText;
|
|
|
+// byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
|
|
+// // 密钥必须是16字节(128位)
|
|
|
+// SecretKeySpec keySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
|
|
|
+//
|
|
|
+// // 获取 Cipher 实例,指定算法为 "SM4/ECB/NoPadding"
|
|
|
+// Cipher cipher = Cipher.getInstance(ALGORITHM, PROVIDER);
|
|
|
+//
|
|
|
+// // 初始化 Cipher 为加密模式
|
|
|
+// cipher.init(Cipher.ENCRYPT_MODE, keySpec);
|
|
|
+//
|
|
|
+// //保证加密明文长度为 16 的倍数
|
|
|
+// int paddingLength = 16 - (plainText.length % 16);
|
|
|
+// if (paddingLength != 16) {
|
|
|
+// byte[] fillBytes = new byte[plainText.length + paddingLength];
|
|
|
+// System.arraycopy(plainText, 0, fillBytes, 0, plainText.length);
|
|
|
+// plainText = fillBytes;
|
|
|
+// }
|
|
|
+// // 执行加密
|
|
|
+// return cipher.doFinal(plainText);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -62,16 +63,17 @@ public class SM4Utils {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static byte[] decrypt(byte[] cipherText) throws Exception {
|
|
|
- if (cipherText.length % 16 != 0) {
|
|
|
- log.info("sm4解密,密文长度不为 16 倍数,解密失败\n" + ByteArrayUtils.bytesToHexString(cipherText));
|
|
|
- return new byte[0];
|
|
|
- }
|
|
|
- byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
|
|
- // 密钥必须是16字节(128位)
|
|
|
- SecretKeySpec keySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
|
|
|
-
|
|
|
- Cipher cipher = Cipher.getInstance(ALGORITHM, PROVIDER);
|
|
|
- cipher.init(Cipher.DECRYPT_MODE, keySpec);
|
|
|
- return cipher.doFinal(cipherText);
|
|
|
+ return cipherText;
|
|
|
+// if (cipherText.length % 16 != 0) {
|
|
|
+// log.info("sm4解密,密文长度不为 16 倍数,解密失败\n" + ByteArrayUtils.bytesToHexString(cipherText));
|
|
|
+// return new byte[0];
|
|
|
+// }
|
|
|
+// byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
|
|
|
+// // 密钥必须是16字节(128位)
|
|
|
+// SecretKeySpec keySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
|
|
|
+//
|
|
|
+// Cipher cipher = Cipher.getInstance(ALGORITHM, PROVIDER);
|
|
|
+// cipher.init(Cipher.DECRYPT_MODE, keySpec);
|
|
|
+// return cipher.doFinal(cipherText);
|
|
|
}
|
|
|
}
|