Skip to content

Commit

Permalink
Merge pull request #13 from shiker1996/feature-new-view
Browse files Browse the repository at this point in the history
1.2.0 new comparison view
  • Loading branch information
shiker1996 authored Mar 21, 2024
2 parents a8eaf99 + 7c48c9e commit 9bcba36
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 46 deletions.
5 changes: 4 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ The ENC decryption tool, which can decrypt configuration files using <a href="ht
2. Feature 2 - encrypt

![demo2](img/encrypt.gif)
3. Supported algorithm:
3. Feature 3 - more style comparison!

![style.png](img/style.png)
4. Supported algorithm:
- AES/ECB/PKCS5Padding
- AES/ECB/NoPadding
- AES/CBC/PKCS5Padding
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "tech.shiker"
version = "1.1.1"
version = "1.2.0"

repositories {
maven { url 'https://maven.aliyun.com/repository/central/' }
Expand Down
Binary file added img/style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 14 additions & 10 deletions src/main/java/tech/shiker/common/SecurityMethod.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package tech.shiker.common;

import com.google.common.collect.Lists;
import tech.shiker.security.AesCBCNoPaddingSecurityInstance;
import tech.shiker.security.AesCBCPkcs5PaddingSecurityInstance;
import tech.shiker.security.AesECBPkcs5PaddingSecurityInstance;
import tech.shiker.security.AesECBNoPaddingSecurityInstance;
import tech.shiker.security.AesECBPkcs5PaddingSecurityInstance;
import tech.shiker.security.DesCBCNoPaddingSecurityInstance;
import tech.shiker.security.DesCBCPkcs5PaddingSecurityInstance;
import tech.shiker.security.DesECBNoPaddingSecurityInstance;
import tech.shiker.security.DesECBPkcs5PaddingSecurityInstance;
import tech.shiker.security.SecurityInstance;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.Map;

public enum SecurityMethod {

Expand Down Expand Up @@ -60,11 +60,15 @@ public static SecurityMethod decryptMethod(String decryptType, String decryptInf
return null;
}

public static Set<String> getAllDecryptType(){
return Arrays.stream(SecurityMethod.values()).map(SecurityMethod::decryptType).collect(Collectors.toSet());
}

public static List<String> getAllDecryptInformation(){
return Arrays.stream(SecurityMethod.values()).map(SecurityMethod::decryptInformation).collect(Collectors.toList());
public static Map<String, List<String>> getType2Information(){
Map<String, List<String>> result = new HashMap<>();
for (SecurityMethod value : SecurityMethod.values()) {
if(result.containsKey(value.decryptType())){
result.get(value.decryptType()).add(value.decryptInformation());
}else{
result.put(value.decryptType(), Lists.newArrayList(value.decryptInformation()));
}
}
return result;
}
}
1 change: 1 addition & 0 deletions src/main/java/tech/shiker/config/EncSettingState.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class EncSettingState implements PersistentStateComponent<EncSettin
public String decryptedKey = "hi World Decrypt";
public String decryptedInformation = "AES/ECB/PKCS5Padding";
public String decryptedVi = null;
public String isHtmlView = "On";

public static EncSettingState getInstance() {
return ApplicationManager.getApplication().getService(EncSettingState.class);
Expand Down
63 changes: 54 additions & 9 deletions src/main/java/tech/shiker/config/EncToolComponent.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package tech.shiker.config;

import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.JBColor;
import com.intellij.ui.TextIcon;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBRadioButton;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder;
import org.jetbrains.annotations.NotNull;
import tech.shiker.common.SecurityMethod;

import javax.swing.*;
import java.awt.*;

public class EncToolComponent {

Expand All @@ -19,20 +23,53 @@ public class EncToolComponent {
private final ComboBox<String> decryptedInformationBox = new ComboBox<>();
private String decryptedInformation = "AES/ECB/PKCS5Padding";
private final JBTextField decryptedIv = new JBTextField();
private final JRadioButton isHtmlView = new JRadioButton("On"); ;

public EncToolComponent() {
SecurityMethod.getAllDecryptType().forEach(decryptedTypeBox::addItem);
SecurityMethod.getAllDecryptInformation().forEach(decryptedInformationBox::addItem);decryptedTypeBox.addActionListener(e -> decryptedType = (String) decryptedTypeBox.getSelectedItem());
decryptedInformationBox.addActionListener(e -> decryptedInformation = (String) decryptedInformationBox.getSelectedItem());
JLabel decryptedIvLabel = new JBLabel("Enter decrypted iv:");
SecurityMethod.getType2Information().keySet().forEach(decryptedTypeBox::addItem);
decryptedTypeBox.addActionListener(e -> {
decryptedType = (String) decryptedTypeBox.getSelectedItem();
updateInformationComboBox(decryptedType, decryptedInformationBox);
});
decryptedInformationBox.addActionListener(e -> {
decryptedInformation = (String) decryptedInformationBox.getSelectedItem();
updateDecryptedVi(decryptedIv, decryptedIvLabel, decryptedInformation);
});
isHtmlView.addActionListener(e -> {
if (isHtmlView.isSelected()) {
isHtmlView.setText("On");
} else {
isHtmlView.setText("Off");
}
});
myMainPanel = FormBuilder.createFormBuilder()
.addLabeledComponent(new JBLabel("Html compare view:"), isHtmlView, 1, false)
.addLabeledComponent(new JBLabel("Enter decrypted type: "), decryptedTypeBox, 1, false)
.addLabeledComponent(new JBLabel("Enter decrypted key:"), decryptedKeyText, 1, false)
.addLabeledComponent(new JBLabel("Enter decrypted information:"), decryptedInformationBox, 1, false)
.addLabeledComponent(new JBLabel("Enter decrypted iv:"), decryptedIv, 1,false)
.addLabeledComponent(new JBLabel("Enter decrypted key:"), decryptedKeyText, 1, false)
.addLabeledComponent(decryptedIvLabel, decryptedIv, 1, false)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}

private void updateDecryptedVi(JBTextField decryptedIv, JLabel decryptedIvLabel, String decryptedInformation) {
// 根据解密信息的不同,更新IV的可见性
if (decryptedInformation != null && decryptedInformation.contains("CBC")) {
decryptedIv.setVisible(true);
decryptedIvLabel.setVisible(true);
}else{
decryptedIv.setVisible(false);
decryptedIvLabel.setVisible(false);
}
}

// 更新城市ComboBox的内容
private void updateInformationComboBox(String decryptedType, ComboBox<String> decryptedInformationBox) {
String[] cities = SecurityMethod.getType2Information().get(decryptedType).toArray(new String[0]);
decryptedInformationBox.setModel(new DefaultComboBoxModel<>(cities));
}

public JPanel getPanel() {
return myMainPanel;
}
Expand Down Expand Up @@ -61,19 +98,27 @@ public String getDecryptedIv() {
return decryptedIv.getText();
}

public void setDecryptedTypeText(@NotNull String decryptedType){
public String getIsHtmlView() {
return isHtmlView.getText();
}

public void setDecryptedTypeText(@NotNull String decryptedType) {
decryptedTypeBox.setSelectedItem(decryptedType);
}

public void setDecryptedKeyText(@NotNull String decryptedKey){
public void setDecryptedKeyText(@NotNull String decryptedKey) {
decryptedKeyText.setText(decryptedKey);
}

public void setDecryptedInformation(@NotNull String decryptedInformationStr){
public void setDecryptedInformation(@NotNull String decryptedInformationStr) {
decryptedInformationBox.setSelectedItem(decryptedInformationStr);
}

public void setDecryptedIv(@NotNull String decryptedIvStr){
public void setDecryptedIv(@NotNull String decryptedIvStr) {
decryptedIv.setText(decryptedIvStr);
}

public void setIsHtmlView(@NotNull String isHtmlViewStr){
isHtmlView.setText(isHtmlViewStr);
}
}
3 changes: 3 additions & 0 deletions src/main/java/tech/shiker/config/EncToolConfigurable.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public boolean isModified() {
modified |= !mySettingsComponent.getDecryptedKey().equals(settings.decryptedKey);
modified |= !mySettingsComponent.getDecryptedInformation().equals(settings.decryptedInformation);
modified |= !mySettingsComponent.getDecryptedIv().equals(settings.decryptedVi);
modified |= !mySettingsComponent.getIsHtmlView().equals(settings.isHtmlView);
return modified;
}

Expand All @@ -48,6 +49,7 @@ public void apply() {
settings.decryptedKey = mySettingsComponent.getDecryptedKey();
settings.decryptedInformation = mySettingsComponent.getDecryptedInformation();
settings.decryptedVi = mySettingsComponent.getDecryptedIv();
settings.isHtmlView = mySettingsComponent.getIsHtmlView();
}

@Override
Expand All @@ -57,6 +59,7 @@ public void reset() {
mySettingsComponent.setDecryptedTypeText(settings.decryptedType);
mySettingsComponent.setDecryptedInformation(settings.decryptedInformation);
mySettingsComponent.setDecryptedIv(settings.decryptedVi);
mySettingsComponent.setIsHtmlView(settings.isHtmlView);
}

@Override
Expand Down
45 changes: 44 additions & 1 deletion src/main/java/tech/shiker/enccore/DecryptEncAction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package tech.shiker.enccore;

import com.intellij.diff.DiffContentFactory;
import com.intellij.diff.DiffManager;
import com.intellij.diff.requests.SimpleDiffRequest;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
Expand All @@ -24,7 +27,11 @@ public void actionPerformed(AnActionEvent e) {
VirtualFile virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE);
if (virtualFile != null && isYamlOrPropertiesFile(virtualFile)) {
try {
processFile(virtualFile);
if("Off".equals(EncSettingState.getInstance().isHtmlView)){
processFileV2(project, virtualFile);
}else{
processFile(virtualFile);
}
} catch (Exception ex) {
Messages.showMessageDialog(ex.getMessage(), SecurityConstant.ENC_DECRYPT_TITLE, Messages.getInformationIcon());
}
Expand All @@ -39,6 +46,42 @@ private boolean isYamlOrPropertiesFile(VirtualFile virtualFile) {
return fileName.endsWith(".yml") || fileName.endsWith(".properties");
}

private void processFileV2(Project project, VirtualFile virtualFile) throws IOException {
String text = new String(virtualFile.contentsToByteArray());
// 使用正则表达式匹配并解密ENC()格式的字符串
Pattern pattern = Pattern.compile("ENC\\(([^)]+)\\)");
Matcher matcher = pattern.matcher(text);
StringBuilder decryptedContent = new StringBuilder();
int lastEnd = 0;
StringBuilder message = new StringBuilder();
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
String encryptedText = matcher.group(1); // 获取ENC()包裹的字符串
DecryptResult decryptResult = decrypt(encryptedText); // 解密

decryptedContent.append(text, lastEnd, start); // 将上次解密位置到本次解密位置之间的内容添加到解密后的文本中
if (!decryptResult.isDecryptError()) {
decryptedContent.append(decryptResult.decryptStr());
} else {
message.append(decryptResult.message()).append("\n");
decryptedContent.append(decryptResult.decryptStr());
}
lastEnd = end; // 更新上次解密位置
}
decryptedContent.append(text, lastEnd, text.length());
if(!message.isEmpty()){
Messages.showMessageDialog(message.toString(), SecurityConstant.ENC_DECRYPT_TITLE, Messages.getInformationIcon());
}
DiffContentFactory contentFactory = DiffContentFactory.getInstance();
SimpleDiffRequest diffRequest = new SimpleDiffRequest("ENC Compare",
contentFactory.create(text),
contentFactory.create(decryptedContent.toString()),
"Source file", "Decrypted file");

DiffManager.getInstance().showDiff(project, diffRequest);
}

private void processFile(VirtualFile virtualFile) throws Exception {
String text = new String(virtualFile.contentsToByteArray());
// 使用正则表达式匹配并解密ENC()格式的字符串
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class AesCBCNoPaddingSecurityInstance implements SecurityInstance{
@Override
public EncryptResult encrypt(String src, String key, String index) throws Exception {
if (key.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE);
}
// 判断Key是否正确
Expand All @@ -25,7 +24,6 @@ public EncryptResult encrypt(String src, String key, String index) throws Except
}
// 判断Key是否为16位
if (index.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE);
}
Cipher cipher = Cipher.getInstance(SecurityMethod.AES_CBC_NO_PADDING.decryptInformation());
Expand All @@ -40,7 +38,6 @@ public EncryptResult encrypt(String src, String key, String index) throws Except
public DecryptResult decrypt(String src, String key, String index) throws Exception {
// 判断Key是否为16位
if (key.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE);
}
// 判断Key是否正确
Expand All @@ -49,7 +46,6 @@ public DecryptResult decrypt(String src, String key, String index) throws Except
}
// 判断Key是否为16位
if (index.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE);
}
Cipher cipher = Cipher.getInstance(SecurityMethod.AES_CBC_NO_PADDING.decryptInformation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class AesCBCPkcs5PaddingSecurityInstance implements SecurityInstance {
@Override
public EncryptResult encrypt(String src, String key, String index) throws Exception {
if (key.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE);
}
// 判断Key是否正确
Expand All @@ -25,7 +24,6 @@ public EncryptResult encrypt(String src, String key, String index) throws Except
}
// 判断Key是否为16位
if (index.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE);
}
Cipher cipher = Cipher.getInstance(SecurityMethod.AES_CBC_PKCS5_PADDING.decryptInformation());
Expand All @@ -40,7 +38,6 @@ public EncryptResult encrypt(String src, String key, String index) throws Except
public DecryptResult decrypt(String src, String key, String index) throws Exception {
// 判断Key是否为16位
if (key.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE);
}
// 判断Key是否正确
Expand All @@ -49,7 +46,6 @@ public DecryptResult decrypt(String src, String key, String index) throws Except
}
// 判断Key是否为16位
if (index.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE);
}
Cipher cipher = Cipher.getInstance(SecurityMethod.AES_CBC_PKCS5_PADDING.decryptInformation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class AesECBNoPaddingSecurityInstance implements SecurityInstance{
public EncryptResult encrypt(String src, String key) throws Exception {
// 判断Key是否为16位
if (key.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE);
}
Cipher cipher = Cipher.getInstance(SecurityMethod.AES_ECB_NO_PADDING.decryptInformation());
Expand All @@ -35,7 +34,6 @@ public EncryptResult encrypt(String src, String key, String index) throws Except
public DecryptResult decrypt(String src, String key) throws Exception {
// 判断Key是否为16位
if (key.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE);
}
Cipher cipher = Cipher.getInstance(SecurityMethod.AES_ECB_NO_PADDING.decryptInformation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class AesECBPkcs5PaddingSecurityInstance implements SecurityInstance {
public EncryptResult encrypt(String src, String encryptedKey) throws Exception {
// 判断Key是否为16位
if (encryptedKey.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE);
}
Cipher cipher = Cipher.getInstance(SecurityMethod.AES_ECB_PKCS5_PADDING.decryptInformation());
Expand All @@ -35,7 +34,6 @@ public EncryptResult encrypt(String src, String key, String index) throws Except
public DecryptResult decrypt(String src, String decryptedKey) throws Exception{
// 判断Key是否为16位
if (decryptedKey.length() != 16) {
Messages.showInfoMessage(SecurityConstant.KEY_INVALID_MESSAGE, SecurityConstant.ENC_DECRYPT_TITLE);
return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE);
}
byte[] raw = decryptedKey.getBytes(StandardCharsets.UTF_8);
Expand Down
Loading

0 comments on commit 9bcba36

Please sign in to comment.