Skip to content

Commit f50beb2

Browse files
committed
更新HMAC功能
1 parent 7bbb7d6 commit f50beb2

9 files changed

Lines changed: 138 additions & 63 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- 对称加密
3636
- 块加密
3737
- hash
38+
- HMAC
3839
- 古典
3940
- ROT13
4041
- 栅栏密码
@@ -81,7 +82,6 @@
8182

8283
## 待办清单
8384
- 图像工具 - 二维码
84-
- 现代加密 - HMAC
8585
- 现代加密 - RSA因数分解
8686
- 编码 - URL编码增强
8787
- 日常实用 - Unix时间戳

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.ffffffff0x</groupId>
88
<artifactId>BerylEnigma</artifactId>
9-
<version>1.11.1</version>
9+
<version>1.12.0</version>
1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1212
<maven.compiler.source>17</maven.compiler.source>

src/main/java/ffffffff0x/beryenigma/App/Controller/Encryption/Modern/HMAC/Modern_HMAC.java

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
import ffffffff0x.beryenigma.App.Controller.Encryption.Coding.HEXCoder.Coding_HEXCoder;
55
import org.apache.commons.codec.binary.Base64;
66
import org.apache.commons.lang3.StringUtils;
7+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
78

89
import javax.crypto.Mac;
910
import javax.crypto.SecretKey;
1011
import javax.crypto.spec.SecretKeySpec;
12+
import java.io.UnsupportedEncodingException;
13+
import java.security.InvalidKeyException;
14+
import java.security.NoSuchAlgorithmException;
15+
import java.security.Security;
1116

1217
/**
1318
* @program: BerylEnigma
@@ -23,38 +28,13 @@ public class Modern_HMAC {
2328
* @param keyMAC 加密方式
2429
* @return 字节数组
2530
*/
26-
public static byte[] encryptHMAC(byte[] data, String key, String keyMAC) {
31+
public static byte[] encryptHMAC(byte[] data, byte[] key, String keyMAC) throws NoSuchAlgorithmException, InvalidKeyException {
2732
SecretKey secretKey;
2833
byte[] bytes = null;
29-
try {
30-
secretKey = new SecretKeySpec(Base64.decodeBase64(key), keyMAC);
31-
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
32-
mac.init(secretKey);
33-
bytes = mac.doFinal(data);
34-
} catch (Exception e) {
35-
e.printStackTrace();
36-
}
34+
secretKey = new SecretKeySpec(key, keyMAC);
35+
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
36+
mac.init(secretKey);
37+
bytes = mac.doFinal(data);
3738
return bytes;
3839
}
39-
40-
/**
41-
* HMAC加密
42-
* @param data 需要加密的字符串
43-
* @param key 密钥
44-
* @param keyMAC 加密方式
45-
* @return 字符串
46-
*/
47-
public static String encryptHMAC(String data, String key, String keyMAC) {
48-
if (StringUtils.isBlank(data)) {
49-
return null;
50-
}
51-
byte[] bytes = encryptHMAC(data.getBytes(), key, keyMAC);
52-
return Coding_HEXCoder.encodeToString(bytes);
53-
}
54-
55-
public static void main(String[] args) throws Exception {
56-
String key = "123";
57-
String word = "123";
58-
System.out.println(encryptHMAC(word, Coding_Base64.encodeToString(key,"UTF-8"), "HMACSHA512/256"));
59-
}
6040
}

src/main/java/ffffffff0x/beryenigma/App/View/Modules/Encryption/Modern/HMAC/HMACController.java

Lines changed: 118 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@
22

33
import com.jfoenix.controls.JFXComboBox;
44
import com.jfoenix.controls.JFXTextArea;
5+
import ffffffff0x.beryenigma.App.Controller.Encryption.Modern.HMAC.Modern_HMAC;
6+
import ffffffff0x.beryenigma.App.Controller.Encryption.Modern.Hash.Modern_Hash;
7+
import ffffffff0x.beryenigma.App.View.Viewobj.PopupSettingDoubleColumnView;
8+
import ffffffff0x.beryenigma.App.View.Viewobj.PopupSettingNode;
59
import ffffffff0x.beryenigma.App.View.Viewobj.ViewControllerFileMode;
10+
import ffffffff0x.beryenigma.Init.Init;
611
import ffffffff0x.beryenigma.Init.ViewInit;
712
import ffffffff0x.beryenigma.Kit.Utils.FileUtils;
813
import javafx.fxml.FXML;
14+
import org.apache.commons.codec.binary.Base64;
15+
import org.apache.commons.codec.binary.Hex;
16+
17+
import java.io.UnsupportedEncodingException;
18+
import java.security.InvalidKeyException;
19+
import java.security.NoSuchAlgorithmException;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
import java.util.Objects;
923

1024
public class HMACController extends ViewControllerFileMode {
1125
/**
1226
* JTA_dst1 :HEX result
1327
* JTA_dst :base64 result
1428
*/
1529

16-
@FXML private JFXComboBox JCB_charset;
30+
@FXML private JFXComboBox JCB_charset = new JFXComboBox();
1731
@FXML private JFXComboBox<String> JCB_HMACMode;
1832
@FXML private JFXTextArea JTA_HMACKey;
1933

@@ -26,41 +40,48 @@ protected void initialize() {
2640

2741
@Override
2842
public void ONClickConfirm() {
29-
// super.ONClickConfirm();
30-
// JTA_dst.setStyle("-fx-text-fill: black");
31-
// JTA_dst1.setStyle("-fx-text-fill: black");
32-
// try{
33-
// String[] dst = new String[0];
34-
// if(JTB_modeSelect.getText().equals(Init.languageResourceBundle.getString("TextMode"))){
35-
// try {
36-
// dst = hash(JTA_src.getText().getBytes(JCB_charset.getValue().toString()));
37-
// } catch (UnsupportedEncodingException e) {
38-
// e.printStackTrace();
39-
// }
40-
// }else{
41-
// dst = hash(byteFile);
42-
// fileEncodeEnd();
43-
// }
44-
// JTA_dst1.setText(dst[0]);
45-
// JTA_dst.setText(dst[1]);
46-
// }catch (Exception e){
47-
// JTA_dst.setStyle("-fx-text-fill: red");
48-
// JTA_dst1.setStyle("-fx-text-fill: red");
49-
// JTA_dst.setText(e.getMessage());
50-
// JTA_dst1.setText(e.getMessage());
51-
// }
43+
super.ONClickConfirm();
44+
JTA_dst.setStyle("-fx-text-fill: black");
45+
JTA_dst1.setStyle("-fx-text-fill: black");
46+
try{
47+
String[] dst = new String[0];
48+
if(JTB_modeSelect.getText().equals(Init.languageResourceBundle.getString("TextMode"))){
49+
try {
50+
dst = hmac(JTA_src.getText().getBytes(JCB_charset.getValue().toString()),
51+
JTA_HMACKey.getText().getBytes(JCB_charset.getValue().toString()));
52+
} catch (UnsupportedEncodingException e) {
53+
e.printStackTrace();
54+
}
55+
}else{
56+
dst = hmac(byteFile, JTA_HMACKey.getText().getBytes(JCB_charset.getValue().toString()));
57+
fileEncodeEnd();
58+
}
59+
JTA_dst1.setText(dst[0]);
60+
JTA_dst.setText(dst[1]);
61+
}catch (Exception e){
62+
JTA_dst.setStyle("-fx-text-fill: red");
63+
JTA_dst1.setStyle("-fx-text-fill: red");
64+
JTA_dst.setText(e.getMessage());
65+
JTA_dst1.setText(e.getMessage());
66+
}
5267
}
5368

5469
private void initComboBox(){
5570
ViewInit.comboBoxCharset(JCB_charset);
56-
71+
JCB_HMACMode.getItems().addAll(getInstanceNameList());
72+
JCB_HMACMode.setValue("HMACMD5");
73+
JCB_HMACMode.setVisibleRowCount(6);
5774

5875
}
5976

60-
private String[] hash(byte[] message){
61-
return null;
77+
private String[] hmac(byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException {
78+
String[] out = new String[2];
79+
out[0] = Hex.encodeHexString(Objects.requireNonNull(Modern_HMAC.encryptHMAC(data,
80+
key, JCB_HMACMode.getValue())));
81+
out[1] = Base64.encodeBase64String(Objects.requireNonNull(Modern_HMAC.encryptHMAC(data,
82+
key, JCB_HMACMode.getValue())));
83+
return out;
6284
}
63-
6485
@Override
6586
public void getFile(){
6687
super.getFile();
@@ -72,4 +93,73 @@ protected void JTADSTContextMenu() {
7293
super.JTADSTContextMenu();
7394
ViewInit.textAreaContextMenu(JTA_dst1,JTA_src);
7495
}
96+
97+
public List<String> getInstanceNameList() {
98+
List<String> result = Arrays.asList("HMACMD2",
99+
"HMACMD4",
100+
"HMACMD5",
101+
"HMACSM3",
102+
"HMACSHA1",
103+
"HMACSHA224",
104+
"HMACSHA256",
105+
"HMACSHA3-224",
106+
"HMACSHA3-256",
107+
"HMACSHA3-384",
108+
"HMACSHA3-512",
109+
"HMACSHA384",
110+
"HMACSHA512",
111+
"HMACSHA512/224",
112+
"HMACSHA512/256",
113+
"HMACPBESHA1",
114+
"HMACPBESHA224",
115+
"HMACPBESHA256",
116+
"HMACPBESHA384",
117+
"HMACPBESHA512",
118+
"HMACPBESHA512/224",
119+
"HMACPBESHA512/256",
120+
"HMACRIPEMD128",
121+
"HMACRIPEMD160",
122+
"HMACRIPEMD256",
123+
"HMACRIPEMD320",
124+
"HMACSKEIN-1024-1024",
125+
"HMACSKEIN-1024-384",
126+
"HMACSKEIN-1024-512",
127+
"HMACSKEIN-256-128",
128+
"HMACSKEIN-256-160",
129+
"HMACSKEIN-256-224",
130+
"HMACSKEIN-256-256",
131+
"HMACSKEIN-512-128",
132+
"HMACSKEIN-512-160",
133+
"HMACSKEIN-512-224",
134+
"HMACSKEIN-512-256",
135+
"HMACSKEIN-512-384",
136+
"HMACSKEIN-512-512",
137+
"HMACDSTU7564-256",
138+
"HMACDSTU7564-384",
139+
"HMACDSTU7564-512",
140+
"HMACGOST3411",
141+
"HMACGOST3411-2012-256",
142+
"HMACGOST3411-2012-512",
143+
"HMACKECCAK224",
144+
"HMACKECCAK256",
145+
"HMACKECCAK288",
146+
"HMACKECCAK384",
147+
"HMACKECCAK512",
148+
"HMACTIGER",
149+
"HMACWHIRLPOOL",
150+
"OLDHMACSHA384",
151+
"OLDHMACSHA512");
152+
return result;
153+
}
154+
155+
@Override
156+
protected void LoadPopupSettingNode() {
157+
super.LoadPopupSettingNode();
158+
159+
//弹出式控件框
160+
PopupSettingDoubleColumnView popupSettingView = new PopupSettingDoubleColumnView(ACP_controllerAnchorPane);
161+
162+
//弹出式控件框中添加初始化的控件
163+
popupSettingView.setSetting(new PopupSettingNode(Init.languageResourceBundle.getString("Charset"),JCB_charset,true));
164+
}
75165
}

src/main/java/ffffffff0x/beryenigma/App/View/Modules/Encryption/Modern/HMAC/HMACView.fxml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
<children>
1818
<JFXToggleButton fx:id="JTB_modeSelect" layoutX="15.0" layoutY="-15.0" onAction="#ONClickModeSelect" prefHeight="50.0" prefWidth="160.0" size="9.0" text="%TextMode" />
1919
<JFXTextArea fx:id="JTA_src" layoutX="40.0" layoutY="39.0" prefHeight="165.0" prefWidth="530.0" AnchorPane.leftAnchor="40.0" AnchorPane.rightAnchor="40.0" AnchorPane.topAnchor="30.0" />
20-
<HBox alignment="CENTER_RIGHT" layoutX="40.0" layoutY="220.0" prefHeight="70.0" spacing="20.0" AnchorPane.leftAnchor="40.0" AnchorPane.rightAnchor="40.0">
20+
<HBox alignment="CENTER_RIGHT" layoutX="40.0" layoutY="220.0" prefHeight="70.0" spacing="40.0" AnchorPane.leftAnchor="40.0" AnchorPane.rightAnchor="40.0">
2121
<children>
22-
<JFXComboBox fx:id="JCB_charset" prefHeight="30.0" prefWidth="100.0" />
23-
<JFXComboBox fx:id="JCB_HMACMode" prefHeight="30.0" prefWidth="110.0" />
22+
<JFXComboBox fx:id="JCB_HMACMode" prefHeight="30.0" prefWidth="180.0" />
2423
<JFXTextArea fx:id="JTA_HMACKey" prefHeight="50.0" prefWidth="140.0" promptText="%Key">
2524
<HBox.margin>
2625
<Insets bottom="10.0" top="10.0" />

src/main/java/ffffffff0x/beryenigma/App/View/Root/RootTreeNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public RootTreeNode(){
6969
modern.getChildren().add(symmetricEncryption);
7070
ItemAdd(symmetricEncryption,"BlockCipher","/ffffffff0x/beryenigma/App/View/Modules/Encryption/Modern/SymmetricEncryption/BlockCipher/BlockCipherView.fxml");
7171
ItemAdd(modern,"HASH", "/ffffffff0x/beryenigma/App/View/Modules/Encryption/Modern/Hash/HashView.fxml");
72+
ItemAdd(modern,"HMAC", "/ffffffff0x/beryenigma/App/View/Modules/Encryption/Modern/HMAC/HMACView.fxml");
7273

7374
//Authentication
7475
ItemAdd(authentication,"NTLM_Hash", "/ffffffff0x/beryenigma/App/View/Modules/Encryption/Modern/Authentication/NTLM/NTLMView.fxml");

src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
requires com.google.gson;
1111
requires java.naming;
1212
requires java.sql;
13+
requires org.bouncycastle.provider;
1314
}

src/main/resources/Language.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Base64Result = Base64Result
3636
LoadFile = LoadFile
3737
Preview = Preview
3838
SignBit = SignBit
39+
Charset = Charset
3940

4041
MultipleFileOutput = Multiple file output
4142

@@ -108,6 +109,7 @@ JWT = JWT
108109
AES = AES
109110
DES = DES
110111
HASH = HASH
112+
HMAC = HMAC
111113
SM3 = SM3
112114
SM4 = SM4
113115

src/main/resources/Language_zh.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Base64Result = Base64\u7ED3\u679C
3636
LoadFile = \u52A0\u8F7D\u6587\u4EF6
3737
Preview = \u9884\u89C8
3838
SignBit = \u7B26\u53F7\u4F4D
39+
Charset = \u5B57\u7B26\u96C6
3940

4041
MultipleFileOutput = \u591A\u6587\u4EF6\u8F93\u51FA
4142

@@ -106,6 +107,7 @@ Authentication = \u8BA4\u8BC1\u52A0\u5BC6
106107
AES = AES
107108
DES = DES
108109
HASH = HASH
110+
HMAC = HMAC
109111
SM3 = SM3
110112
SM4 = SM4
111113

0 commit comments

Comments
 (0)