From a519c324819578594142c0dc76d6a67df5ec3f68 Mon Sep 17 00:00:00 2001 From: Anner Date: Wed, 11 Nov 2020 09:45:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E4=BC=A0=E8=BE=93=E4=B9=B1=E7=A0=81=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20&=20=E8=B7=A8=E7=B3=BB=E7=BB=9F=E4=BC=A0=E8=BE=93=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=B8=8D=E5=AE=8C=E6=95=B4=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../com/fr/password/bean/RequestBean.java | 36 ++++ .../controller/PasswordController.java | 17 +- .../controller/StorageController.java | 102 ++++++--- .../controller/TransmissionController.java | 34 +-- .../fr/password/service/PasswordService.java | 4 +- .../fr/password/service/StorageService.java | 28 ++- .../java/com/fr/password/tool/Runner.java | 9 - .../com/fr/password/tool/SecurityToolbox.java | 15 +- .../fr/password/tool/keys/CustomSM2Keys.java | 10 + .../fr/password/tool/keys/LoadSeedKeys.java | 5 +- .../com/fr/password/tool/ui/FilePanel.java | 101 --------- .../fr/password/tool/ui/FrameConstants.java | 35 --- .../com/fr/password/tool/ui/MainFrame.java | 31 --- .../com/fr/password/tool/ui/TextPanel.java | 46 ---- .../com/fr/password/tool/ui/TipPanel.java | 32 --- .../com/fr/password/tool/ui/TopPanel.java | 183 ---------------- .../com/fr/password/tool/util/EncodeUtil.java | 13 ++ src/main/resources/static/index.html | 3 + src/main/resources/static/index.js | 204 ++++++++++++------ 20 files changed, 341 insertions(+), 569 deletions(-) create mode 100644 src/main/java/com/fr/password/bean/RequestBean.java delete mode 100644 src/main/java/com/fr/password/tool/Runner.java delete mode 100644 src/main/java/com/fr/password/tool/ui/FilePanel.java delete mode 100644 src/main/java/com/fr/password/tool/ui/FrameConstants.java delete mode 100644 src/main/java/com/fr/password/tool/ui/MainFrame.java delete mode 100644 src/main/java/com/fr/password/tool/ui/TextPanel.java delete mode 100644 src/main/java/com/fr/password/tool/ui/TipPanel.java delete mode 100644 src/main/java/com/fr/password/tool/ui/TopPanel.java diff --git a/build.gradle b/build.gradle index 330b8c6..d5ef8e5 100644 --- a/build.gradle +++ b/build.gradle @@ -47,6 +47,6 @@ jar { archiveVersion = '0.0.1' manifest { attributes "Manifest-Version": 1.0, - 'Main-Class': 'com.fr.password.Runner' + 'Main-Class': 'com.fr.password.terminal.TerminalRunner' } } diff --git a/src/main/java/com/fr/password/bean/RequestBean.java b/src/main/java/com/fr/password/bean/RequestBean.java new file mode 100644 index 0000000..faee2ca --- /dev/null +++ b/src/main/java/com/fr/password/bean/RequestBean.java @@ -0,0 +1,36 @@ +package com.fr.password.bean; + +/** + * desc + * + * @author Anner + * created on 2020-11-10 + */ +public class RequestBean { + private String key; + private String text; + + public RequestBean() { + } + + public RequestBean(String key, String text) { + this.key = key; + this.text = text; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/src/main/java/com/fr/password/controller/PasswordController.java b/src/main/java/com/fr/password/controller/PasswordController.java index eb12fcb..83e6dc6 100644 --- a/src/main/java/com/fr/password/controller/PasswordController.java +++ b/src/main/java/com/fr/password/controller/PasswordController.java @@ -1,13 +1,14 @@ package com.fr.password.controller; import com.fr.password.service.PasswordService; -import com.fr.password.tool.SecurityToolbox; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; +import java.util.Map; /** * desc @@ -22,17 +23,19 @@ public class PasswordController { @Autowired private PasswordService passwordService; - @RequestMapping("/sha") - public String sha(@RequestParam String plainText) throws IOException { + @PostMapping("/sha") + public String sha(@RequestBody Map params) throws IOException { + String plainText = (String) params.get("text"); return passwordService.sha(filter(plainText)); } - @RequestMapping("/sm3") - public String sm3(@RequestParam String plainText) throws IOException { + @PostMapping("/sm3") + public String sm3(@RequestBody Map params) throws IOException { + String plainText = (String) params.get("text"); return passwordService.sm3(filter(plainText)); } private String filter(String origin) throws IOException { - return new String(SecurityToolbox.getInstance().base642Byte(origin)); + return origin; } } diff --git a/src/main/java/com/fr/password/controller/StorageController.java b/src/main/java/com/fr/password/controller/StorageController.java index 99c91f6..e656b90 100644 --- a/src/main/java/com/fr/password/controller/StorageController.java +++ b/src/main/java/com/fr/password/controller/StorageController.java @@ -1,12 +1,11 @@ package com.fr.password.controller; -import com.fr.password.Constants; import com.fr.password.service.StorageService; -import com.fr.password.tool.SecurityToolbox; import org.bouncycastle.crypto.InvalidCipherTextException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; @@ -14,6 +13,7 @@ import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.spec.InvalidKeySpecException; +import java.util.Map; @RestController @RequestMapping("/storage") @@ -22,69 +22,103 @@ public class StorageController { @Autowired private StorageService storageService; - @RequestMapping("/encrypt/sm2") - private String sm2Encrypt(@RequestParam String plainText, @RequestParam String key) throws InvalidCipherTextException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException, IOException { - if (key.equals(Constants.DEFAULT_KEY)) { + @PostMapping("/encrypt/sm2") + private String sm2Encrypt(@RequestBody Map params) throws InvalidCipherTextException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException, IOException { + String key = (String) params.get("key"); + String plainText = (String) params.get("text"); + if (key == null) { return storageService.sm2Encrypt(filter(plainText), key, false); } return storageService.sm2Encrypt(filter(plainText), key); } - @RequestMapping("/decrypt/sm2") - private String sm2Decrypt(@RequestParam String cipherText, @RequestParam String key) throws Exception { - if (key.equals(Constants.DEFAULT_KEY)) { + @PostMapping("/decrypt/sm2") + private String sm2Decrypt(@RequestBody Map params) throws Exception { + String cipherText = (String) params.get("text"); + String key = (String) params.get("key"); + if (key == null) { return storageService.sm2Decrypt(filter(cipherText), key, false); } return storageService.sm2Decrypt(filter(cipherText), key); } - @RequestMapping("/encrypt/sm2/custom") - private String sm2CustomEncrypt(@RequestParam String plainText) throws Exception { - return storageService.sm2CustomEncrypt(filter(plainText)); + @PostMapping("/encrypt/sm2/custom") + private String sm2CustomEncrypt(@RequestBody Map params) throws Exception { + String plainText = (String) params.get("text"); + String key = (String) params.get("key"); + if (key == null) { + return storageService.sm2CustomEncrypt(filter(plainText), key, false); + } + return storageService.sm2CustomEncrypt(filter(plainText), key, true); } - @RequestMapping("/decrypt/sm2/custom") - private String sm2CustomDecrypt(@RequestParam String cipherText) throws Exception { - return storageService.sm2CustomDecrypt(filter(cipherText)); + @PostMapping("/decrypt/sm2/custom") + private String sm2CustomDecrypt(@RequestBody Map params) throws Exception { + String key = (String) params.get("key"); + String cipherText = (String) params.get("text"); + if (key == null) { + return storageService.sm2CustomDecrypt(filter(cipherText), key, false); + } + return storageService.sm2CustomDecrypt(filter(cipherText), key, true); } - @RequestMapping("/encrypt/sm2/seed") - private String sm2EncryptWithSeed(@RequestParam String plainText,@RequestParam String seed1,@RequestParam String seed2,@RequestParam String seed3) throws IOException, InvalidCipherTextException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { - return storageService.sm2EncryptWithSeed(filter(plainText),filter(seed1),filter(seed2),filter(seed3)); + @PostMapping("/encrypt/sm2/seed") + private String sm2EncryptWithSeed(@RequestBody Map params) throws IOException, InvalidCipherTextException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { + String plainText = (String) params.get("plainText"); + String seed1 = (String) params.get("seed1"); + String seed2 = (String) params.get("seed2"); + String seed3 = (String) params.get("seed3"); + return storageService.sm2EncryptWithSeed(filter(plainText), filter(seed1), filter(seed2), filter(seed3)); } - @RequestMapping("/decrypt/sm2/seed") - private String sm2DecryptWithSeed(@RequestParam String cipherText,@RequestParam String seed1,@RequestParam String seed2,@RequestParam String seed3) throws Exception { - return storageService.sm2DecryptWithSeed(filter(cipherText),filter(seed1),filter(seed2),filter(seed3)); + @PostMapping("/decrypt/sm2/seed") + private String sm2DecryptWithSeed(@RequestBody Map params) throws Exception { + String cipherText = (String) params.get("plainText"); + String seed1 = (String) params.get("seed1"); + String seed2 = (String) params.get("seed2"); + String seed3 = (String) params.get("seed3"); + return storageService.sm2DecryptWithSeed(filter(cipherText), filter(seed1), filter(seed2), filter(seed3)); } - @RequestMapping("/encrypt/rsa") - private String rsaEncrypt(@RequestParam String plainText, @RequestParam String key) throws IOException { - if (key.equals(Constants.DEFAULT_KEY)) { + @PostMapping("/encrypt/rsa") + private String rsaEncrypt(@RequestBody Map params) throws IOException { + String key = (String) params.get("key"); + String plainText = (String) params.get("text"); + if (key == null) { return storageService.rsaEncrypt(filter(plainText), key, false); } return storageService.rsaEncrypt(filter(plainText), key, true); } - @RequestMapping("/decrypt/rsa") - private String rsaDecrypt(@RequestParam String cipherText, @RequestParam String key) throws IOException { - if (key.equals(Constants.DEFAULT_KEY)) { + @PostMapping("/decrypt/rsa") + private String rsaDecrypt(@RequestBody Map params) throws IOException { + String key = (String) params.get("key"); + String cipherText = (String) params.get("text"); + if (key == null) { return storageService.rsaDecrypt(filter(cipherText), key, false); } return storageService.rsaDecrypt(filter(cipherText), key, true); } - @RequestMapping("/encrypt/rsa/seed") - private String rsaEncryptWithSeed(@RequestParam String plainText,@RequestParam String seed1,@RequestParam String seed2,@RequestParam String seed3) throws IOException, InvalidCipherTextException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { - return storageService.rsaEncryptWithSeed(filter(plainText),filter(seed1),filter(seed2),filter(seed3)); + @PostMapping("/encrypt/rsa/seed") + private String rsaEncryptWithSeed(@RequestBody Map params) throws IOException, InvalidCipherTextException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { + String plainText = (String) params.get("plainText"); + String seed1 = (String) params.get("seed1"); + String seed2 = (String) params.get("seed2"); + String seed3 = (String) params.get("seed3"); + return storageService.rsaEncryptWithSeed(filter(plainText), filter(seed1), filter(seed2), filter(seed3)); } - @RequestMapping("/decrypt/rsa/seed") - private String rsaDecryptWithSeed(@RequestParam String cipherText,@RequestParam String seed1,@RequestParam String seed2,@RequestParam String seed3) throws Exception { - return storageService.rsaDecryptWithSeed(filter(cipherText),filter(seed1),filter(seed2),filter(seed3)); + @PostMapping("/decrypt/rsa/seed") + private String rsaDecryptWithSeed(@RequestBody Map params) throws Exception { + String cipherText = (String) params.get("plainText"); + String seed1 = (String) params.get("seed1"); + String seed2 = (String) params.get("seed2"); + String seed3 = (String) params.get("seed3"); + return storageService.rsaDecryptWithSeed(filter(cipherText), filter(seed1), filter(seed2), filter(seed3)); } private String filter(String origin) throws IOException { - return new String(SecurityToolbox.getInstance().base642Byte(origin)).trim().replace("\\n","\n"); + return origin; } } diff --git a/src/main/java/com/fr/password/controller/TransmissionController.java b/src/main/java/com/fr/password/controller/TransmissionController.java index 273d6a5..980558a 100644 --- a/src/main/java/com/fr/password/controller/TransmissionController.java +++ b/src/main/java/com/fr/password/controller/TransmissionController.java @@ -3,11 +3,13 @@ package com.fr.password.controller; import com.fr.password.service.TransmissionService; import com.fr.password.tool.SecurityToolbox; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; +import java.util.Map; @RestController @RequestMapping("/transmission") @@ -16,28 +18,36 @@ public class TransmissionController { @Autowired private TransmissionService transmissionService; - @RequestMapping("/encrypt/aes") - private String aesEncrypt(@RequestParam String plainText, @RequestParam String key) throws Exception { + @PostMapping("/encrypt/aes") + private String aesEncrypt(@RequestBody Map params) throws Exception { + String plainText = (String) params.get("text"); + String key = (String) params.get("key"); return transmissionService.aesEncrypt(filter(plainText), filter(key)); } - @RequestMapping("/decrypt/aes") - private String aesDecrypt(@RequestParam String cipherText, @RequestParam String key) throws Exception { - return transmissionService.aesDecrypt(filter(cipherText),filter(key)); + @PostMapping("/decrypt/aes") + private String aesDecrypt(@RequestBody Map params) throws Exception { + String cipherText = (String) params.get("text"); + String key = (String) params.get("key"); + return transmissionService.aesDecrypt(filter(cipherText), filter(key)); } - @RequestMapping("/encrypt/sm4") - private String sm4Encrypt(@RequestParam String plainText, @RequestParam String key) throws Exception { - return transmissionService.sm4Encrypt(filter(plainText),filter(key)); + @PostMapping("/encrypt/sm4") + private String sm4Encrypt(@RequestBody Map params) throws Exception { + String plainText = (String) params.get("text"); + String key = (String) params.get("key"); + return transmissionService.sm4Encrypt(filter(plainText), filter(key)); } - @RequestMapping("/decrypt/sm4") - private String sm4Decrypt(@RequestParam String cipherText, @RequestParam String key) throws Exception { + @PostMapping("/decrypt/sm4") + private String sm4Decrypt(@RequestBody Map params) throws Exception { + String cipherText = (String) params.get("text"); + String key = (String) params.get("key"); return transmissionService.sm4Decrypt(filter(cipherText), filter(key)); } private String filter(String origin) throws IOException { - return new String(SecurityToolbox.getInstance().base642Byte(origin)).trim().replace("\\n","\n"); + return origin; } } diff --git a/src/main/java/com/fr/password/service/PasswordService.java b/src/main/java/com/fr/password/service/PasswordService.java index 20717ca..4d37024 100644 --- a/src/main/java/com/fr/password/service/PasswordService.java +++ b/src/main/java/com/fr/password/service/PasswordService.java @@ -16,7 +16,7 @@ public class PasswordService { return SecurityToolbox.getInstance().sha256(plainText); } - public String sm3(String plainText){ + public String sm3(String plainText) { return SecurityToolbox.getInstance().sm3Encrypt(plainText); } -} +} \ No newline at end of file diff --git a/src/main/java/com/fr/password/service/StorageService.java b/src/main/java/com/fr/password/service/StorageService.java index 57eaa10..17d3b99 100644 --- a/src/main/java/com/fr/password/service/StorageService.java +++ b/src/main/java/com/fr/password/service/StorageService.java @@ -42,21 +42,27 @@ public class StorageService { public String sm2EncryptWithSeed(String plainText, String seed1, String seed2, String seed3) throws InvalidKeySpecException, InvalidCipherTextException, NoSuchAlgorithmException, NoSuchProviderException, IOException, InvalidAlgorithmParameterException { - byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1,seed2,seed3); - return SecurityToolbox.getInstance().sm2Encrypt(plainText,LoadSeedKeys.getInstance().generateSM2Keys(random)); + byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1, seed2, seed3); + return SecurityToolbox.getInstance().sm2Encrypt(plainText, LoadSeedKeys.getInstance().generateSM2Keys(random)); } public String sm2DecryptWithSeed(String cipherText, String seed1, String seed2, String seed3) throws Exception { - byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1,seed2,seed3); - return SecurityToolbox.getInstance().sm2Decrypt(cipherText,LoadSeedKeys.getInstance().generateSM2Keys(random)); + byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1, seed2, seed3); + return SecurityToolbox.getInstance().sm2Decrypt(cipherText, LoadSeedKeys.getInstance().generateSM2Keys(random)); } - public String sm2CustomEncrypt(String plainText) throws Exception { + public String sm2CustomEncrypt(String plainText, String key, boolean isCustom) throws Exception { + if (isCustom) { + return CustomSM2Keys.getInstance().encrypt(plainText, SecretHelper.loadFromText(filter(key)).get(SecretHelper.PUBLIC_KEY)); + } return CustomSM2Keys.getInstance().encrypt(plainText); } - public String sm2CustomDecrypt(String cipherText) throws Exception { + public String sm2CustomDecrypt(String cipherText, String key, boolean isCustom) throws Exception { + if (isCustom) { + return CustomSM2Keys.getInstance().decrypt(cipherText, SecretHelper.loadFromText(filter(key)).get(SecretHelper.PRIVATE_KEY)); + } return CustomSM2Keys.getInstance().decrypt(cipherText); } @@ -75,16 +81,16 @@ public class StorageService { } public String rsaEncryptWithSeed(String plainText, String seed1, String seed2, String seed3) throws InvalidKeySpecException, InvalidCipherTextException, NoSuchAlgorithmException, NoSuchProviderException, IOException, InvalidAlgorithmParameterException { - byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1,seed2,seed3); - return SecurityToolbox.getInstance().rsaEncrypt(plainText,LoadSeedKeys.getInstance().generateRSAKeys(random)[0]); + byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1, seed2, seed3); + return SecurityToolbox.getInstance().rsaEncrypt(plainText, LoadSeedKeys.getInstance().generateRSAKeys(random)[0]); } public String rsaDecryptWithSeed(String cipherText, String seed1, String seed2, String seed3) throws Exception { - byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1,seed2,seed3); - return SecurityToolbox.getInstance().rsaDecrypt(cipherText,LoadSeedKeys.getInstance().generateRSAKeys(random)[1]); + byte[] random = LoadSeedKeys.getInstance().loadSeedFile(seed1, seed2, seed3); + return SecurityToolbox.getInstance().rsaDecrypt(cipherText, LoadSeedKeys.getInstance().generateRSAKeys(random)[1]); } private String filter(String origin) throws IOException { - return new String(SecurityToolbox.getInstance().base642Byte(origin)); + return origin; } } diff --git a/src/main/java/com/fr/password/tool/Runner.java b/src/main/java/com/fr/password/tool/Runner.java deleted file mode 100644 index 6e62309..0000000 --- a/src/main/java/com/fr/password/tool/Runner.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.fr.password.tool; - -import com.fr.password.tool.ui.MainFrame; - -public class Runner { - public static void main(String[] args) { - new MainFrame(); - } -} diff --git a/src/main/java/com/fr/password/tool/SecurityToolbox.java b/src/main/java/com/fr/password/tool/SecurityToolbox.java index 03779f7..6b8a7c5 100644 --- a/src/main/java/com/fr/password/tool/SecurityToolbox.java +++ b/src/main/java/com/fr/password/tool/SecurityToolbox.java @@ -145,10 +145,19 @@ public class SecurityToolbox { return plainTextData; } - public String sm3Encrypt(String plainText) { - return byteArrayToHexString(SM3Util.hash(plainText.getBytes())); + public String sm3Encrypt(String plainText) { + if (StringUtils.isEmpty(plainText)) { + return plainText; + } + try { + byte[] bytes = SM3Util.hash(plainText.getBytes("UTF-8")); + return byteArrayToHexString(bytes); + } catch (UnsupportedEncodingException exception) { + } + return plainText; } + public String byteArrayToHexString(byte[] b) { StringBuilder hs = new StringBuilder(); String tempStr; @@ -180,7 +189,7 @@ public class SecurityToolbox { return null; } - public String getPublicKey(KeyPair keyPair) { + public String getPublicKey(KeyPair keyPair) { PublicKey publicKey = keyPair.getPublic(); byte[] bytes = publicKey.getEncoded(); return byte2Base64(bytes); diff --git a/src/main/java/com/fr/password/tool/keys/CustomSM2Keys.java b/src/main/java/com/fr/password/tool/keys/CustomSM2Keys.java index 26adeda..88e2c4d 100644 --- a/src/main/java/com/fr/password/tool/keys/CustomSM2Keys.java +++ b/src/main/java/com/fr/password/tool/keys/CustomSM2Keys.java @@ -32,8 +32,18 @@ public class CustomSM2Keys { return rsa.encryptBase64(StrUtil.bytes(s, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey); } + public String encrypt(String s, String s1) throws Exception { + RSA custom = new RSA(null, s1); + return custom.encryptBase64(StrUtil.bytes(s, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey); + } + public String decrypt(String s) throws Exception { return rsa.decryptStr(s, KeyType.PrivateKey); } + + public String decrypt(String s, String s1) throws Exception { + RSA custom = new RSA(s1, null); + return custom.decryptStr(s, KeyType.PrivateKey); + } } diff --git a/src/main/java/com/fr/password/tool/keys/LoadSeedKeys.java b/src/main/java/com/fr/password/tool/keys/LoadSeedKeys.java index 8f91a6e..2eef7c2 100644 --- a/src/main/java/com/fr/password/tool/keys/LoadSeedKeys.java +++ b/src/main/java/com/fr/password/tool/keys/LoadSeedKeys.java @@ -16,6 +16,7 @@ import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; +import java.security.spec.RSAKeyGenParameterSpec; /** * desc @@ -42,11 +43,11 @@ public class LoadSeedKeys { return random; } - public String[] generateRSAKeys(byte[] random) throws NoSuchAlgorithmException { + public String[] generateRSAKeys(byte[] random) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { FineSecureRandom fineSecureRandom = new FineSecureRandom(random); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); //传入一个伪随机源,保证相同seed产生相同密钥 - keyPairGenerator.initialize(2048, fineSecureRandom); + keyPairGenerator.initialize(new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4), fineSecureRandom); KeyPair keyPair = keyPairGenerator.generateKeyPair(); String decodeKey = SecurityToolbox.getInstance().getPrivateKey(keyPair); String encodeKey = SecurityToolbox.getInstance().getPublicKey(keyPair); diff --git a/src/main/java/com/fr/password/tool/ui/FilePanel.java b/src/main/java/com/fr/password/tool/ui/FilePanel.java deleted file mode 100644 index 7597e30..0000000 --- a/src/main/java/com/fr/password/tool/ui/FilePanel.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.fr.password.tool.ui; - -import cn.hutool.core.io.FileUtil; -import cn.hutool.core.util.StrUtil; -import com.fr.password.tool.factory.SecretHelper; - -import javax.swing.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.io.File; - -public class FilePanel extends JPanel { - public final static FilePanel INSTANCE = new FilePanel(); - - private JLabel fileLabel = new JLabel(); - private JTextField filePathField = new JTextField(); - private JButton chooseFileButton = new JButton(); - - private EventHandler eventHandler = new EventHandler(); - - private FilePanel() { - chooseFileButton.addActionListener(eventHandler); - - fileLabel.setText(FrameConstants.SAVE_KEY); - add(fileLabel); - - filePathField.setColumns(28); - add(filePathField); - - chooseFileButton.setText(FrameConstants.CHOOSE); - add(chooseFileButton); - } - - public void hide() { - this.fileLabel.setVisible(false); - this.filePathField.setVisible(false); - this.chooseFileButton.setVisible(false); - } - - public void setLeftLabelText(String text) { - this.fileLabel.setText(text); - } - - public void setButtonText(String text) { - this.chooseFileButton.setText(text); - } - - public String getFilePath(){ - return filePathField.getText(); - } - - - class EventHandler implements ActionListener { - - @Override - public void actionPerformed(ActionEvent e) { - if (!TopPanel.INSTANCE.isChooseEncryption()) { - return; - } - String publicKey = StrUtil.EMPTY; - String privateKey = StrUtil.EMPTY; - JFileChooser fileChooser = new JFileChooser(); - fileChooser.setVisible(true); - // 保存密钥文件 - if (e.getSource() == chooseFileButton && fileLabel.getText().equals(FrameConstants.SAVE_KEY)) { - if (StrUtil.isEmpty(TextPanel.INSTANCE.getLeftText()) || StrUtil.isEmpty(TextPanel.INSTANCE.getRightText())) { - return; - } - fileChooser.setApproveButtonText(FrameConstants.SAVE); - fileChooser.setDialogTitle(FrameConstants.CHOOSE_SAVE_DIR); - fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - publicKey = TextPanel.INSTANCE.getLeftText(); - privateKey = TextPanel.INSTANCE.getRightText(); - String content = StrUtil.format("{}\r\n{}\r\n{}\r\n{}\r\n{}\r\n{}", - SecretHelper.BEGIN_PUBLIC_KEY, publicKey, SecretHelper.END_PUBLIC_KEY, - SecretHelper.BEGIN_PRIVATE_KEY, privateKey, SecretHelper.END_PRIVATE_KEY); - int result = fileChooser.showOpenDialog(FilePanel.INSTANCE); - if (result == JFileChooser.APPROVE_OPTION) { - String dirPath = fileChooser.getSelectedFile().getAbsolutePath(); - String keyFilePath = StrUtil.format("{}{}{}", dirPath, File.separator, SecretHelper.KEY_FILE_NAME); - filePathField.setText(keyFilePath); - FileUtil.writeUtf8String(content, keyFilePath); - } - } - - // 选择密钥文件 - if (e.getSource() == chooseFileButton && fileLabel.getText().equals(FrameConstants.CHOOSE_FILE)) { - fileChooser.setApproveButtonText(FrameConstants.CONFIRM); - fileChooser.setDialogTitle(FrameConstants.CHOOSE_KEY_DIR); - int result = fileChooser.showOpenDialog(FilePanel.INSTANCE); - if (result == JFileChooser.APPROVE_OPTION) { - String keyFilePath = fileChooser.getSelectedFile().getAbsolutePath(); - filePathField.setText(keyFilePath); - // TODO: 密钥正确性检测 - } - } - - - } - } -} diff --git a/src/main/java/com/fr/password/tool/ui/FrameConstants.java b/src/main/java/com/fr/password/tool/ui/FrameConstants.java deleted file mode 100644 index 60a6893..0000000 --- a/src/main/java/com/fr/password/tool/ui/FrameConstants.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.fr.password.tool.ui; - -/** - * 主面板的常量 - */ -public class FrameConstants { - public static final String TITLE = "加解密小工具"; - public static final String GENERATE = "密钥生成界面"; - public static final String ENCRYPT = "加密界面"; - public static final String DECRYPT = "解密界面"; - public static final String RUN_ENCRYPT = "开始加密"; - public static final String RUN_DECRYPT = "开始解密"; - public static final String CHOOSE_ENCRYPTION = "选择算法:"; - public static final String SM2 = "国密算法"; - public static final String RSA = "RSA"; - public static final String WAIT_CHOOSE = "--请选择--"; - public static final String CHOOSE_FILE = "密钥文件:"; - public static final String PUBLIC_KEY = "公钥"; - public static final String PRIVATE_KEY = "私钥"; - public static final String CHOOSE_SAVE_DIR ="选择保存的目录"; - public static final String CHOOSE_KEY_DIR ="选择密钥文件"; - public static final String CONFIRM ="确定"; - public static final String SAVE_KEY ="存储密钥:"; - public static final String SAVE ="保存"; - public static final String CHOOSE ="选择"; - public static final String GENERATE_KEY ="开始生成密钥"; - public static final String PLAIN_TEXT ="明文"; - public static final String CIPHER_TEXT ="密文"; - public static final Integer WIDTH = 500; - public static final Integer HEIGHT = 560; - public static final Integer FLOW_WIDTH = 220; - public static final Integer CHOICE_FLOW_WIDTH = 5; - public static final Integer TEXT_AREA_HEIGHT = 25; - public static final Integer TEXT_AREA_WIDTH = 20; -} diff --git a/src/main/java/com/fr/password/tool/ui/MainFrame.java b/src/main/java/com/fr/password/tool/ui/MainFrame.java deleted file mode 100644 index 5155b1a..0000000 --- a/src/main/java/com/fr/password/tool/ui/MainFrame.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.fr.password.tool.ui; - -import javax.swing.*; -import java.awt.*; - -public class MainFrame extends JFrame { - - public MainFrame() { - initInterface(); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setVisible(true); - } - - /** - * 初始化主界面的默认布局 - */ - private void initInterface() { - setTitle(FrameConstants.TITLE); - setSize(FrameConstants.WIDTH, FrameConstants.HEIGHT); - setLayout(new BorderLayout()); - setResizable(false); - // 默认是生成密钥的界面 - chooseGenerateKeys(); - } - - private void chooseGenerateKeys(){ - add(TopPanel.INSTANCE, BorderLayout.NORTH); - add(TipPanel.INSTANCE,BorderLayout.CENTER); - add(TextPanel.INSTANCE,BorderLayout.SOUTH); - } -} diff --git a/src/main/java/com/fr/password/tool/ui/TextPanel.java b/src/main/java/com/fr/password/tool/ui/TextPanel.java deleted file mode 100644 index b1fbed7..0000000 --- a/src/main/java/com/fr/password/tool/ui/TextPanel.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fr.password.tool.ui; - -import javax.swing.*; -import java.awt.*; - -public class TextPanel extends JPanel { - public final static TextPanel INSTANCE = new TextPanel(); - - private JTextArea leftTextArea = new JTextArea(); - private JTextArea rightTextArea = new JTextArea(); - - public TextPanel() { - FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT); - flowLayout.setVgap(FrameConstants.CHOICE_FLOW_WIDTH); - setLayout(flowLayout); - - JScrollPane leftJScrollPane = new JScrollPane(leftTextArea); - JScrollPane rightJScrollPane = new JScrollPane(rightTextArea); - leftTextArea.setColumns(FrameConstants.TEXT_AREA_WIDTH); - leftTextArea.setRows(FrameConstants.TEXT_AREA_HEIGHT); - rightTextArea.setColumns(FrameConstants.TEXT_AREA_WIDTH); - rightTextArea.setRows(FrameConstants.TEXT_AREA_HEIGHT); - - leftJScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); - rightJScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); - - add(leftJScrollPane); - add(rightJScrollPane); - } - - public void setLeftText(String text) { - this.leftTextArea.setText(text); - } - - public void setRightText(String text) { - this.rightTextArea.setText(text); - } - - public String getLeftText() { - return leftTextArea.getText(); - } - - public String getRightText() { - return rightTextArea.getText(); - } -} diff --git a/src/main/java/com/fr/password/tool/ui/TipPanel.java b/src/main/java/com/fr/password/tool/ui/TipPanel.java deleted file mode 100644 index 3c656d8..0000000 --- a/src/main/java/com/fr/password/tool/ui/TipPanel.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.fr.password.tool.ui; - -import javax.swing.*; -import java.awt.*; - -public class TipPanel extends JPanel { - public final static TipPanel INSTANCE = new TipPanel(); - - private JLabel leftLabel = new JLabel(); - private JLabel rightLabel = new JLabel(); - - - private TipPanel() { - FlowLayout flowLayout = new FlowLayout(); - flowLayout.setHgap(FrameConstants.FLOW_WIDTH); - setLayout(flowLayout); - add(FilePanel.INSTANCE); - - this.leftLabel.setText(FrameConstants.PUBLIC_KEY); - this.rightLabel.setText(FrameConstants.PRIVATE_KEY); - add(leftLabel); - add(rightLabel); - } - - public void setLeftLabel(String text) { - this.leftLabel.setText(text); - } - - public void setRightLabel(String text) { - this.rightLabel.setText(text); - } -} diff --git a/src/main/java/com/fr/password/tool/ui/TopPanel.java b/src/main/java/com/fr/password/tool/ui/TopPanel.java deleted file mode 100644 index 5d4e2e4..0000000 --- a/src/main/java/com/fr/password/tool/ui/TopPanel.java +++ /dev/null @@ -1,183 +0,0 @@ -package com.fr.password.tool.ui; - -import cn.hutool.core.util.StrUtil; -import com.fr.password.tool.factory.SecretHelper; -import com.fr.password.tool.keys.RSAKeysHandler; -import com.fr.password.tool.keys.SM2KeysHandler; -import com.fr.password.tool.util.EncodeUtil; -import com.fr.password.tool.util.rsa.RSAUtil; -import com.fr.password.tool.util.smx.SM2KeyPair; -import com.fr.password.tool.util.smx.SM2Util; -import org.bouncycastle.crypto.params.ECPrivateKeyParameters; -import org.bouncycastle.crypto.params.ECPublicKeyParameters; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.security.KeyPair; -import java.security.NoSuchAlgorithmException; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.util.Map; - -public class TopPanel extends JPanel { - public final static TopPanel INSTANCE = new TopPanel(); - - private JButton runButton = new JButton(); - private JComboBox chooseComboBox = new JComboBox(); - private JComboBox interfaceComboBox = new JComboBox(); - - private EventHandler eventHandler = new EventHandler(); - - private TopPanel() { - FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT); - flowLayout.setVgap(FrameConstants.CHOICE_FLOW_WIDTH); - flowLayout.setHgap(FrameConstants.CHOICE_FLOW_WIDTH); - setLayout(flowLayout); - - interfaceComboBox.addItem(FrameConstants.GENERATE); - interfaceComboBox.addItem(FrameConstants.ENCRYPT); - interfaceComboBox.addItem(FrameConstants.DECRYPT); - interfaceComboBox.addActionListener(eventHandler); - add(interfaceComboBox); - - JLabel chooseLabel = new JLabel(FrameConstants.CHOOSE_ENCRYPTION); - chooseComboBox.addItem(FrameConstants.WAIT_CHOOSE); - chooseComboBox.addItem(FrameConstants.RSA); - chooseComboBox.addItem(FrameConstants.SM2); - add(chooseLabel); - add(chooseComboBox); - - runButton.setText(FrameConstants.GENERATE_KEY); - runButton.addActionListener(eventHandler); - add(runButton); - - setBackground(Color.lightGray); - } - - public void setRunButtonText(String text) { - this.runButton.setText(text); - } - - public boolean isChooseEncryption() { - return chooseComboBox.getSelectedIndex() != 0; - } - - public String getChooseEncryption() { - return (String) chooseComboBox.getSelectedItem(); - } - - class EventHandler implements ActionListener { - - @Override - public void actionPerformed(ActionEvent e) { - // 生成密钥界面 (使用默认的页面) - if (interfaceComboBox.getSelectedIndex() == 0) { - runButton.setText(FrameConstants.GENERATE_KEY); - FilePanel.INSTANCE.setLeftLabelText(FrameConstants.SAVE_KEY); - FilePanel.INSTANCE.setButtonText(FrameConstants.CHOOSE); - TipPanel.INSTANCE.setLeftLabel(FrameConstants.PUBLIC_KEY); - TipPanel.INSTANCE.setRightLabel(FrameConstants.PRIVATE_KEY); - if (e.getSource() != runButton || !isChooseEncryption()) { - return; - } - // 获取加密方式 - String encryptionType = (String) chooseComboBox.getSelectedItem(); - String publicKeyText = StrUtil.EMPTY; - String privateKeyText = StrUtil.EMPTY; - if (encryptionType.equals(FrameConstants.RSA)) { - try { - KeyPair keyPair = RSAKeysHandler.getInstance().generate(); - publicKeyText = RSAKeysHandler.getInstance().publicKey2String(keyPair.getPublic()); - privateKeyText = RSAKeysHandler.getInstance().privateKey2String(keyPair.getPrivate()); - } catch (NoSuchAlgorithmException noSuchAlgorithmException) { - noSuchAlgorithmException.printStackTrace(); - } - } - if (encryptionType.equals(FrameConstants.SM2)) { - try { - SM2KeyPair sm2KeyPair = SM2KeysHandler.getInstance().generate(); - publicKeyText = SM2KeysHandler.getInstance().publicKey2String(sm2KeyPair.getPublicKeyParameters(), sm2KeyPair.getPrivateKeyParameters()); - privateKeyText = SM2KeysHandler.getInstance().privateKey2String(sm2KeyPair.getPrivateKeyParameters()); - } catch (Exception exception) { - exception.printStackTrace(); - } - } - TextPanel.INSTANCE.setLeftText(publicKeyText); - TextPanel.INSTANCE.setRightText(privateKeyText); - } else { - FilePanel.INSTANCE.setLeftLabelText(FrameConstants.CHOOSE_FILE); - FilePanel.INSTANCE.setButtonText(FrameConstants.CHOOSE); - if (interfaceComboBox.getSelectedIndex() == 1) { - TipPanel.INSTANCE.setLeftLabel(FrameConstants.PLAIN_TEXT); - TipPanel.INSTANCE.setRightLabel(FrameConstants.CIPHER_TEXT); - runButton.setText(FrameConstants.RUN_ENCRYPT); - } else { - TipPanel.INSTANCE.setLeftLabel(FrameConstants.CIPHER_TEXT); - TipPanel.INSTANCE.setRightLabel(FrameConstants.PLAIN_TEXT); - runButton.setText(FrameConstants.RUN_DECRYPT); - } - if (e.getSource() == interfaceComboBox) { - TextPanel.INSTANCE.setLeftText(StrUtil.EMPTY); - TextPanel.INSTANCE.setRightText(StrUtil.EMPTY); - } - if (e.getSource() != runButton || !isChooseEncryption() || StrUtil.isEmpty(FilePanel.INSTANCE.getFilePath())) { - return; - } - String encryptionType = (String) chooseComboBox.getSelectedItem(); - String publicKeyText = StrUtil.EMPTY; - String privateKeyText = StrUtil.EMPTY; - Map keyMap = SecretHelper.getKeyPair(FilePanel.INSTANCE.getFilePath()); - publicKeyText = keyMap.get(SecretHelper.PUBLIC_KEY); - privateKeyText = keyMap.get(SecretHelper.PRIVATE_KEY); - if (StrUtil.isEmpty(publicKeyText) || StrUtil.isEmpty(privateKeyText)) { - return; - } - // 加密界面 - if (interfaceComboBox.getSelectedIndex() == 1) { - String plainText = TextPanel.INSTANCE.getLeftText(); - if (StrUtil.isEmpty(plainText)) { - return; - } - if (encryptionType.equals(FrameConstants.RSA)) { - PublicKey publicKey = RSAKeysHandler.getInstance().string2PublicKey(publicKeyText); - TextPanel.INSTANCE.setRightText(EncodeUtil.byte2Base64(RSAUtil.encrypt(plainText.getBytes(), publicKey))); - } - if (encryptionType.equals(FrameConstants.SM2)) { - try { - ECPublicKeyParameters publicKeyParameters = SM2KeysHandler.getInstance().string2PublicKey(publicKeyText, privateKeyText); - TextPanel.INSTANCE.setRightText(EncodeUtil.byte2Base64(SM2Util.encrypt(publicKeyParameters, plainText.getBytes()))); - } catch (Exception exception) { - return; - } - } - } else { // 解密界面 - String cipherText = TextPanel.INSTANCE.getLeftText(); - if (StrUtil.isEmpty(cipherText)) { - return; - } - if (encryptionType.equals(FrameConstants.RSA)) { // RSA 解密 - try { - PrivateKey privateKey = RSAKeysHandler.getInstance().string2PrivateKey(privateKeyText); - TextPanel.INSTANCE.setLeftText(new String(RSAUtil.decrypt(EncodeUtil.base642Byte(cipherText), privateKey), StandardCharsets.UTF_8)); - } catch (IOException ioException) { - return; - } - - } else { // SM2 解密 - try { - ECPrivateKeyParameters privateKeyParameters = SM2KeysHandler.getInstance().string2PrivateKey(privateKeyText); - TextPanel.INSTANCE.setRightText(new String(SM2Util.decrypt(privateKeyParameters, EncodeUtil.base642Byte(cipherText)), StandardCharsets.UTF_8)); - } catch (Exception exception) { - return; - } - } - } - } - } - } -} - diff --git a/src/main/java/com/fr/password/tool/util/EncodeUtil.java b/src/main/java/com/fr/password/tool/util/EncodeUtil.java index f2a3a93..85dbdea 100644 --- a/src/main/java/com/fr/password/tool/util/EncodeUtil.java +++ b/src/main/java/com/fr/password/tool/util/EncodeUtil.java @@ -1,10 +1,15 @@ package com.fr.password.tool.util; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.fr.password.bean.RequestBean; import org.bouncycastle.pqc.math.linearalgebra.ByteUtils; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; /** * 编码相关的工具类 @@ -23,4 +28,12 @@ public class EncodeUtil { public static String byte2HexString(byte[] bytes) { return ByteUtils.toHexString(bytes); } + + public static RequestBean parseBody(String content) throws UnsupportedEncodingException { + String originContent = URLDecoder.decode(content, "UTF8"); + JSONObject jsonObject = JSONUtil.parseObj(originContent); + String key = (String) jsonObject.get("key"); + String text = (String) jsonObject.get("text"); + return new RequestBean(key, text); + } } diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index adc6c84..2f575b9 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -90,6 +90,9 @@ onclick="sm2CustomEncrypt()"> +

diff --git a/src/main/resources/static/index.js b/src/main/resources/static/index.js index 7507553..0842cbb 100644 --- a/src/main/resources/static/index.js +++ b/src/main/resources/static/index.js @@ -10,8 +10,10 @@ function sm2Encrypt() { reader.onload = function (event) { key = event.target.result; $.ajax({ - url: `/storage/encrypt/sm2?plainText=${Base64.encode(sm2Text)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/storage/encrypt/sm2`, + data:JSON.stringify({"text":sm2Text,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#sm2-textarea').val(data) @@ -20,8 +22,10 @@ function sm2Encrypt() { } } else { $.ajax({ - url: `/storage/encrypt/sm2?plainText=${Base64.encode(sm2Text)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/storage/encrypt/sm2`, + data:JSON.stringify({"text":sm2Text,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#sm2-textarea').val(data) @@ -41,8 +45,10 @@ function sm2Decrypt() { reader.onload = function (event) { key = event.target.result; $.ajax({ - url: `/storage/decrypt/sm2?cipherText=${Base64.encode(sm2Text)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/storage/decrypt/sm2`, + data:JSON.stringify({"text":sm2Text,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#sm2-textarea').val(data) @@ -51,8 +57,10 @@ function sm2Decrypt() { } } else { $.ajax({ - url: `/storage/decrypt/sm2?cipherText=${Base64.encode(sm2Text)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/storage/decrypt/sm2`, + data:JSON.stringify({"text":sm2Text,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#sm2-textarea').val(data) @@ -62,6 +70,78 @@ function sm2Decrypt() { } +// 自定义的加密方式 +function sm2CustomEncrypt() { + const sm2Text = $('#sm2-textarea-custom').val(); + console.log(123) + const files = $('#sm2-key-custom').prop('files'); + var key = undefined; + if (files.length > 0) { + var reader = new FileReader(); + reader.readAsText(files[0], "UTF-8"); + reader.onload = function (event) { + key = event.target.result; + $.ajax({ + url: `/storage/encrypt/sm2/custom`, + data: {'text':Base64.encode(sm2Text)}, + data:JSON.stringify({"text":sm2Text,"key":key}), + type: "POST", + contentType: "application/json", + cache: false, + success: function (data) { + $('#sm2-textarea-custom').val(data) + }, + }) + } + } else { + $.ajax({ + url: `/storage/encrypt/sm2/custom`, + data:JSON.stringify({"text":sm2Text,"key":key}), + type: "POST", + contentType: "application/json", + cache: false, + success: function (data) { + $('#sm2-textarea-custom').val(data) + }, + }) + } +} + +function sm2CustomDecrypt() { + const sm2Text = $('#sm2-textarea-custom').val(); + const files = $('#sm2-key-custom').prop('files'); + var key = undefined; + if (files.length > 0) { + var reader = new FileReader(); + reader.readAsText(files[0], "UTF-8"); + reader.onload = function (event) { + key = event.target.result; + $.ajax({ + url: `/storage/decrypt/sm2/custom`, + data:JSON.stringify({"text":sm2Text,"key":key}), + type: "POST", + contentType: "application/json", + cache: false, + success: function (data) { + $('#sm2-textarea-custom').val(data) + }, + }) + } + } else { + $.ajax({ + url: `/storage/decrypt/sm2/custom`, + data:JSON.stringify({"text":sm2Text,"key":key}), + type: "POST", + contentType: "application/json", + cache: false, + success: function (data) { + $('#sm2-textarea-custom').val(data) + }, + }) + } +} + + function sm2EncryptWithSeed() { const sm2Text = $('#sm2-textarea-with-seed').val(); const files = $('#sm2-key-with-seed').prop('files'); @@ -86,8 +166,10 @@ function sm2EncryptWithSeed() { console.log(seed2); console.log(seed3); $.ajax({ - url: `/storage/encrypt/sm2/seed?plainText=${sm2Text}&seed1=${Base64.encode(seed1)}&seed2=${Base64.encode(seed2)}&seed3=${Base64.encode(seed3)}`, - type: "GET", + url: `/storage/encrypt/sm2/seed`, + data:JSON.stringify({"text":sm2Text,"seed1":seed1,"seed2":seed2,"seed3":seed3}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#sm2-textarea-with-seed').val(data) @@ -124,8 +206,10 @@ function sm2DecryptWithSeed() { console.log(seed2); console.log(seed3); $.ajax({ - url: `/storage/decrypt/sm2/seed?cipherText=${Base64.encode(sm2Text)}&seed1=${Base64.encode(seed1)}&seed2=${Base64.encode(seed2)}&seed3=${Base64.encode(seed3)}`, - type: "GET", + url: `/storage/decrypt/sm2/seed`, + data:JSON.stringify({"text":sm2Text,"seed1":seed1,"seed2":seed2,"seed3":seed3}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#sm2-textarea-with-seed').val(data) @@ -150,8 +234,10 @@ function rsaEncrypt() { reader.onload = function (event) { key = event.target.result; $.ajax({ - url: `/storage/encrypt/rsa?plainText=${Base64.encode(rsaText)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/storage/encrypt/rsa`, + data:JSON.stringify({"text":rsaText,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#rsa-textarea').val(data) @@ -160,8 +246,10 @@ function rsaEncrypt() { } } else { $.ajax({ - url: `/storage/encrypt/rsa?plainText=${Base64.encode(rsaText)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/storage/encrypt/rsa`, + data:JSON.stringify({"text":rsaText,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#rsa-textarea').val(data) @@ -181,8 +269,10 @@ function rsaDecrypt() { reader.onload = function (event) { key = event.target.result; $.ajax({ - url: `/storage/decrypt/rsa?cipherText=${Base64.encode(rsaText)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/storage/decrypt/rsa`, + data:JSON.stringify({"text":rsaText,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#rsa-textarea').val(data) @@ -191,8 +281,10 @@ function rsaDecrypt() { } } else { $.ajax({ - url: `/storage/decrypt/rsa?cipherText=${Base64.encode(rsaText)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/storage/decrypt/rsa`, + data:JSON.stringify({"text":rsaText,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#rsa-textarea').val(data) @@ -227,8 +319,10 @@ function rsaEncryptWithSeed() { console.log(seed2); console.log(seed3); $.ajax({ - url: `/storage/encrypt/rsa/seed?plainText=${Base64.encode(rsaText)}&seed1=${Base64.encode(seed1)}&seed2=${Base64.encode(seed2)}&seed3=${Base64.encode(seed3)}`, - type: "GET", + url: `/storage/encrypt/rsa/seed`, + data:JSON.stringify({"text":rsaText,"seed1":seed1,"seed2":seed2,"seed3":seed3}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#rsa-textarea-with-seed').val(data) @@ -265,8 +359,10 @@ function rsaDecryptWithSeed() { console.log(seed2); console.log(seed3); $.ajax({ - url: `/storage/decrypt/rsa/seed?cipherText=${Base64.encode(rsaText)}&seed1=${Base64.encode(seed1)}&seed2=${Base64.encode(seed2)}&seed3=${Base64.encode(seed3)}`, - type: "GET", + url: `/storage/decrypt/rsa/seed`, + data:JSON.stringify({"text":rsaText,"seed1":seed1,"seed2":seed2,"seed3":seed3}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#rsa-textarea-with-seed').val(data) @@ -285,8 +381,10 @@ function aesEncrypt() { console.log(key) $.ajax({ - url: `/transmission/encrypt/aes?plainText=${Base64.encode(aesText)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/transmission/encrypt/aes`, + data:JSON.stringify({"text":aesText,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#aes-textarea').val(data) @@ -300,8 +398,10 @@ function aesDecrypt() { const key = $('#aes-key').val() $.ajax({ - url: `/transmission/decrypt/aes?cipherText=${Base64.encode(aesText)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/transmission/decrypt/aes`, + data:JSON.stringify({"text":aesText,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#aes-textarea').val(data) @@ -316,8 +416,10 @@ function sm4Encrypt() { console.log(key) $.ajax({ - url: `/transmission/encrypt/sm4?plainText=${Base64.encode(sm4Text)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/transmission/encrypt/sm4`, + data:JSON.stringify({"text":sm4Text,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#sm4-textarea').val(data) @@ -331,8 +433,10 @@ function sm4Decrypt() { const key = $('#sm4-key').val() $.ajax({ - url: `/transmission/decrypt/sm4?cipherText=${Base64.encode(sm4Text)}&key=${Base64.encode(key)}`, - type: "GET", + url: `/transmission/decrypt/sm4`, + data:JSON.stringify({"text":sm4Text,"key":key}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#sm4-textarea').val(data) @@ -340,36 +444,14 @@ function sm4Decrypt() { }) } -// 自定义的加密方式 -function sm2CustomEncrypt() { - const sm2Text = $('#sm2-textarea-custom').val(); - $.ajax({ - url: `/storage/encrypt/sm2/custom?plainText=${Base64.encode(sm2Text)}`, - type: "GET", - cache: false, - success: function (data) { - $('#sm2-textarea-custom').val(data) - }, - }) -} - -function sm2CustomDecrypt() { - const sm2Text = $('#sm2-textarea-custom').val(); - $.ajax({ - url: `/storage/decrypt/sm2/custom?cipherText=${Base64.encode(sm2Text)}`, - type: "GET", - cache: false, - success: function (data) { - $('#sm2-textarea-custom').val(data) - }, - }) -} function sha() { const sm2Text = $('#sha-textarea').val(); $.ajax({ - url: `/password/encrypt/sha?plainText=${Base64.encode(sm2Text)}`, - type: "GET", + url: `/password/encrypt/sha`, + data:JSON.stringify({"text":sm2Text}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#sha-textarea').val(data) @@ -380,8 +462,10 @@ function sha() { function sm3() { const sm2Text = $('#sm3-textarea').val(); $.ajax({ - url: `/password/encrypt/sm3?plainText=${Base64.encode(sm2Text)}`, - type: "GET", + url: `/password/encrypt/sm3`, + data:JSON.stringify({"text":sm2Text}), + type: "POST", + contentType: "application/json", cache: false, success: function (data) { $('#sm3-textarea').val(data)