|
@@ -158,7 +158,7 @@ public class SM4Utils {
|
|
|
* @param cipherText
|
|
|
* @return
|
|
|
*/
|
|
|
- public String decryptData_CBC(String cipherText) {
|
|
|
+ public String decryptData_CBC(String cipherText,String mac) {
|
|
|
try {
|
|
|
byte[] encrypted = Util.hexToByte(cipherText);
|
|
|
cipherText = Base64.encode(encrypted);
|
|
@@ -175,10 +175,10 @@ public class SM4Utils {
|
|
|
byte[] keyBytes;
|
|
|
byte[] ivBytes;
|
|
|
if (hexString) {
|
|
|
- keyBytes = Util.hexStringToBytes(secretKey);
|
|
|
+ keyBytes = Util.hexStringToBytes(mac);
|
|
|
ivBytes = Util.hexStringToBytes(iv);
|
|
|
} else {
|
|
|
- keyBytes = secretKey.getBytes();
|
|
|
+ keyBytes = mac.getBytes();
|
|
|
ivBytes = iv.getBytes();
|
|
|
}
|
|
|
|
|
@@ -195,31 +195,31 @@ public class SM4Utils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) throws IOException {
|
|
|
- String plainText = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiLpobnnm64iLCJpYXQiOjE3MDA3MDQzMTQsImV4cCI6MTcwMTI3MzYwMH0.eWbknSZZnlpOiHhKtddjzMKl0FbUVFznV3OB6aanUlA";
|
|
|
-// String s = Util.byteToHex(plainText.getBytes());
|
|
|
-// System.out.println("原文" + s);
|
|
|
- SM4Utils sm4 = new SM4Utils();
|
|
|
- sm4.secretKey = "C97E76AF40C82627E4BDF23B2DB9D476";
|
|
|
- sm4.hexString = true;
|
|
|
+// public static void main(String[] args) throws IOException {
|
|
|
+// String plainText = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiLpobnnm64iLCJpYXQiOjE3MDA3MDQzMTQsImV4cCI6MTcwMTI3MzYwMH0.eWbknSZZnlpOiHhKtddjzMKl0FbUVFznV3OB6aanUlA";
|
|
|
+//// String s = Util.byteToHex(plainText.getBytes());
|
|
|
+//// System.out.println("原文" + s);
|
|
|
+// SM4Utils sm4 = new SM4Utils();
|
|
|
+// sm4.secretKey = "C97E76AF40C82627E4BDF23B2DB9D476";
|
|
|
+// sm4.hexString = true;
|
|
|
+////
|
|
|
+//// System.out.println("ECB模式加密");
|
|
|
+//// String cipherText = sm4.encryptData_ECB(plainText);
|
|
|
+//// System.out.println("密文: " + cipherText);
|
|
|
+//// System.out.println();
|
|
|
+////
|
|
|
+//// System.out.println("ECB模式解密");
|
|
|
+//// String plainText1 = sm4.decryptData_ECB(cipherText);
|
|
|
+//// System.out.println("明文: " + plainText1);
|
|
|
+//// System.out.println();
|
|
|
//
|
|
|
-// System.out.println("ECB模式加密");
|
|
|
-// String cipherText = sm4.encryptData_ECB(plainText);
|
|
|
-// System.out.println("密文: " + cipherText);
|
|
|
-// System.out.println();
|
|
|
+// System.out.println("CBC模式加密");
|
|
|
+// sm4.iv = "31313131313131313131313131313131";
|
|
|
+// String cipherText2 = sm4.encryptData_CBC(plainText);
|
|
|
+// System.out.println("加密密文: " + cipherText2);
|
|
|
//
|
|
|
-// System.out.println("ECB模式解密");
|
|
|
-// String plainText1 = sm4.decryptData_ECB(cipherText);
|
|
|
-// System.out.println("明文: " + plainText1);
|
|
|
-// System.out.println();
|
|
|
-
|
|
|
- System.out.println("CBC模式加密");
|
|
|
- sm4.iv = "31313131313131313131313131313131";
|
|
|
- String cipherText2 = sm4.encryptData_CBC(plainText);
|
|
|
- System.out.println("加密密文: " + cipherText2);
|
|
|
-
|
|
|
- String plainText3 = sm4.decryptData_CBC(cipherText2);
|
|
|
- System.out.println("解密明文: " + plainText3);
|
|
|
-
|
|
|
- }
|
|
|
+// String plainText3 = sm4.decryptData_CBC(cipherText2);
|
|
|
+// System.out.println("解密明文: " + plainText3);
|
|
|
+//
|
|
|
+// }
|
|
|
}
|