From 05cf0f3fea042cfa7b9ad1f2a9ff6046664b0d9a Mon Sep 17 00:00:00 2001 From: shiker <32234876+shiker1996@users.noreply.github.com> Date: Sun, 31 Mar 2024 18:30:09 +0800 Subject: [PATCH] improve plugin size and settings option Signed-off-by: shker <1159181016@qq.com> --- build.gradle | 2 +- .../{security => common}/NoPaddingUtil.java | 2 +- .../tech/shiker/common/SecurityConstant.java | 4 +- .../java/tech/shiker/common/SecurityInfo.java | 80 ++++++++ .../tech/shiker/common/SecurityMethod.java | 70 +++---- .../tech/shiker/config/EncToolComponent.java | 4 +- .../AesCBCPkcs5PaddingSecurityInstance.java | 67 ------ .../AesECBNoPaddingSecurityInstance.java | 50 ----- .../AesECBPkcs5PaddingSecurityInstance.java | 51 ----- .../DesCBCNoPaddingSecurityInstance.java | 66 ------ .../DesCBCPkcs5PaddingSecurityInstance.java | 68 ------ .../DesECBNoPaddingSecurityInstance.java | 49 ----- .../DesECBPkcs5PaddingSecurityInstance.java | 50 ----- .../PBEWithSHA1AndDesSecurityInstance.java | 64 ------ ...BEWithSHA256AndAes128SecurityInstance.java | 66 ------ ...BEWithSHA512AndAes256SecurityInstance.java | 66 ------ .../CBCSecurityInstance.java} | 43 ++-- .../security/impl/ECBSecurityInstance.java | 56 +++++ .../impl/PBEWithAesSecurityInstance.java | 81 ++++++++ .../PBEWithDesSecurityInstance.java} | 55 +++-- src/test/java/test/SecurityInstanceTest.java | 193 ++++++++++++++++++ 21 files changed, 507 insertions(+), 680 deletions(-) rename src/main/java/tech/shiker/{security => common}/NoPaddingUtil.java (96%) create mode 100644 src/main/java/tech/shiker/common/SecurityInfo.java delete mode 100644 src/main/java/tech/shiker/security/AesCBCPkcs5PaddingSecurityInstance.java delete mode 100644 src/main/java/tech/shiker/security/AesECBNoPaddingSecurityInstance.java delete mode 100644 src/main/java/tech/shiker/security/AesECBPkcs5PaddingSecurityInstance.java delete mode 100644 src/main/java/tech/shiker/security/DesCBCNoPaddingSecurityInstance.java delete mode 100644 src/main/java/tech/shiker/security/DesCBCPkcs5PaddingSecurityInstance.java delete mode 100644 src/main/java/tech/shiker/security/DesECBNoPaddingSecurityInstance.java delete mode 100644 src/main/java/tech/shiker/security/DesECBPkcs5PaddingSecurityInstance.java delete mode 100644 src/main/java/tech/shiker/security/PBEWithSHA1AndDesSecurityInstance.java delete mode 100644 src/main/java/tech/shiker/security/PBEWithSHA256AndAes128SecurityInstance.java delete mode 100644 src/main/java/tech/shiker/security/PBEWithSHA512AndAes256SecurityInstance.java rename src/main/java/tech/shiker/security/{AesCBCNoPaddingSecurityInstance.java => impl/CBCSecurityInstance.java} (52%) create mode 100644 src/main/java/tech/shiker/security/impl/ECBSecurityInstance.java create mode 100644 src/main/java/tech/shiker/security/impl/PBEWithAesSecurityInstance.java rename src/main/java/tech/shiker/security/{PBEWithMD5AndDesSecurityInstance.java => impl/PBEWithDesSecurityInstance.java} (53%) create mode 100644 src/test/java/test/SecurityInstanceTest.java diff --git a/build.gradle b/build.gradle index eb14e4d..20f600e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = "tech.shiker" -version = "1.3.0" +version = "1.3.1" repositories { maven { url 'https://maven.aliyun.com/repository/central/' } diff --git a/src/main/java/tech/shiker/security/NoPaddingUtil.java b/src/main/java/tech/shiker/common/NoPaddingUtil.java similarity index 96% rename from src/main/java/tech/shiker/security/NoPaddingUtil.java rename to src/main/java/tech/shiker/common/NoPaddingUtil.java index fee4b43..ca8057c 100644 --- a/src/main/java/tech/shiker/security/NoPaddingUtil.java +++ b/src/main/java/tech/shiker/common/NoPaddingUtil.java @@ -1,4 +1,4 @@ -package tech.shiker.security; +package tech.shiker.common; import java.nio.charset.StandardCharsets; diff --git a/src/main/java/tech/shiker/common/SecurityConstant.java b/src/main/java/tech/shiker/common/SecurityConstant.java index c8d5051..483b8da 100644 --- a/src/main/java/tech/shiker/common/SecurityConstant.java +++ b/src/main/java/tech/shiker/common/SecurityConstant.java @@ -5,7 +5,7 @@ public class SecurityConstant { public static final String KEY_NULL_MESSAGE = "Security key can't be null"; - public static final String KEY_INVALID_MESSAGE = "Security key is invalid"; + public static final String KEY_INVALID_MESSAGE = "Security key must be %s bytes long"; public static final String SALT_INVALID_MESSAGE = "Security salt must be at least 8 bytes long"; @@ -13,7 +13,7 @@ public class SecurityConstant { public static final String IV_NULL_MESSAGE = "Security iv can't be null"; - public static final String IV_INVALID_MESSAGE = "Security iv is invalid"; + public static final String IV_INVALID_MESSAGE = "Security iv must be %s bytes long"; public static final String DECRYPT_ERR_MESSAGE = "Decrypt error:%s, err:%s"; diff --git a/src/main/java/tech/shiker/common/SecurityInfo.java b/src/main/java/tech/shiker/common/SecurityInfo.java new file mode 100644 index 0000000..5b07029 --- /dev/null +++ b/src/main/java/tech/shiker/common/SecurityInfo.java @@ -0,0 +1,80 @@ +package tech.shiker.common; + +public enum SecurityInfo { + + AES_ECB_PKCS5_PADDING("AES", "AES/ECB/PKCS5Padding", -1, 16, false), + AES_ECB_NO_PADDING("AES", "AES/ECB/NoPadding", -1, 16, true), + AES_CBC_PKCS5_PADDING("AES", "AES/CBC/PKCS5Padding", 16, 16, false), + AES_CBC_NO_PADDING("AES", "AES/CBC/NoPadding", 16, 16, true), + DES_ECB_PKCS5_PADDING("DESede", "DESede/ECB/PKCS5Padding", -1, 24, false), + DES_ECB_NO_PADDING("DESede", "DESede/ECB/NoPadding", -1, 24, true), + DES_CBC_PKCS5_PADDING("DESede", "DESede/CBC/PKCS5Padding", 8, 24, false), + DES_CBC_NO_PADDING("DESede", "DESede/CBC/NoPadding", 8, 24, true), + PBE_WITH_MD5_AND_DES("PBEWithMD5AndDES", "PBEWithMD5AndDES", -1, 8), + PBE_WITH_SHA1_AND_DES("PBEWithSHA1AndDESede", "PBEWithSHA1AndDESede", -1, 8), + PBE_WITH_SHA256_AND_AES_128("PBEWithHmacSHA256AndAES_128", "PBEWithHmacSHA256AndAES_128", 16, 8), + PBE_WITH_SHA512_AND_AES_256("PBEWithHmacSHA512AndAES_256", "PBEWithHmacSHA512AndAES_256", 16, 8), + ; + + private final String decryptType; + + private final String decryptInformation; + + private final int indexLength; + + private final int keyLength; + + private final int saltLength; + + private final int iterations; + + private final boolean handlePadding; + + SecurityInfo(String decryptType, String decryptInformation, int indexLength, int keyLength, boolean handlePadding) { + this.decryptType = decryptType; + this.decryptInformation = decryptInformation; + this.indexLength = indexLength; + this.keyLength = keyLength; + this.saltLength = -1; + this.iterations = -1; + this.handlePadding = handlePadding; + } + + SecurityInfo(String decryptType, String decryptInformation, int indexLength, int saltLength) { + this.decryptType = decryptType; + this.decryptInformation = decryptInformation; + this.indexLength = indexLength; + this.keyLength = -1; + this.saltLength = saltLength; + this.iterations = 0; + this.handlePadding = false; + } + + public String decryptType() { + return decryptType; + } + + public String decryptInformation() { + return decryptInformation; + } + + public int indexLength() { + return indexLength; + } + + public int keyLength() { + return keyLength; + } + + public int saltLength() { + return saltLength; + } + + public int iterations() { + return iterations; + } + + public boolean handlePadding() { + return handlePadding; + } +} diff --git a/src/main/java/tech/shiker/common/SecurityMethod.java b/src/main/java/tech/shiker/common/SecurityMethod.java index 0b62a1f..ea34937 100644 --- a/src/main/java/tech/shiker/common/SecurityMethod.java +++ b/src/main/java/tech/shiker/common/SecurityMethod.java @@ -1,19 +1,11 @@ package tech.shiker.common; import com.google.common.collect.Lists; -import tech.shiker.security.AesCBCNoPaddingSecurityInstance; -import tech.shiker.security.AesCBCPkcs5PaddingSecurityInstance; -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.PBEWithMD5AndDesSecurityInstance; -import tech.shiker.security.PBEWithSHA1AndDesSecurityInstance; -import tech.shiker.security.PBEWithSHA256AndAes128SecurityInstance; -import tech.shiker.security.PBEWithSHA512AndAes256SecurityInstance; import tech.shiker.security.SecurityInstance; +import tech.shiker.security.impl.CBCSecurityInstance; +import tech.shiker.security.impl.ECBSecurityInstance; +import tech.shiker.security.impl.PBEWithAesSecurityInstance; +import tech.shiker.security.impl.PBEWithDesSecurityInstance; import java.util.HashMap; import java.util.List; @@ -21,52 +13,50 @@ public enum SecurityMethod { - AES_ECB_PKCS5_PADDING("AES", "AES/ECB/PKCS5Padding", new AesECBPkcs5PaddingSecurityInstance()), - AES_ECB_NO_PADDING("AES", "AES/ECB/NoPadding", new AesECBNoPaddingSecurityInstance()), - AES_CBC_PKCS5_PADDING("AES", "AES/CBC/PKCS5Padding", new AesCBCPkcs5PaddingSecurityInstance()), - AES_CBC_NO_PADDING("AES", "AES/CBC/NoPadding", new AesCBCNoPaddingSecurityInstance()), - DES_ECB_PKCS5_PADDING("DESede", "DESede/ECB/PKCS5Padding", new DesECBPkcs5PaddingSecurityInstance()), - DES_ECB_NO_PADDING("DESede", "DESede/ECB/NoPadding", new DesECBNoPaddingSecurityInstance()), - DES_CBC_PKCS5_PADDING("DESede", "DESede/CBC/PKCS5Padding", new DesCBCPkcs5PaddingSecurityInstance()), - DES_CBC_NO_PADDING("DESede", "DESede/CBC/NoPadding", new DesCBCNoPaddingSecurityInstance()), - PBE_WITH_MD5_AND_DES("PBEWithMD5AndDES", "PBEWithMD5AndDES", new PBEWithMD5AndDesSecurityInstance()), - PBE_WITH_SHA1_AND_DES("PBEWithSHA1AndDESede", "PBEWithSHA1AndDESede", new PBEWithSHA1AndDesSecurityInstance()), - PBE_WITH_SHA256_AND_AES_128("PBEWithHmacSHA256AndAES_128", "PBEWithHmacSHA256AndAES_128", new PBEWithSHA256AndAes128SecurityInstance()), - PBE_WITH_SHA512_AND_AES_256("PBEWithHmacSHA512AndAES_256", "PBEWithHmacSHA512AndAES_256", new PBEWithSHA512AndAes256SecurityInstance()), + AES_ECB_NO_PADDING(SecurityInfo.AES_ECB_NO_PADDING, new ECBSecurityInstance(SecurityInfo.AES_ECB_NO_PADDING)), + AES_ECB_PKCS5_PADDING(SecurityInfo.AES_ECB_PKCS5_PADDING, new ECBSecurityInstance(SecurityInfo.AES_ECB_PKCS5_PADDING)), + DES_ECB_NO_PADDING(SecurityInfo.DES_ECB_NO_PADDING, new ECBSecurityInstance(SecurityInfo.DES_ECB_NO_PADDING)), + DES_ECB_PKCS5_PADDING(SecurityInfo.DES_ECB_PKCS5_PADDING, new ECBSecurityInstance(SecurityInfo.DES_ECB_PKCS5_PADDING)), + AES_CBC_NO_PADDING(SecurityInfo.AES_CBC_NO_PADDING, new CBCSecurityInstance(SecurityInfo.AES_CBC_NO_PADDING)), + AES_CBC_PKCS5_PADDING(SecurityInfo.AES_CBC_PKCS5_PADDING, new CBCSecurityInstance(SecurityInfo.AES_CBC_PKCS5_PADDING)), + DES_CBC_NO_PADDING(SecurityInfo.DES_CBC_NO_PADDING, new CBCSecurityInstance(SecurityInfo.DES_CBC_NO_PADDING)), + DES_CBC_PKCS5_PADDING(SecurityInfo.DES_CBC_PKCS5_PADDING, new CBCSecurityInstance(SecurityInfo.DES_CBC_PKCS5_PADDING)), + PBE_WITH_MD5_AND_DES(SecurityInfo.PBE_WITH_MD5_AND_DES, new PBEWithDesSecurityInstance(SecurityInfo.PBE_WITH_MD5_AND_DES)), + PBE_WITH_SHA1_AND_DES(SecurityInfo.PBE_WITH_SHA1_AND_DES, new PBEWithDesSecurityInstance(SecurityInfo.PBE_WITH_SHA1_AND_DES)), + PBE_WITH_SHA256_AND_AES_128(SecurityInfo.PBE_WITH_SHA256_AND_AES_128, new PBEWithAesSecurityInstance(SecurityInfo.PBE_WITH_SHA256_AND_AES_128)), + PBE_WITH_SHA512_AND_AES_256(SecurityInfo.PBE_WITH_SHA512_AND_AES_256, new PBEWithAesSecurityInstance(SecurityInfo.PBE_WITH_SHA512_AND_AES_256)), ; - private final String decryptType; - private final String decryptInformation; + private final SecurityInfo securityInfo; private final SecurityInstance decryptInstance; - SecurityMethod(String decryptType, String decryptInformation, SecurityInstance securityInstance) { - this.decryptType = decryptType; - this.decryptInformation = decryptInformation; + SecurityMethod(SecurityInfo securityInfo, SecurityInstance securityInstance) { + this.securityInfo = securityInfo; this.decryptInstance = securityInstance; } - public String decryptType() { - return decryptType; + public static SecurityMethod decryptMethod(String decryptType, String decryptInformation) { + for (SecurityMethod securityMethod : SecurityMethod.values()) { + if (securityMethod.securityInfo.decryptInformation().equals(decryptInformation) + && securityMethod.securityInfo.decryptType().equals(decryptType)) + return securityMethod; + } + return null; } - public String decryptInformation() { - return decryptInformation; + public String decryptType() { + return securityInfo.decryptType(); } public SecurityInstance decryptInstance() { return decryptInstance; } - public static SecurityMethod decryptMethod(String decryptType, String decryptInformation) { - for (SecurityMethod securityMethod : SecurityMethod.values()) { - if(securityMethod.decryptInformation.equals(decryptInformation) - && securityMethod.decryptType.equals(decryptType)) - return securityMethod; - } - return null; + public String decryptInformation() { + return securityInfo.decryptInformation(); } public static Map> getType2Information(){ diff --git a/src/main/java/tech/shiker/config/EncToolComponent.java b/src/main/java/tech/shiker/config/EncToolComponent.java index f8f5408..e2a7d2e 100644 --- a/src/main/java/tech/shiker/config/EncToolComponent.java +++ b/src/main/java/tech/shiker/config/EncToolComponent.java @@ -26,7 +26,7 @@ public EncToolComponent() { JLabel decryptedIvLabel = new JBLabel("Enter decrypted iv:"); JLabel decryptedSaltLabel = new JBLabel("Enter decrypted salt:"); JLabel decryptedStepLabel = new JBLabel("Enter decrypted iteration count:"); - SecurityMethod.getType2Information().keySet().forEach(decryptedTypeBox::addItem); + SecurityMethod.getType2Information().keySet().stream().sorted().forEach(decryptedTypeBox::addItem); myMainPanel = FormBuilder.createFormBuilder() .addLabeledComponent(new JBLabel("Html compare view:"), isHtmlView, 1, false) .addLabeledComponent(new JBLabel("Enter decrypted type: "), decryptedTypeBox, 1, false) @@ -92,7 +92,7 @@ private void updateDecryptedSalt(JBTextField decryptedSalt, JLabel decryptedSalt // 更新城市ComboBox的内容 private void updateInformationComboBox(String decryptedType, ComboBox decryptedInformationBox) { - String[] decryptedInformationArr = SecurityMethod.getType2Information().get(decryptedType).toArray(new String[0]); + String[] decryptedInformationArr = SecurityMethod.getType2Information().get(decryptedType).stream().sorted().toArray(String[]::new); decryptedInformationBox.setModel(new DefaultComboBoxModel<>(decryptedInformationArr)); decryptedInformationBox.setSelectedItem(decryptedInformationArr[0]); } diff --git a/src/main/java/tech/shiker/security/AesCBCPkcs5PaddingSecurityInstance.java b/src/main/java/tech/shiker/security/AesCBCPkcs5PaddingSecurityInstance.java deleted file mode 100644 index aa9b007..0000000 --- a/src/main/java/tech/shiker/security/AesCBCPkcs5PaddingSecurityInstance.java +++ /dev/null @@ -1,67 +0,0 @@ -package tech.shiker.security; - -import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; -import tech.shiker.enccore.DecryptResult; -import tech.shiker.enccore.EncryptResult; - -import javax.crypto.Cipher; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; -import java.nio.charset.StandardCharsets; -import java.util.Base64; - -public class AesCBCPkcs5PaddingSecurityInstance implements SecurityInstance { - - public EncryptResult encrypt(String src, String key, String index) throws Exception { - if (key.length() != 16) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - // 判断Key是否正确 - if (index == null) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_NULL_MESSAGE); - } - // 判断Key是否为16位 - if (index.length() != 16) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.AES_CBC_PKCS5_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), SecurityMethod.AES_CBC_PKCS5_PADDING.decryptType()); - IvParameterSpec iv = new IvParameterSpec(index.getBytes(StandardCharsets.UTF_8)); - cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); - byte[] encryptedBytes = cipher.doFinal(src.getBytes()); - return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); - } - - public DecryptResult decrypt(String src, String key, String index) throws Exception { - // 判断Key是否为16位 - if (key.length() != 16) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - // 判断Key是否正确 - if (index == null) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_NULL_MESSAGE); - } - // 判断Key是否为16位 - if (index.length() != 16) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.AES_CBC_PKCS5_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), SecurityMethod.AES_CBC_PKCS5_PADDING.decryptType()); - IvParameterSpec iv = new IvParameterSpec(index.getBytes(StandardCharsets.UTF_8)); - cipher.init(Cipher.DECRYPT_MODE, keySpec, iv); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(decryptedBytes, StandardCharsets.UTF_8), false, null); - } - - @Override - public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return encrypt(src, key, index); - } - - @Override - public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return decrypt(src, key, index); - } -} diff --git a/src/main/java/tech/shiker/security/AesECBNoPaddingSecurityInstance.java b/src/main/java/tech/shiker/security/AesECBNoPaddingSecurityInstance.java deleted file mode 100644 index 9dac0dd..0000000 --- a/src/main/java/tech/shiker/security/AesECBNoPaddingSecurityInstance.java +++ /dev/null @@ -1,50 +0,0 @@ -package tech.shiker.security; - -import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; -import tech.shiker.enccore.DecryptResult; -import tech.shiker.enccore.EncryptResult; - -import javax.crypto.Cipher; -import javax.crypto.spec.SecretKeySpec; -import java.nio.charset.StandardCharsets; -import java.util.Base64; - -public class AesECBNoPaddingSecurityInstance implements SecurityInstance{ - - public EncryptResult encrypt(String src, String key) throws Exception { - // 判断Key是否为16位 - if (key.length() != 16) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.AES_ECB_NO_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), SecurityMethod.AES_ECB_NO_PADDING.decryptType()); - cipher.init(Cipher.ENCRYPT_MODE, keySpec); - byte[] encryptedBytes = cipher.doFinal(NoPaddingUtil.addPadding(src)); - return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); - } - - @Override - public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return encrypt(src, key); - } - - - public DecryptResult decrypt(String src, String key) throws Exception { - // 判断Key是否为16位 - if (key.length() != 16) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.AES_ECB_NO_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), SecurityMethod.AES_ECB_NO_PADDING.decryptType()); - cipher.init(Cipher.DECRYPT_MODE, keySpec); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(NoPaddingUtil.removePadding(decryptedBytes), StandardCharsets.UTF_8).trim(), false, null); - } - - @Override - public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return decrypt(src, key); - } -} diff --git a/src/main/java/tech/shiker/security/AesECBPkcs5PaddingSecurityInstance.java b/src/main/java/tech/shiker/security/AesECBPkcs5PaddingSecurityInstance.java deleted file mode 100644 index 9d118c0..0000000 --- a/src/main/java/tech/shiker/security/AesECBPkcs5PaddingSecurityInstance.java +++ /dev/null @@ -1,51 +0,0 @@ -package tech.shiker.security; - -import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; -import tech.shiker.enccore.DecryptResult; -import tech.shiker.enccore.EncryptResult; - -import javax.crypto.Cipher; -import javax.crypto.spec.SecretKeySpec; -import java.nio.charset.StandardCharsets; -import java.util.Base64; - -public class AesECBPkcs5PaddingSecurityInstance implements SecurityInstance { - - public EncryptResult encrypt(String src, String encryptedKey) throws Exception { - // 判断Key是否为16位 - if (encryptedKey.length() != 16) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.AES_ECB_PKCS5_PADDING.decryptInformation()); - byte[] raw = encryptedKey.getBytes(StandardCharsets.UTF_8); - SecretKeySpec keySpec = new SecretKeySpec(raw, SecurityMethod.AES_ECB_PKCS5_PADDING.decryptType()); - cipher.init(Cipher.ENCRYPT_MODE, keySpec); - byte[] encryptedBytes = cipher.doFinal(src.getBytes()); - return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); - } - - public DecryptResult decrypt(String src, String decryptedKey) throws Exception{ - // 判断Key是否为16位 - if (decryptedKey.length() != 16) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - byte[] raw = decryptedKey.getBytes(StandardCharsets.UTF_8); - SecretKeySpec keySpec = new SecretKeySpec(raw, SecurityMethod.AES_ECB_PKCS5_PADDING.decryptType()); - Cipher cipher = Cipher.getInstance(SecurityMethod.AES_ECB_PKCS5_PADDING.decryptInformation()); - cipher.init(Cipher.DECRYPT_MODE, keySpec); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(decryptedBytes, StandardCharsets.UTF_8), false, null); - } - - @Override - public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return encrypt(src, key); - } - - @Override - public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return decrypt(src, key); - } -} diff --git a/src/main/java/tech/shiker/security/DesCBCNoPaddingSecurityInstance.java b/src/main/java/tech/shiker/security/DesCBCNoPaddingSecurityInstance.java deleted file mode 100644 index 972d23a..0000000 --- a/src/main/java/tech/shiker/security/DesCBCNoPaddingSecurityInstance.java +++ /dev/null @@ -1,66 +0,0 @@ -package tech.shiker.security; - -import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; -import tech.shiker.enccore.DecryptResult; -import tech.shiker.enccore.EncryptResult; - -import javax.crypto.Cipher; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; -import java.util.Base64; - -public class DesCBCNoPaddingSecurityInstance implements SecurityInstance{ - - public EncryptResult encrypt(String src, String key, String index) throws Exception { - // 判断Key是否为16位 - if (key.length() != 24) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - // 判断Key是否正确 - if (index == null) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_NULL_MESSAGE); - } - // 判断Key是否为24位 - if (index.length() != 8) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.DES_CBC_NO_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), SecurityMethod.DES_CBC_NO_PADDING.decryptType()); - IvParameterSpec iv = new IvParameterSpec(index.getBytes()); - cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); - byte[] encryptedBytes = cipher.doFinal(NoPaddingUtil.addPadding(src)); - return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); - } - - public DecryptResult decrypt(String src, String key, String index) throws Exception { - // 判断Key是否为16位 - if (key.length() != 24) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - if (index == null) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_NULL_MESSAGE); - } - // 判断Key是否为16位 - if (index.length() != 8) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.DES_CBC_NO_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), SecurityMethod.DES_CBC_NO_PADDING.decryptType()); - IvParameterSpec iv = new IvParameterSpec(index.getBytes()); - cipher.init(Cipher.DECRYPT_MODE, keySpec, iv); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(NoPaddingUtil.removePadding(decryptedBytes)), false, null); - } - - @Override - public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return encrypt(src, key, index); - } - - @Override - public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return decrypt(src, key, index); - } -} diff --git a/src/main/java/tech/shiker/security/DesCBCPkcs5PaddingSecurityInstance.java b/src/main/java/tech/shiker/security/DesCBCPkcs5PaddingSecurityInstance.java deleted file mode 100644 index bdc89cb..0000000 --- a/src/main/java/tech/shiker/security/DesCBCPkcs5PaddingSecurityInstance.java +++ /dev/null @@ -1,68 +0,0 @@ -package tech.shiker.security; - -import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; -import tech.shiker.enccore.DecryptResult; -import tech.shiker.enccore.EncryptResult; - -import javax.crypto.Cipher; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; -import java.nio.charset.StandardCharsets; -import java.util.Base64; - -public class DesCBCPkcs5PaddingSecurityInstance implements SecurityInstance { - - public EncryptResult encrypt(String src, String key, String index) throws Exception { - // 判断Key是否为16位 - if (key.length() != 24) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - // 判断Key是否正确 - if (index == null) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_NULL_MESSAGE); - } - // 判断Key是否为16位 - if (index.length() != 8) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.DES_CBC_PKCS5_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), SecurityMethod.DES_CBC_PKCS5_PADDING.decryptType()); - IvParameterSpec iv = new IvParameterSpec(index.getBytes(StandardCharsets.UTF_8)); - cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); - byte[] encryptedBytes = cipher.doFinal(src.getBytes()); - return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); - } - - public DecryptResult decrypt(String src, String key, String index) throws Exception { - // 判断Key是否为16位 - if (key.length() != 24) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - // 判断Key是否正确 - if (index == null) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_NULL_MESSAGE); - } - // 判断Key是否为16位 - if (index.length() != 8) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.DES_CBC_PKCS5_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), SecurityMethod.DES_CBC_PKCS5_PADDING.decryptType()); - IvParameterSpec iv = new IvParameterSpec(index.getBytes(StandardCharsets.UTF_8)); - cipher.init(Cipher.DECRYPT_MODE, keySpec, iv); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(decryptedBytes, StandardCharsets.UTF_8), false, null); - } - - @Override - public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return encrypt(src, key, index); - } - - @Override - public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return decrypt(src, key, index); - } -} diff --git a/src/main/java/tech/shiker/security/DesECBNoPaddingSecurityInstance.java b/src/main/java/tech/shiker/security/DesECBNoPaddingSecurityInstance.java deleted file mode 100644 index 05a84ac..0000000 --- a/src/main/java/tech/shiker/security/DesECBNoPaddingSecurityInstance.java +++ /dev/null @@ -1,49 +0,0 @@ -package tech.shiker.security; - -import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; -import tech.shiker.enccore.DecryptResult; -import tech.shiker.enccore.EncryptResult; - -import javax.crypto.Cipher; -import javax.crypto.spec.SecretKeySpec; -import java.nio.charset.StandardCharsets; -import java.util.Base64; - -public class DesECBNoPaddingSecurityInstance implements SecurityInstance { - - public EncryptResult encrypt(String src, String key) throws Exception { - // 判断Key是否为16位 - if (key.length() != 24) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.DES_ECB_NO_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), SecurityMethod.DES_ECB_NO_PADDING.decryptType()); - cipher.init(Cipher.ENCRYPT_MODE, keySpec); - byte[] encryptedBytes = cipher.doFinal(NoPaddingUtil.addPadding(src)); - return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); - } - - public DecryptResult decrypt(String src, String key) throws Exception { - // 判断Key是否为16位 - if (key.length() != 24) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.DES_ECB_NO_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), SecurityMethod.DES_ECB_NO_PADDING.decryptType()); - cipher.init(Cipher.DECRYPT_MODE, keySpec); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(NoPaddingUtil.removePadding(decryptedBytes), StandardCharsets.UTF_8).trim(), false, null); - } - - @Override - public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return encrypt(src, key); - } - - @Override - public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return decrypt(src, key); - } -} diff --git a/src/main/java/tech/shiker/security/DesECBPkcs5PaddingSecurityInstance.java b/src/main/java/tech/shiker/security/DesECBPkcs5PaddingSecurityInstance.java deleted file mode 100644 index ebaf303..0000000 --- a/src/main/java/tech/shiker/security/DesECBPkcs5PaddingSecurityInstance.java +++ /dev/null @@ -1,50 +0,0 @@ -package tech.shiker.security; - -import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; -import tech.shiker.enccore.DecryptResult; -import tech.shiker.enccore.EncryptResult; - -import javax.crypto.Cipher; -import javax.crypto.spec.SecretKeySpec; -import java.nio.charset.StandardCharsets; -import java.util.Base64; - -public class DesECBPkcs5PaddingSecurityInstance implements SecurityInstance { - public EncryptResult encrypt(String src, String encryptedKey) throws Exception { - // 判断Key是否为16位 - if (encryptedKey.length() != 24) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - Cipher cipher = Cipher.getInstance(SecurityMethod.DES_ECB_PKCS5_PADDING.decryptInformation()); - byte[] raw = encryptedKey.getBytes(StandardCharsets.UTF_8); - SecretKeySpec keySpec = new SecretKeySpec(raw, SecurityMethod.DES_ECB_PKCS5_PADDING.decryptType()); - cipher.init(Cipher.ENCRYPT_MODE, keySpec); - byte[] encryptedBytes = cipher.doFinal(src.getBytes()); - return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); - } - - public DecryptResult decrypt(String src, String decryptedKey) throws Exception{ - // 判断Key是否为16位 - if (decryptedKey.length() != 24) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); - } - byte[] raw = decryptedKey.getBytes(StandardCharsets.UTF_8); - SecretKeySpec keySpec = new SecretKeySpec(raw, SecurityMethod.DES_ECB_PKCS5_PADDING.decryptType()); - Cipher cipher = Cipher.getInstance(SecurityMethod.DES_ECB_PKCS5_PADDING.decryptInformation()); - cipher.init(Cipher.DECRYPT_MODE, keySpec); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(decryptedBytes, StandardCharsets.UTF_8), false, null); - } - - @Override - public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return encrypt(src, key); - } - - @Override - public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return decrypt(src, key); - } -} diff --git a/src/main/java/tech/shiker/security/PBEWithSHA1AndDesSecurityInstance.java b/src/main/java/tech/shiker/security/PBEWithSHA1AndDesSecurityInstance.java deleted file mode 100644 index 4450624..0000000 --- a/src/main/java/tech/shiker/security/PBEWithSHA1AndDesSecurityInstance.java +++ /dev/null @@ -1,64 +0,0 @@ -package tech.shiker.security; - -import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; -import tech.shiker.enccore.DecryptResult; -import tech.shiker.enccore.EncryptResult; - -import javax.crypto.Cipher; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.PBEKeySpec; -import javax.crypto.spec.PBEParameterSpec; -import java.nio.charset.StandardCharsets; -import java.security.spec.AlgorithmParameterSpec; -import java.security.spec.KeySpec; -import java.util.Base64; - -public class PBEWithSHA1AndDesSecurityInstance implements SecurityInstance { - - public EncryptResult encrypt(String src, String key, String salt, Integer iterations ) throws Exception { - if (salt.length() < 8) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.SALT_INVALID_MESSAGE); - } - if (iterations == 0) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.ITERATIONS_INVALID_MESSAGE); - } - byte[] saltBytes = salt.getBytes(); - KeySpec keySpec = new PBEKeySpec(key.toCharArray(), saltBytes, iterations); - SecretKey secretKey = SecretKeyFactory.getInstance(SecurityMethod.PBE_WITH_SHA1_AND_DES.decryptType()).generateSecret(keySpec); - AlgorithmParameterSpec paramSpec = new PBEParameterSpec(saltBytes, iterations); - Cipher cipher = Cipher.getInstance(SecurityMethod.PBE_WITH_SHA1_AND_DES.decryptInformation()); - cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); - byte[] encryptedBytes = cipher.doFinal(src.getBytes()); - return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); - } - - public DecryptResult decrypt(String src, String key, String salt, Integer iterations) throws Exception { - if (salt.length() < 8) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.SALT_INVALID_MESSAGE); - } - if (iterations == 0) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.ITERATIONS_INVALID_MESSAGE); - } - byte[] saltBytes = salt.getBytes(); - KeySpec keySpec = new PBEKeySpec(key.toCharArray(), saltBytes, iterations); - SecretKey secretKey = SecretKeyFactory.getInstance(SecurityMethod.PBE_WITH_SHA1_AND_DES.decryptType()).generateSecret(keySpec); - AlgorithmParameterSpec paramSpec = new PBEParameterSpec(saltBytes, iterations); - Cipher cipher = Cipher.getInstance(SecurityMethod.PBE_WITH_SHA1_AND_DES.decryptInformation()); - cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(decryptedBytes, StandardCharsets.UTF_8), false, null); - } - - @Override - public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return encrypt(src, key, salt, iterations); - } - - @Override - public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - return decrypt(src, key, salt, iterations); - } -} diff --git a/src/main/java/tech/shiker/security/PBEWithSHA256AndAes128SecurityInstance.java b/src/main/java/tech/shiker/security/PBEWithSHA256AndAes128SecurityInstance.java deleted file mode 100644 index 65d9abb..0000000 --- a/src/main/java/tech/shiker/security/PBEWithSHA256AndAes128SecurityInstance.java +++ /dev/null @@ -1,66 +0,0 @@ -package tech.shiker.security; - -import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; -import tech.shiker.enccore.DecryptResult; -import tech.shiker.enccore.EncryptResult; - -import javax.crypto.Cipher; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.PBEKeySpec; -import javax.crypto.spec.PBEParameterSpec; -import java.nio.charset.StandardCharsets; -import java.security.spec.AlgorithmParameterSpec; -import java.security.spec.KeySpec; -import java.util.Base64; - -public class PBEWithSHA256AndAes128SecurityInstance implements SecurityInstance { - @Override - public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations ) throws Exception { - if (salt.length() < 8) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.SALT_INVALID_MESSAGE); - } - if (iterations == 0) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.ITERATIONS_INVALID_MESSAGE); - } - // 判断Key是否为16位 - if (index.length() != 16) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); - } - byte[] saltBytes = salt.getBytes(); - KeySpec keySpec = new PBEKeySpec(key.toCharArray(), saltBytes, iterations, 128); - SecretKey secretKey = SecretKeyFactory.getInstance(SecurityMethod.PBE_WITH_SHA256_AND_AES_128.decryptType()).generateSecret(keySpec); - IvParameterSpec iv = new IvParameterSpec(index.getBytes(StandardCharsets.UTF_8)); - AlgorithmParameterSpec paramSpec = new PBEParameterSpec(saltBytes, iterations, iv); - Cipher cipher = Cipher.getInstance(SecurityMethod.PBE_WITH_SHA256_AND_AES_128.decryptInformation()); - cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); - byte[] encryptedBytes = cipher.doFinal(src.getBytes()); - return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); - } - - @Override - public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - if (salt.length() < 8) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.SALT_INVALID_MESSAGE); - } - if (iterations == 0) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.ITERATIONS_INVALID_MESSAGE); - } - // 判断Key是否为16位 - if (index.length() != 16) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); - } - byte[] saltBytes = salt.getBytes(); - KeySpec keySpec = new PBEKeySpec(key.toCharArray(), saltBytes, iterations, 128); - SecretKey secretKey = SecretKeyFactory.getInstance(SecurityMethod.PBE_WITH_SHA256_AND_AES_128.decryptType()).generateSecret(keySpec); - IvParameterSpec iv = new IvParameterSpec(index.getBytes(StandardCharsets.UTF_8)); - AlgorithmParameterSpec paramSpec = new PBEParameterSpec(saltBytes, iterations, iv); - Cipher cipher = Cipher.getInstance(SecurityMethod.PBE_WITH_SHA256_AND_AES_128.decryptInformation()); - cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(decryptedBytes, StandardCharsets.UTF_8), false, null); - } -} diff --git a/src/main/java/tech/shiker/security/PBEWithSHA512AndAes256SecurityInstance.java b/src/main/java/tech/shiker/security/PBEWithSHA512AndAes256SecurityInstance.java deleted file mode 100644 index c13ead7..0000000 --- a/src/main/java/tech/shiker/security/PBEWithSHA512AndAes256SecurityInstance.java +++ /dev/null @@ -1,66 +0,0 @@ -package tech.shiker.security; - -import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; -import tech.shiker.enccore.DecryptResult; -import tech.shiker.enccore.EncryptResult; - -import javax.crypto.Cipher; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.PBEKeySpec; -import javax.crypto.spec.PBEParameterSpec; -import java.nio.charset.StandardCharsets; -import java.security.spec.AlgorithmParameterSpec; -import java.security.spec.KeySpec; -import java.util.Base64; - -public class PBEWithSHA512AndAes256SecurityInstance implements SecurityInstance { - @Override - public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations ) throws Exception { - if (salt.length() < 8) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.SALT_INVALID_MESSAGE); - } - if (iterations == 0) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.ITERATIONS_INVALID_MESSAGE); - } - // 判断Key是否为16位 - if (index.length() != 16) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); - } - byte[] saltBytes = salt.getBytes(); - KeySpec keySpec = new PBEKeySpec(key.toCharArray(), saltBytes, iterations, 128); - SecretKey secretKey = SecretKeyFactory.getInstance(SecurityMethod.PBE_WITH_SHA512_AND_AES_256.decryptType()).generateSecret(keySpec); - IvParameterSpec iv = new IvParameterSpec(index.getBytes(StandardCharsets.UTF_8)); - AlgorithmParameterSpec paramSpec = new PBEParameterSpec(saltBytes, iterations, iv); - Cipher cipher = Cipher.getInstance(SecurityMethod.PBE_WITH_SHA512_AND_AES_256.decryptInformation()); - cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); - byte[] encryptedBytes = cipher.doFinal(src.getBytes()); - return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); - } - - @Override - public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { - if (salt.length() < 8) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.SALT_INVALID_MESSAGE); - } - if (iterations == 0) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.ITERATIONS_INVALID_MESSAGE); - } - // 判断Key是否为16位 - if (index.length() != 16) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); - } - byte[] saltBytes = salt.getBytes(); - KeySpec keySpec = new PBEKeySpec(key.toCharArray(), saltBytes, iterations, 128); - SecretKey secretKey = SecretKeyFactory.getInstance(SecurityMethod.PBE_WITH_SHA512_AND_AES_256.decryptType()).generateSecret(keySpec); - IvParameterSpec iv = new IvParameterSpec(index.getBytes(StandardCharsets.UTF_8)); - AlgorithmParameterSpec paramSpec = new PBEParameterSpec(saltBytes, iterations, iv); - Cipher cipher = Cipher.getInstance(SecurityMethod.PBE_WITH_SHA512_AND_AES_256.decryptInformation()); - cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(decryptedBytes, StandardCharsets.UTF_8), false, null); - } -} diff --git a/src/main/java/tech/shiker/security/AesCBCNoPaddingSecurityInstance.java b/src/main/java/tech/shiker/security/impl/CBCSecurityInstance.java similarity index 52% rename from src/main/java/tech/shiker/security/AesCBCNoPaddingSecurityInstance.java rename to src/main/java/tech/shiker/security/impl/CBCSecurityInstance.java index 0aa7e99..7936899 100644 --- a/src/main/java/tech/shiker/security/AesCBCNoPaddingSecurityInstance.java +++ b/src/main/java/tech/shiker/security/impl/CBCSecurityInstance.java @@ -1,57 +1,66 @@ -package tech.shiker.security; +package tech.shiker.security.impl; +import tech.shiker.common.NoPaddingUtil; import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; +import tech.shiker.common.SecurityInfo; import tech.shiker.enccore.DecryptResult; import tech.shiker.enccore.EncryptResult; +import tech.shiker.security.SecurityInstance; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; import java.util.Base64; -public class AesCBCNoPaddingSecurityInstance implements SecurityInstance{ +public class CBCSecurityInstance implements SecurityInstance { + + private final SecurityInfo securityInfo; + + public CBCSecurityInstance(SecurityInfo securityInfo) { + this.securityInfo = securityInfo; + } public EncryptResult encrypt(String src, String key, String index) throws Exception { - if (key.length() != 16) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); + if (key.length() != securityInfo.keyLength()) { + return new EncryptResult("!!!!ERROR!!!", true, String.format(SecurityConstant.KEY_INVALID_MESSAGE, securityInfo.keyLength())); } // 判断Key是否正确 if (index == null) { return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_NULL_MESSAGE); } // 判断Key是否为16位 - if (index.length() != 16) { - return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); + if (index.length() != securityInfo.indexLength()) { + return new EncryptResult("!!!!ERROR!!!", true, String.format(SecurityConstant.IV_INVALID_MESSAGE, securityInfo.indexLength())); } - Cipher cipher = Cipher.getInstance(SecurityMethod.AES_CBC_NO_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), SecurityMethod.AES_CBC_NO_PADDING.decryptType()); + Cipher cipher = Cipher.getInstance(securityInfo.decryptInformation()); + SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), securityInfo.decryptType()); IvParameterSpec iv = new IvParameterSpec(index.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); - byte[] encryptedBytes = cipher.doFinal(NoPaddingUtil.addPadding(src)); + byte[] encryptedBytes = cipher.doFinal(securityInfo.handlePadding() ? NoPaddingUtil.addPadding(src) : src.getBytes()); return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); } public DecryptResult decrypt(String src, String key, String index) throws Exception { // 判断Key是否为16位 - if (key.length() != 16) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.KEY_INVALID_MESSAGE); + if (key.length() != securityInfo.keyLength()) { + return new DecryptResult("!!!!ERROR!!!", true, String.format(SecurityConstant.KEY_INVALID_MESSAGE, securityInfo.keyLength())); } // 判断Key是否正确 if (index == null) { return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_NULL_MESSAGE); } // 判断Key是否为16位 - if (index.length() != 16) { - return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.IV_INVALID_MESSAGE); + if (index.length() != securityInfo.indexLength()) { + return new DecryptResult("!!!!ERROR!!!", true, String.format(SecurityConstant.IV_INVALID_MESSAGE, securityInfo.indexLength())); } - Cipher cipher = Cipher.getInstance(SecurityMethod.AES_CBC_NO_PADDING.decryptInformation()); - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), SecurityMethod.AES_CBC_NO_PADDING.decryptType()); + Cipher cipher = Cipher.getInstance(securityInfo.decryptInformation()); + SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), securityInfo.decryptType()); IvParameterSpec iv = new IvParameterSpec(index.getBytes()); cipher.init(Cipher.DECRYPT_MODE, keySpec, iv); byte[] encryptedBytes = Base64.getDecoder().decode(src); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(NoPaddingUtil.removePadding(decryptedBytes)), false, null); + return new DecryptResult(new String(securityInfo.handlePadding() ? NoPaddingUtil.removePadding(decryptedBytes) : decryptedBytes, StandardCharsets.UTF_8), false, null); } @Override diff --git a/src/main/java/tech/shiker/security/impl/ECBSecurityInstance.java b/src/main/java/tech/shiker/security/impl/ECBSecurityInstance.java new file mode 100644 index 0000000..175d90b --- /dev/null +++ b/src/main/java/tech/shiker/security/impl/ECBSecurityInstance.java @@ -0,0 +1,56 @@ +package tech.shiker.security.impl; + +import tech.shiker.common.NoPaddingUtil; +import tech.shiker.common.SecurityConstant; +import tech.shiker.common.SecurityInfo; +import tech.shiker.enccore.DecryptResult; +import tech.shiker.enccore.EncryptResult; +import tech.shiker.security.SecurityInstance; + +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +public class ECBSecurityInstance implements SecurityInstance { + private final SecurityInfo securityInfo; + + public ECBSecurityInstance(SecurityInfo securityInfo) { + this.securityInfo = securityInfo; + } + + public EncryptResult encrypt(String src, String key) throws Exception { + // 判断Key是否为16位 + if (key.length() != securityInfo.keyLength()) { + return new EncryptResult("!!!!ERROR!!!", true, String.format(SecurityConstant.KEY_INVALID_MESSAGE, securityInfo.keyLength())); + } + Cipher cipher = Cipher.getInstance(securityInfo.decryptInformation()); + SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), securityInfo.decryptType()); + cipher.init(Cipher.ENCRYPT_MODE, keySpec); + byte[] encryptedBytes = cipher.doFinal(securityInfo.handlePadding() ? NoPaddingUtil.addPadding(src) : src.getBytes()); + return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); + } + + public DecryptResult decrypt(String src, String key) throws Exception { + // 判断Key是否为16位 + if (key.length() != securityInfo.keyLength()) { + return new DecryptResult("!!!!ERROR!!!", true, String.format(SecurityConstant.KEY_INVALID_MESSAGE, securityInfo.keyLength())); + } + Cipher cipher = Cipher.getInstance(securityInfo.decryptInformation()); + SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), securityInfo.decryptType()); + cipher.init(Cipher.DECRYPT_MODE, keySpec); + byte[] encryptedBytes = Base64.getDecoder().decode(src); + byte[] decryptedBytes = cipher.doFinal(encryptedBytes); + return new DecryptResult(new String(securityInfo.handlePadding() ? NoPaddingUtil.removePadding(decryptedBytes) : decryptedBytes, StandardCharsets.UTF_8).trim(), false, null); + } + + @Override + public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { + return encrypt(src, key); + } + + @Override + public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { + return decrypt(src, key); + } +} diff --git a/src/main/java/tech/shiker/security/impl/PBEWithAesSecurityInstance.java b/src/main/java/tech/shiker/security/impl/PBEWithAesSecurityInstance.java new file mode 100644 index 0000000..a0d0b84 --- /dev/null +++ b/src/main/java/tech/shiker/security/impl/PBEWithAesSecurityInstance.java @@ -0,0 +1,81 @@ +package tech.shiker.security.impl; + +import org.jetbrains.annotations.NotNull; +import tech.shiker.common.SecurityConstant; +import tech.shiker.common.SecurityInfo; +import tech.shiker.enccore.DecryptResult; +import tech.shiker.enccore.EncryptResult; +import tech.shiker.security.SecurityInstance; + +import javax.crypto.Cipher; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.PBEParameterSpec; +import java.nio.charset.StandardCharsets; +import java.security.NoSuchAlgorithmException; +import java.security.spec.AlgorithmParameterSpec; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.KeySpec; +import java.util.Base64; + +public class PBEWithAesSecurityInstance implements SecurityInstance { + + private final SecurityInfo securityInfo; + + public PBEWithAesSecurityInstance(SecurityInfo securityInfo) { + this.securityInfo = securityInfo; + } + @Override + public EncryptResult encrypt(String src, String key, String index, String salt, Integer iterations ) throws Exception { + if (salt.length() < securityInfo.saltLength()) { + return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.SALT_INVALID_MESSAGE); + } + if (iterations == securityInfo.iterations()) { + return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.ITERATIONS_INVALID_MESSAGE); + } + // 判断Key是否为16位 + if (index.length() != securityInfo.indexLength()) { + return new EncryptResult("!!!!ERROR!!!", true, String.format(SecurityConstant.IV_INVALID_MESSAGE, securityInfo.indexLength())); + } + Result result = getSecurityCipherInfo(key, index, salt, iterations); + result.cipher().init(Cipher.ENCRYPT_MODE, result.secretKey(), result.paramSpec()); + byte[] encryptedBytes = result.cipher().doFinal(src.getBytes()); + return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); + } + + @Override + public DecryptResult decrypt(String src, String key, String index, String salt, Integer iterations) throws Exception { + if (salt.length() < securityInfo.saltLength()) { + return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.SALT_INVALID_MESSAGE); + } + if (iterations == securityInfo.iterations()) { + return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.ITERATIONS_INVALID_MESSAGE); + } + // 判断Key是否为16位 + if (index.length() != securityInfo.indexLength()) { + return new DecryptResult("!!!!ERROR!!!", true, String.format(SecurityConstant.IV_INVALID_MESSAGE, securityInfo.indexLength())); + } + Result result = getSecurityCipherInfo(key, index, salt, iterations); + result.cipher().init(Cipher.DECRYPT_MODE, result.secretKey(), result.paramSpec()); + byte[] encryptedBytes = Base64.getDecoder().decode(src); + byte[] decryptedBytes = result.cipher().doFinal(encryptedBytes); + return new DecryptResult(new String(decryptedBytes, StandardCharsets.UTF_8), false, null); + } + + @NotNull + private Result getSecurityCipherInfo(String key, String index, String salt, Integer iterations) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException { + byte[] saltBytes = salt.getBytes(); + KeySpec keySpec = new PBEKeySpec(key.toCharArray(), saltBytes, iterations, 128); + SecretKey secretKey = SecretKeyFactory.getInstance(securityInfo.decryptType()).generateSecret(keySpec); + IvParameterSpec iv = new IvParameterSpec(index.getBytes(StandardCharsets.UTF_8)); + AlgorithmParameterSpec paramSpec = new PBEParameterSpec(saltBytes, iterations, iv); + Cipher cipher = Cipher.getInstance(securityInfo.decryptInformation()); + return new Result(secretKey, paramSpec, cipher); + } + + private record Result(SecretKey secretKey, AlgorithmParameterSpec paramSpec, Cipher cipher) { + } +} diff --git a/src/main/java/tech/shiker/security/PBEWithMD5AndDesSecurityInstance.java b/src/main/java/tech/shiker/security/impl/PBEWithDesSecurityInstance.java similarity index 53% rename from src/main/java/tech/shiker/security/PBEWithMD5AndDesSecurityInstance.java rename to src/main/java/tech/shiker/security/impl/PBEWithDesSecurityInstance.java index c7bbc95..2cf9124 100644 --- a/src/main/java/tech/shiker/security/PBEWithMD5AndDesSecurityInstance.java +++ b/src/main/java/tech/shiker/security/impl/PBEWithDesSecurityInstance.java @@ -1,56 +1,71 @@ -package tech.shiker.security; +package tech.shiker.security.impl; +import org.jetbrains.annotations.NotNull; import tech.shiker.common.SecurityConstant; -import tech.shiker.common.SecurityMethod; +import tech.shiker.common.SecurityInfo; import tech.shiker.enccore.DecryptResult; import tech.shiker.enccore.EncryptResult; +import tech.shiker.security.SecurityInstance; import javax.crypto.Cipher; +import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; import java.nio.charset.StandardCharsets; +import java.security.NoSuchAlgorithmException; import java.security.spec.AlgorithmParameterSpec; +import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import java.util.Base64; -public class PBEWithMD5AndDesSecurityInstance implements SecurityInstance { +public class PBEWithDesSecurityInstance implements SecurityInstance { + private final SecurityInfo securityInfo; + public PBEWithDesSecurityInstance(SecurityInfo securityInfo) { + this.securityInfo = securityInfo; + } public EncryptResult encrypt(String src, String key, String salt, Integer iterations) throws Exception { // 判断Key是否为16位 - if (salt.length() < 8) { + if (salt.length() < securityInfo.saltLength()) { return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.SALT_INVALID_MESSAGE); } - if (iterations == 0) { + if (iterations == securityInfo.iterations()) { return new EncryptResult("!!!!ERROR!!!", true, SecurityConstant.ITERATIONS_INVALID_MESSAGE); } - byte[] saltBytes = salt.getBytes(); - KeySpec keySpec = new PBEKeySpec(key.toCharArray(), saltBytes, iterations); - SecretKey secretKey = SecretKeyFactory.getInstance(SecurityMethod.PBE_WITH_MD5_AND_DES.decryptType()).generateSecret(keySpec); - AlgorithmParameterSpec paramSpec = new PBEParameterSpec(saltBytes, iterations); - Cipher cipher = Cipher.getInstance(SecurityMethod.PBE_WITH_MD5_AND_DES.decryptInformation()); - cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); - byte[] encryptedBytes = cipher.doFinal(src.getBytes()); + + Result result = getSecurityCipherInfo(key, salt, iterations); + result.cipher().init(Cipher.ENCRYPT_MODE, result.secretKey(), result.paramSpec()); + byte[] encryptedBytes = result.cipher().doFinal(src.getBytes()); return new EncryptResult(Base64.getEncoder().encodeToString(encryptedBytes), false, null); } public DecryptResult decrypt(String src, String key, String salt, Integer iterations) throws Exception { - if (salt.length() < 8) { + if (salt.length() < securityInfo.saltLength()) { return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.SALT_INVALID_MESSAGE); } - if (iterations == 0) { + if (iterations == securityInfo.iterations()) { return new DecryptResult("!!!!ERROR!!!", true, SecurityConstant.ITERATIONS_INVALID_MESSAGE); } + Result result = getSecurityCipherInfo(key, salt, iterations); + result.cipher().init(Cipher.DECRYPT_MODE, result.secretKey(), result.paramSpec()); + byte[] encryptedBytes = Base64.getDecoder().decode(src); + byte[] decryptedBytes = result.cipher().doFinal(encryptedBytes); + return new DecryptResult(new String(decryptedBytes, StandardCharsets.UTF_8), false, null); + } + + @NotNull + private Result getSecurityCipherInfo(String key, String salt, Integer iterations) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException { byte[] saltBytes = salt.getBytes(); KeySpec keySpec = new PBEKeySpec(key.toCharArray(), saltBytes, iterations); - SecretKey secretKey = SecretKeyFactory.getInstance(SecurityMethod.PBE_WITH_MD5_AND_DES.decryptType()).generateSecret(keySpec); + SecretKey secretKey = SecretKeyFactory.getInstance(securityInfo.decryptType()).generateSecret(keySpec); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(saltBytes, iterations); - Cipher cipher = Cipher.getInstance(SecurityMethod.PBE_WITH_MD5_AND_DES.decryptInformation()); - cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); - byte[] encryptedBytes = Base64.getDecoder().decode(src); - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); - return new DecryptResult(new String(decryptedBytes, StandardCharsets.UTF_8), false, null); + Cipher cipher = Cipher.getInstance(securityInfo.decryptInformation()); + return new Result(secretKey, paramSpec, cipher); + } + + private record Result(SecretKey secretKey, AlgorithmParameterSpec paramSpec, Cipher cipher) { } diff --git a/src/test/java/test/SecurityInstanceTest.java b/src/test/java/test/SecurityInstanceTest.java new file mode 100644 index 0000000..43080ad --- /dev/null +++ b/src/test/java/test/SecurityInstanceTest.java @@ -0,0 +1,193 @@ +package test; + +import org.junit.Assert; +import org.junit.Test; +import tech.shiker.common.SecurityMethod; +import tech.shiker.enccore.DecryptResult; +import tech.shiker.enccore.EncryptResult; +import tech.shiker.security.SecurityInstance; + + +public class SecurityInstanceTest { + + @Test + public void DES_ECB_PKCS5_PADDING_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "123456789012345678901234"; + String salt = "12345678"; + String index = "1234567812345678"; + SecurityInstance securityInstance = SecurityMethod.DES_ECB_PKCS5_PADDING.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + @Test + public void DES_ECB_NO_PADDING_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "123456789012345678901234"; + String salt = "12345678"; + String index = "1234567812345678"; + SecurityInstance securityInstance = SecurityMethod.DES_ECB_NO_PADDING.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + @Test + public void DES_CBC_NO_PADDING_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "123456789012345678901234"; + String salt = "12345678"; + String index = "12345678"; + SecurityInstance securityInstance = SecurityMethod.DES_CBC_NO_PADDING.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + @Test + public void DES_CBC_PKCS5_PADDING_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "123456789012345678901234"; + String salt = "12345678"; + String index = "12345678"; + SecurityInstance securityInstance = SecurityMethod.DES_CBC_PKCS5_PADDING.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + + @Test + public void AES_ECB_PKCS5_PADDING_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "MySecretPassword"; + String salt = "12345678"; + String index = "1234567812345678"; + SecurityInstance securityInstance = SecurityMethod.AES_ECB_PKCS5_PADDING.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + @Test + public void AES_ECB_NO_PADDING_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "MySecretPassword"; + String salt = "12345678"; + String index = "1234567812345678"; + SecurityInstance securityInstance = SecurityMethod.AES_ECB_NO_PADDING.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + @Test + public void AES_CBC_NO_PADDING_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "MySecretPassword"; + String salt = "12345678"; + String index = "1234567812345678"; + SecurityInstance securityInstance = SecurityMethod.AES_CBC_NO_PADDING.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + @Test + public void AES_CBC_PKCS5_PADDING_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "MySecretPassword"; + String salt = "12345678"; + String index = "1234567812345678"; + SecurityInstance securityInstance = SecurityMethod.AES_CBC_PKCS5_PADDING.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + @Test + public void PBE_WITH_SHA1_AND_DES_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "MySecretPassword"; + String salt = "12345678"; + String index = "1234567812345678"; + SecurityInstance securityInstance = SecurityMethod.PBE_WITH_SHA1_AND_DES.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + @Test + public void PBE_WITH_MD5_AND_DES_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "MySecretPassword"; + String salt = "12345678"; + String index = "1234567812345678"; + SecurityInstance securityInstance = SecurityMethod.PBE_WITH_MD5_AND_DES.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + @Test + public void PBE_WITH_SHA512_AND_AES_256_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "MySecretPassword"; + String salt = "12345678"; + String index = "1234567812345678"; + SecurityInstance securityInstance = SecurityMethod.PBE_WITH_SHA512_AND_AES_256.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } + + @Test + public void PBE_WITH_SHA256_AND_AES_128_Test() throws Exception { + String originalText = "Hello, world!"; + String password = "MySecretPassword"; + String salt = "12345678"; + String index = "1234567812345678"; + SecurityInstance securityInstance = SecurityMethod.PBE_WITH_SHA256_AND_AES_128.decryptInstance(); + // Encryption + EncryptResult encryptResult = securityInstance.encrypt(originalText, password, index, salt, 1000); + Assert.assertNull(encryptResult.message()); + // Decryption + DecryptResult decryptResult = securityInstance.decrypt(encryptResult.encryptStr(), password, index, salt, 1000); + Assert.assertEquals(decryptResult.decryptStr(), originalText); + } +}