├── lib └── commons-codec-1.11.jar ├── README.md ├── .classpath ├── .project ├── src └── com │ └── github │ └── encrypt │ ├── EncryptEnum │ ├── MDEnum.java │ ├── ShaEnum.java │ ├── RSAEnum.java │ ├── AseEnum.java │ ├── DecEnum.java │ └── Dec3Enum.java │ ├── encryptImp │ ├── IMessageDigest.java │ ├── ICipherEncrypt.java │ ├── MessageDigestImp.java │ └── CipherEencryptImp.java │ ├── encryptTypeImpl │ ├── ShaUtil.java │ ├── HexUtil.java │ ├── MDUtil.java │ ├── AesUtil.java │ ├── DesUtil.java │ ├── Des3Util.java │ ├── RsaUtil.java │ └── AesKgenUtil.java │ ├── EncryptUtil.java │ └── EncryptTest.java └── .settings └── org.eclipse.jdt.core.prefs /lib/commons-codec-1.11.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caizhaokai/Java_Cipher_Encrypt/HEAD/lib/commons-codec-1.11.jar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java_Cipher_Encrypt 2 | # Java原生加密,使用Cipher类进行加密 3 | @see https://www.cnblogs.com/caizhaokai/p/10944667.html 4 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Java_Cipher_Encrypt 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/com/github/encrypt/EncryptEnum/MDEnum.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.EncryptEnum; 2 | 3 | public enum MDEnum { 4 | MD2("MD2"), 5 | MD5("MD5"); 6 | 7 | private MDEnum(String encryptType) { 8 | this.encryptType = encryptType; 9 | } 10 | 11 | private String encryptType; 12 | 13 | public String getEncryptType() { 14 | return encryptType; 15 | } 16 | 17 | public void setEncryptType(String encryptType) { 18 | this.encryptType = encryptType; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/com/github/encrypt/EncryptEnum/ShaEnum.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.EncryptEnum; 2 | 3 | public enum ShaEnum { 4 | SHA1("SHA-1"), 5 | SHA224("SHA-224"), 6 | SHA256("SHA-256"), 7 | SHA384("SHA-384"), 8 | SHA512("SHA-512"); 9 | 10 | private ShaEnum(String encryptType) { 11 | this.encryptType = encryptType; 12 | } 13 | 14 | private String encryptType; 15 | 16 | public String getEncryptType() { 17 | return encryptType; 18 | } 19 | 20 | public void setEncryptType(String encryptType) { 21 | this.encryptType = encryptType; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptImp/IMessageDigest.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptImp; 2 | 3 | /** 4 | * MessageDigest加密的接口 5 | */ 6 | public interface IMessageDigest> { 7 | String encryptBase64(String content); 8 | String encryptBase64(String content, String slat); 9 | String encryptBase64(String content, E encryptType); 10 | String encryptBase64(String content, String slat, E encryptType); 11 | 12 | String encryptHex(String content); 13 | String encryptHex(String content, String slat); 14 | String encryptHex(String content, E encryptType); 15 | String encryptHex(String content, String slat, E encryptType); 16 | } 17 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /src/com/github/encrypt/EncryptEnum/RSAEnum.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.EncryptEnum; 2 | 3 | public enum RSAEnum { 4 | /** 5 | * 无向量加密模式, PKCS1Padding模式填充 6 | */ 7 | ECB_PKCS1PADDING("RSA/ECB/PKCS1Padding"), 8 | /** 9 | * 无向量加密模式, SHA-1摘要 + MGF1方式填充 10 | */ 11 | ECB_OAEP_WITH_SHA1_AND_MGF_1PADDING("RSA/ECB/OAEPWithSHA-1AndMGF1Padding"), 12 | /** 13 | * 无向量加密模式, SHA-256摘要 + MGF1方式填充 14 | */ 15 | ECB_OAEP_WITH_SHA256_AND_MGF_1PADDING("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); 16 | 17 | private RSAEnum(String encryptType) { 18 | this.encryptType = encryptType; 19 | } 20 | 21 | private String encryptType; 22 | 23 | public String getEncryptType() { 24 | return encryptType; 25 | } 26 | 27 | public void setEncryptType(String encryptType) { 28 | this.encryptType = encryptType; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/com/github/encrypt/EncryptEnum/AseEnum.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.EncryptEnum; 2 | 3 | public enum AseEnum { 4 | /** 5 | * 有向量加密模式, 不足8位用0补足8位, 需代码给加密内容添加0, 如{65,65,65,0,0,0,0,0} 6 | */ 7 | CBC_NO_PADDING("AES/CBC/NoPadding"), 8 | /** 9 | * 有向量加密模式, 不足8位用余位数补足8位, 如{65,65,65,5,5,5,5,5}或{97,97,97,97,97,97,2,2}; 刚好8位补8位8 10 | */ 11 | CBC_PKCS5PADDING("AES/CBC/PKCS5Padding"), 12 | /** 13 | * 无向量加密模式, 不足8位用0补足8位, 需代码给加密内容添加0 14 | */ 15 | ECB_NO_PADDING("AES/ECB/NoPadding"), 16 | /** 17 | * 无向量加密模式, 不足8位用余位数补足8位 18 | */ 19 | ECB_PKCS5PADDING("AES/ECB/PKCS5Padding"); 20 | 21 | private AseEnum(String encryptType) { 22 | this.encryptType = encryptType; 23 | } 24 | 25 | private String encryptType; 26 | 27 | public String getEncryptType() { 28 | return encryptType; 29 | } 30 | 31 | public void setEncryptType(String encryptType) { 32 | this.encryptType = encryptType; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/com/github/encrypt/EncryptEnum/DecEnum.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.EncryptEnum; 2 | 3 | public enum DecEnum { 4 | /** 5 | * 有向量加密模式, 不足8位用0补足8位, 需代码给加密内容添加0, 如{65,65,65,0,0,0,0,0} 6 | */ 7 | CBC_NO_PADDING("DES/CBC/NoPadding"), 8 | /** 9 | * 有向量加密模式, 不足8位用余位数补足8位, 如{65,65,65,5,5,5,5,5}或{97,97,97,97,97,97,2,2}; 10 | * 刚好8位补8位8 11 | */ 12 | CBC_PKCS5PADDING("DES/CBC/PKCS5Padding"), 13 | /** 14 | * 无向量加密模式, 不足8位用0补足8位, 需代码给加密内容添加0 15 | */ 16 | ECB_NO_PADDING("DES/ECB/NoPadding"), 17 | /** 18 | * 无向量加密模式, 不足8位用余位数补足8位 19 | */ 20 | ECB_PKCS5PADDING("DES/ECB/PKCS5Padding"); 21 | 22 | private DecEnum(String encryptType) { 23 | this.encryptType = encryptType; 24 | } 25 | 26 | private String encryptType; 27 | 28 | public String getEncryptType() { 29 | return encryptType; 30 | } 31 | 32 | public void setEncryptType(String encryptType) { 33 | this.encryptType = encryptType; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/com/github/encrypt/EncryptEnum/Dec3Enum.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.EncryptEnum; 2 | 3 | public enum Dec3Enum { 4 | /** 5 | * 有向量加密模式, 不足8位用0补足8位, 需代码给加密内容添加0, 如{65,65,65,0,0,0,0,0} 6 | */ 7 | CBC_NO_PADDING("DESede/CBC/NoPadding"), 8 | /** 9 | * 有向量加密模式, 不足8位用余位数补足8位, 如{65,65,65,5,5,5,5,5}或{97,97,97,97,97,97,2,2}; 10 | * 刚好8位补8位8 11 | */ 12 | CBC_PKCS5PADDING("DESede/CBC/PKCS5Padding"), 13 | /** 14 | * 无向量加密模式, 不足8位用0补足8位, 需代码给加密内容添加0 15 | */ 16 | ECB_NO_PADDING("DESede/ECB/NoPadding"), 17 | /** 18 | * 无向量加密模式, 不足8位用余位数补足8位 19 | */ 20 | ECB_PKCS5PADDING("DESede/ECB/PKCS5Padding"); 21 | 22 | private Dec3Enum(String encryptType) { 23 | this.encryptType = encryptType; 24 | } 25 | 26 | private String encryptType; 27 | 28 | public String getEncryptType() { 29 | return encryptType; 30 | } 31 | 32 | public void setEncryptType(String encryptType) { 33 | this.encryptType = encryptType; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptTypeImpl/ShaUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptTypeImpl; 2 | 3 | import java.security.MessageDigest; 4 | import java.security.NoSuchAlgorithmException; 5 | 6 | import com.github.encrypt.EncryptUtil; 7 | import com.github.encrypt.EncryptEnum.ShaEnum; 8 | import com.github.encrypt.encryptImp.MessageDigestImp; 9 | 10 | /** 11 | * SHA摘要加密 12 | */ 13 | public class ShaUtil extends MessageDigestImp{ 14 | public ShaUtil(ShaEnum defaultEncrypt) { 15 | this.defaultAlgorithm = defaultEncrypt == null ? ShaEnum.SHA256 : defaultEncrypt; 16 | this.configSlat = EncryptUtil.SHA_SLAT; 17 | } 18 | // private final static Logger logger = LoggerFactory.getLogger(ShaUtil.class); 19 | 20 | @Override 21 | protected byte[] encrypt(String content, String slat, ShaEnum encryptType) { 22 | try { 23 | String encryptContent = content + slat; 24 | MessageDigest messageDigest = MessageDigest.getInstance(encryptType.getEncryptType()); 25 | return messageDigest.digest(encryptContent.getBytes()); 26 | } catch (NoSuchAlgorithmException e) { 27 | // logger.error("Sha MessageDigest init error, encrypt type no support."); 28 | } 29 | return null; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptTypeImpl/HexUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptTypeImpl; 2 | 3 | 4 | /** 5 | * 16进制工具类 6 | */ 7 | public class HexUtil { 8 | private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 9 | 10 | /** 11 | * 十六进制转化为二进制 12 | */ 13 | public static byte[] hexStrToByteArray(String hexString) { 14 | if (hexString == null) { 15 | return null; 16 | } 17 | if (hexString.length() == 0) { 18 | return new byte[0]; 19 | } 20 | byte[] byteArray = new byte[hexString.length() / 2]; 21 | for (int i = 0; i < byteArray.length; i++) { 22 | String subStr = hexString.substring(2 * i, 2 * i + 2); 23 | byteArray[i] = ((byte) Integer.parseInt(subStr, 16)); 24 | } 25 | return byteArray; 26 | } 27 | 28 | /** 29 | * 二进制转化为十六进制 30 | */ 31 | public static String byteArrayToHexStr(byte[] byteArray) { 32 | if (byteArray == null) { 33 | return null; 34 | } 35 | char[] hexChars = new char[byteArray.length * 2]; 36 | for (int j = 0; j < byteArray.length; j++) { 37 | int v = byteArray[j] & 0xFF; 38 | hexChars[j * 2] = HEX_CHARS[v >>> 4]; 39 | hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F]; 40 | } 41 | return new String(hexChars); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptTypeImpl/MDUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptTypeImpl; 2 | 3 | import java.security.MessageDigest; 4 | import java.security.NoSuchAlgorithmException; 5 | 6 | import com.github.encrypt.EncryptUtil; 7 | import com.github.encrypt.EncryptEnum.MDEnum; 8 | import com.github.encrypt.encryptImp.MessageDigestImp; 9 | 10 | /** 11 | * MD加密工具类 12 | */ 13 | public class MDUtil extends MessageDigestImp{ 14 | 15 | // private final static Logger logger = LoggerFactory.getLogger(ShaUtil.class); 16 | 17 | public MDUtil(MDEnum defaultEncrypt) { 18 | this.defaultAlgorithm = defaultEncrypt == null ? MDEnum.MD5 : defaultEncrypt; 19 | this.configSlat = EncryptUtil.MD_SLAT; 20 | } 21 | 22 | @Override 23 | protected byte[] encrypt(String content, String slat, MDEnum encryptType) { 24 | try { 25 | String encryptContent = null; 26 | if (slat != null) 27 | { 28 | encryptContent = content + slat; 29 | } 30 | else 31 | { 32 | encryptContent = content; 33 | } 34 | MessageDigest messageDigest = MessageDigest.getInstance(encryptType.getEncryptType()); 35 | return messageDigest.digest(encryptContent.getBytes()); 36 | } catch (NoSuchAlgorithmException e) { 37 | // logger.error("MD MessageDigest init error, encrypt type no support."); 38 | } 39 | return null; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptImp/ICipherEncrypt.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptImp; 2 | 3 | /** 4 | * Cipher类的加密、解密接口 5 | */ 6 | public interface ICipherEncrypt> { 7 | String encryptBase64(String content) throws Exception; 8 | String decryptBase64(String content) throws Exception; 9 | String encryptBase64(String content, String slatKey, String vectorKey) throws Exception; 10 | String decryptBase64(String content, String slatKey, String vectorKey) throws Exception; 11 | String encryptBase64(String content, E encryptType) throws Exception; 12 | String decryptBase64(String content, E encryptType) throws Exception; 13 | String encryptBase64(String content, String slatKey, String vectorKey, E encryptType) throws Exception; 14 | String decryptBase64(String content, String slatKey, String vectorKey, E encryptType) throws Exception; 15 | 16 | String encryptHex(String content) throws Exception; 17 | String decryptHex(String content) throws Exception; 18 | String encryptHex(String content, String slatKey, String vectorKey) throws Exception; 19 | String decryptHex(String content, String slatKey, String vectorKey) throws Exception; 20 | String encryptHex(String content, E encryptType) throws Exception; 21 | String decryptHex(String content, E encryptType) throws Exception; 22 | String encryptHex(String content, String slatKey, String vectorKey, E encryptType) throws Exception; 23 | String decryptHex(String content, String slatKey, String vectorKey, E encryptType) throws Exception; 24 | } 25 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptImp/MessageDigestImp.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptImp; 2 | 3 | import org.apache.commons.codec.binary.Base64; 4 | 5 | import com.github.encrypt.encryptTypeImpl.HexUtil; 6 | 7 | /** 8 | * 使用MessageDigest进行加密 9 | */ 10 | public abstract class MessageDigestImp > implements IMessageDigest { 11 | /** 12 | * 配置文件配置的盐 13 | */ 14 | protected String configSlat = null; 15 | /** 16 | * 默认加密模式 17 | */ 18 | protected E defaultAlgorithm = null; 19 | 20 | protected abstract byte[] encrypt(String content, String slat, E encryptType); 21 | 22 | @Override 23 | public String encryptBase64(String content) { 24 | return encryptBase64(content, configSlat, defaultAlgorithm); 25 | } 26 | 27 | @Override 28 | public String encryptBase64(String content, String slat) { 29 | return encryptBase64(content, slat, defaultAlgorithm); 30 | } 31 | 32 | @Override 33 | public String encryptBase64(String content, E encryptType) { 34 | return encryptBase64(content, configSlat, encryptType); 35 | } 36 | 37 | @Override 38 | public String encryptBase64(String content, String slat, E encryptType) { 39 | byte[] result = encrypt(content, slat, encryptType); 40 | return Base64.encodeBase64String(result); 41 | } 42 | 43 | @Override 44 | public String encryptHex(String content) { 45 | return encryptHex(content, configSlat, defaultAlgorithm); 46 | } 47 | 48 | @Override 49 | public String encryptHex(String content, String slat) { 50 | return encryptHex(content, slat, defaultAlgorithm); 51 | } 52 | 53 | @Override 54 | public String encryptHex(String content, E encryptType) { 55 | return encryptHex(content, configSlat, encryptType); 56 | } 57 | 58 | @Override 59 | public String encryptHex(String content, String slat, E encryptType) { 60 | byte[] result = encrypt(content, slat, encryptType); 61 | return HexUtil.byteArrayToHexStr(result); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/com/github/encrypt/EncryptUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt; 2 | 3 | import com.github.encrypt.EncryptEnum.AseEnum; 4 | import com.github.encrypt.EncryptEnum.Dec3Enum; 5 | import com.github.encrypt.EncryptEnum.DecEnum; 6 | import com.github.encrypt.EncryptEnum.MDEnum; 7 | import com.github.encrypt.EncryptEnum.RSAEnum; 8 | import com.github.encrypt.EncryptEnum.ShaEnum; 9 | import com.github.encrypt.encryptImp.ICipherEncrypt; 10 | import com.github.encrypt.encryptImp.IMessageDigest; 11 | import com.github.encrypt.encryptTypeImpl.AesKgenUtil; 12 | import com.github.encrypt.encryptTypeImpl.AesUtil; 13 | import com.github.encrypt.encryptTypeImpl.Des3Util; 14 | import com.github.encrypt.encryptTypeImpl.DesUtil; 15 | import com.github.encrypt.encryptTypeImpl.MDUtil; 16 | import com.github.encrypt.encryptTypeImpl.RsaUtil; 17 | import com.github.encrypt.encryptTypeImpl.ShaUtil; 18 | 19 | /** 20 | * 加密算法工具类 21 | */ 22 | public class EncryptUtil { 23 | public static String MD_SLAT = "797bv851O7pU04yu001aZ57Vb36Ai4810786Khe0H59U85Z2LR61o5201zam5DdxR7084A9G71"; 24 | 25 | public static String SHA_SLAT = "797bv851O7pU04yu001aZ57Vb36Ai4810786Khe0H59U85Z2LR61o5201zam5DdxR7084A9G72"; 26 | 27 | public static String AES_SLAT = "797bv851O7pU04yu001aZ57Vb36Ai4810786Khe0H59U85Z2LR61o5201zam5DdxR7084A9G73"; 28 | 29 | public static String DES_SLAT = "797bv851O7pU04yu001aZ57Vb36Ai4810786Khe0H59U85Z2LR61o5201zam5DdxR7084A9G74"; 30 | 31 | public static String DES3_SLAT = "797bv851O7pU04yu001aZ57Vb36Ai4810786Khe0H59U85Z2LR61o5201zam5DdxR7084A9G74"; 32 | 33 | public static String RSA_SLAT = "797bv851O7pU04yu001aZ57Vb36Ai4810786Khe0H59U85Z2LR61o5201zam5DdxR7084A9G75"; 34 | 35 | public final static IMessageDigest MD_ENCRYPT = new MDUtil(MDEnum.MD5); 36 | 37 | public final static IMessageDigest SHA_ENCRYPT = new ShaUtil(ShaEnum.SHA256); 38 | 39 | public final static ICipherEncrypt AES_ENCRYPT = new AesUtil(AseEnum.CBC_NO_PADDING); 40 | 41 | public final static ICipherEncrypt DES_ENCRYPT = new DesUtil(DecEnum.CBC_NO_PADDING); 42 | 43 | public final static ICipherEncrypt DES3_ENCRYPT = new Des3Util(Dec3Enum.CBC_NO_PADDING); 44 | 45 | public final static ICipherEncrypt AES_KGEN_ENCRYPT = new AesKgenUtil(AseEnum.CBC_NO_PADDING); 46 | 47 | public final static ICipherEncrypt RSA_ENCRYPT = new RsaUtil(RSAEnum.ECB_PKCS1PADDING); 48 | } 49 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptTypeImpl/AesUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptTypeImpl; 2 | 3 | import javax.crypto.Cipher; 4 | import javax.crypto.SecretKey; 5 | import javax.crypto.spec.IvParameterSpec; 6 | import javax.crypto.spec.SecretKeySpec; 7 | 8 | import com.github.encrypt.EncryptUtil; 9 | import com.github.encrypt.EncryptEnum.AseEnum; 10 | import com.github.encrypt.EncryptEnum.MDEnum; 11 | import com.github.encrypt.encryptImp.CipherEencryptImp; 12 | 13 | /** 14 | * AES加密工具类 15 | */ 16 | public class AesUtil extends CipherEencryptImp { 17 | public AesUtil(AseEnum defaultEncrypt) { 18 | this.defaultAlgorithm = defaultEncrypt == null ? AseEnum.CBC_NO_PADDING : defaultEncrypt; 19 | String slat = EncryptUtil.MD_ENCRYPT.encryptHex(EncryptUtil.AES_SLAT, EncryptUtil.AES_SLAT, MDEnum.MD5); 20 | this.configSlat = slat.substring(0, 16); 21 | this.configVectorKey = slat.substring(16); 22 | } 23 | // private final static Logger logger = LoggerFactory.getLogger(AesUtil.class); 24 | private final static String AES_ALGORITHM_NAME = "AES"; 25 | private final static int SLAT_KEY_LENGTH = 16; 26 | private final static int VECTOR_KEY_LENGTH = 16; 27 | 28 | @Override 29 | protected byte[] encrypt(String content, String slatKey, String vectorKey, AseEnum encryptType) throws Exception { 30 | byte[] encrypted = null; 31 | try { 32 | if (slatKey == null || slatKey.length() != SLAT_KEY_LENGTH) 33 | { 34 | throw new Exception("slatKey is null or slatKey is not at " + SLAT_KEY_LENGTH + "-bytes."); 35 | } 36 | if (encryptType == null) 37 | { 38 | throw new Exception("encryptType is null"); 39 | } 40 | Cipher cipher = Cipher.getInstance(encryptType.getEncryptType()); 41 | SecretKey secretKey = new SecretKeySpec(slatKey.getBytes(), AES_ALGORITHM_NAME); 42 | byte[] plaintext = null; 43 | if (AseEnum.CBC_NO_PADDING.equals(encryptType) || AseEnum.ECB_NO_PADDING.equals(encryptType)) 44 | { 45 | plaintext = handleNoPaddingEncryptFormat(cipher, content); 46 | } 47 | else 48 | { 49 | plaintext = content.getBytes(); 50 | } 51 | if (AseEnum.CBC_NO_PADDING.equals(encryptType) || AseEnum.CBC_PKCS5PADDING.equals(encryptType)) 52 | { 53 | if (vectorKey == null || vectorKey.length() != VECTOR_KEY_LENGTH) 54 | { 55 | throw new Exception("vectorKey is null or vectorKey is not at " + VECTOR_KEY_LENGTH + "-bytes."); 56 | } 57 | IvParameterSpec iv = new IvParameterSpec(vectorKey.getBytes()); 58 | cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); 59 | } 60 | else 61 | { 62 | cipher.init(Cipher.ENCRYPT_MODE, secretKey); 63 | } 64 | encrypted = cipher.doFinal(plaintext); 65 | } catch (Exception e) { 66 | // logger.error("Aes Util encryption failed, errors: {}", e.getMessage()); 67 | throw new Exception(encryptException); 68 | } 69 | return encrypted; 70 | } 71 | 72 | @Override 73 | protected String decrypt(byte[] content, String slatKey, String vectorKey, AseEnum encryptType) throws Exception { 74 | String result = null; 75 | try { 76 | if (slatKey == null || slatKey.length() != SLAT_KEY_LENGTH) 77 | { 78 | throw new Exception("slatKey is null or slatKey is not at " + SLAT_KEY_LENGTH + "-bytes."); 79 | } 80 | if (encryptType == null) 81 | { 82 | throw new Exception("encryptType is null"); 83 | } 84 | Cipher cipher = Cipher.getInstance(encryptType.getEncryptType()); 85 | SecretKey secretKey = new SecretKeySpec(slatKey.getBytes(), AES_ALGORITHM_NAME); 86 | if (AseEnum.CBC_NO_PADDING.equals(encryptType) || AseEnum.CBC_PKCS5PADDING.equals(encryptType)) 87 | { 88 | if (vectorKey == null || vectorKey.length() != VECTOR_KEY_LENGTH) 89 | { 90 | throw new Exception("vectorKey is null or vectorKey is not at " + VECTOR_KEY_LENGTH + "-bytes."); 91 | } 92 | IvParameterSpec iv = new IvParameterSpec(vectorKey.getBytes()); 93 | cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); 94 | } 95 | else 96 | { 97 | cipher.init(Cipher.DECRYPT_MODE, secretKey); 98 | } 99 | byte[] original = cipher.doFinal(content); 100 | String originalString = new String(original); 101 | result = originalString.trim(); 102 | } catch (Exception e) { 103 | // logger.error("Aes Util decryption failed, errors: {}", e.getMessage()); 104 | throw new Exception(decryptException); 105 | } 106 | return result; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptTypeImpl/DesUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptTypeImpl; 2 | 3 | import javax.crypto.Cipher; 4 | import javax.crypto.SecretKey; 5 | import javax.crypto.spec.IvParameterSpec; 6 | import javax.crypto.spec.SecretKeySpec; 7 | 8 | import com.github.encrypt.EncryptUtil; 9 | import com.github.encrypt.EncryptEnum.DecEnum; 10 | import com.github.encrypt.EncryptEnum.MDEnum; 11 | import com.github.encrypt.encryptImp.CipherEencryptImp; 12 | 13 | /** 14 | * DES加密工具类 15 | */ 16 | public class DesUtil extends CipherEencryptImp { 17 | public DesUtil(DecEnum defaultEncrypt) { 18 | this.defaultAlgorithm = defaultEncrypt == null ? DecEnum.CBC_NO_PADDING : defaultEncrypt; 19 | String slat = EncryptUtil.MD_ENCRYPT.encryptHex(EncryptUtil.DES_SLAT, EncryptUtil.DES_SLAT, MDEnum.MD5); 20 | this.configSlat = slat.substring(0, 8); 21 | this.configVectorKey = slat.substring(24); 22 | } 23 | // private final static Logger logger = LoggerFactory.getLogger(DesUtil.class); 24 | private final static int SLAT_KEY_LENGTH = 8; 25 | private final static int VECTOR_KEY_LENGTH = 8; 26 | private final static String DES_ALGORITHM_NAME = "DES"; 27 | 28 | @Override 29 | protected byte[] encrypt(String content, String slatKey, String vectorKey, DecEnum encryptType) throws Exception { 30 | byte[] encrypted = null; 31 | try { 32 | if (slatKey == null || slatKey.length() != SLAT_KEY_LENGTH) 33 | { 34 | throw new Exception("slatKey is null or slatKey is not at " + SLAT_KEY_LENGTH + "-bytes."); 35 | } 36 | if (encryptType == null) 37 | { 38 | throw new Exception("encryptType is null"); 39 | } 40 | Cipher cipher = Cipher.getInstance(encryptType.getEncryptType()); 41 | SecretKey secretKey = new SecretKeySpec(slatKey.getBytes(), DES_ALGORITHM_NAME); 42 | byte[] plaintext = null; 43 | if (DecEnum.CBC_NO_PADDING.equals(encryptType) || DecEnum.ECB_NO_PADDING.equals(encryptType)) 44 | { 45 | plaintext = handleNoPaddingEncryptFormat(cipher, content); 46 | } 47 | else 48 | { 49 | plaintext = content.getBytes(); 50 | } 51 | if (DecEnum.CBC_NO_PADDING.equals(encryptType) || DecEnum.CBC_PKCS5PADDING.equals(encryptType)) 52 | { 53 | if (vectorKey == null || vectorKey.length() != VECTOR_KEY_LENGTH) 54 | { 55 | throw new Exception("vectorKey is null or vectorKey is not at " + VECTOR_KEY_LENGTH + "-bytes."); 56 | } 57 | IvParameterSpec iv = new IvParameterSpec(vectorKey.getBytes()); 58 | cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); 59 | } 60 | else 61 | { 62 | cipher.init(Cipher.ENCRYPT_MODE, secretKey); 63 | } 64 | encrypted = cipher.doFinal(plaintext); 65 | } catch (Exception e) { 66 | // logger.error("Des Util encryption failed, errors: {}", e.getMessage()); 67 | throw new Exception(encryptException); 68 | } 69 | return encrypted; 70 | } 71 | 72 | @Override 73 | protected String decrypt(byte[] content, String slatKey, String vectorKey, DecEnum encryptType) throws Exception { 74 | String result = null; 75 | try { 76 | if (slatKey == null || slatKey.length() != SLAT_KEY_LENGTH) 77 | { 78 | throw new Exception("slatKey is null or slatKey is not at " + SLAT_KEY_LENGTH + "-bytes."); 79 | } 80 | if (encryptType == null) 81 | { 82 | throw new Exception("encryptType is null"); 83 | } 84 | Cipher cipher = Cipher.getInstance(encryptType.getEncryptType()); 85 | SecretKey secretKey = new SecretKeySpec(slatKey.getBytes(), DES_ALGORITHM_NAME); 86 | if (DecEnum.CBC_NO_PADDING.equals(encryptType) || DecEnum.CBC_PKCS5PADDING.equals(encryptType)) 87 | { 88 | if (vectorKey == null || vectorKey.length() != VECTOR_KEY_LENGTH) 89 | { 90 | throw new Exception("vectorKey is null or vectorKey is not at " + VECTOR_KEY_LENGTH + "-bytes."); 91 | } 92 | IvParameterSpec iv = new IvParameterSpec(vectorKey.getBytes()); 93 | cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); 94 | } 95 | else 96 | { 97 | cipher.init(Cipher.DECRYPT_MODE, secretKey); 98 | } 99 | byte[] original = cipher.doFinal(content); 100 | String originalString = new String(original); 101 | result = originalString.trim(); 102 | } catch (Exception e) { 103 | // logger.error("Des Util decryption failed, errors: {}", e.getMessage()); 104 | throw new Exception(decryptException); 105 | } 106 | return result; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptTypeImpl/Des3Util.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptTypeImpl; 2 | 3 | import javax.crypto.Cipher; 4 | import javax.crypto.SecretKey; 5 | import javax.crypto.spec.IvParameterSpec; 6 | import javax.crypto.spec.SecretKeySpec; 7 | 8 | import com.github.encrypt.EncryptUtil; 9 | import com.github.encrypt.EncryptEnum.Dec3Enum; 10 | import com.github.encrypt.EncryptEnum.MDEnum; 11 | import com.github.encrypt.encryptImp.CipherEencryptImp; 12 | 13 | /** 14 | * Des3加密工具类 15 | */ 16 | public class Des3Util extends CipherEencryptImp { 17 | public Des3Util(Dec3Enum defaultEncrypt) { 18 | this.defaultAlgorithm = defaultEncrypt == null ? Dec3Enum.CBC_NO_PADDING : defaultEncrypt; 19 | String slat = EncryptUtil.MD_ENCRYPT.encryptHex(EncryptUtil.DES3_SLAT, EncryptUtil.DES3_SLAT, MDEnum.MD5); 20 | this.configSlat = slat.substring(0, 24); 21 | this.configVectorKey = slat.substring(24); 22 | } 23 | // private final static Logger logger = LoggerFactory.getLogger(Des3Util.class); 24 | private final static String DESEDE_ALGORITHM_NAME = "DESede"; 25 | private final static int SLAT_KEY_LENGTH = 24; 26 | private final static int VECTOR_KEY_LENGTH = 8; 27 | 28 | @Override 29 | protected byte[] encrypt(String content, String slatKey, String vectorKey, Dec3Enum encryptType) throws Exception { 30 | byte[] encrypted = null; 31 | try { 32 | if (slatKey == null || slatKey.length() != SLAT_KEY_LENGTH) 33 | { 34 | throw new Exception("slatKey is null or slatKey is not at " + SLAT_KEY_LENGTH + "-bytes."); 35 | } 36 | if (encryptType == null) 37 | { 38 | throw new Exception("encryptType is null"); 39 | } 40 | Cipher cipher = Cipher.getInstance(encryptType.getEncryptType()); 41 | SecretKey secretKey = new SecretKeySpec(slatKey.getBytes(), DESEDE_ALGORITHM_NAME); 42 | byte[] plaintext = null; 43 | if (Dec3Enum.CBC_NO_PADDING.equals(encryptType) || Dec3Enum.ECB_NO_PADDING.equals(encryptType)) 44 | { 45 | plaintext = handleNoPaddingEncryptFormat(cipher, content); 46 | } 47 | else 48 | { 49 | plaintext = content.getBytes(); 50 | } 51 | if (Dec3Enum.CBC_NO_PADDING.equals(encryptType) || Dec3Enum.CBC_PKCS5PADDING.equals(encryptType)) 52 | { 53 | if (vectorKey == null || vectorKey.length() != VECTOR_KEY_LENGTH) 54 | { 55 | throw new Exception("vectorKey is null or vectorKey is not at " + VECTOR_KEY_LENGTH + "-bytes."); 56 | } 57 | IvParameterSpec iv = new IvParameterSpec(vectorKey.getBytes()); 58 | cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); 59 | } 60 | else 61 | { 62 | cipher.init(Cipher.ENCRYPT_MODE, secretKey); 63 | } 64 | encrypted = cipher.doFinal(plaintext); 65 | } catch (Exception e) { 66 | // logger.error("Des3 Util encryption failed, errors: {}", e.getMessage()); 67 | throw new Exception(encryptException); 68 | } 69 | return encrypted; 70 | } 71 | 72 | @Override 73 | protected String decrypt(byte[] content, String slatKey, String vectorKey, Dec3Enum encryptType) throws Exception { 74 | String result = null; 75 | try { 76 | if (slatKey == null || slatKey.length() != SLAT_KEY_LENGTH) 77 | { 78 | throw new Exception("slatKey is null or slatKey is not at " + SLAT_KEY_LENGTH + "-bytes."); 79 | } 80 | if (encryptType == null) 81 | { 82 | throw new Exception("encryptType is null"); 83 | } 84 | Cipher cipher = Cipher.getInstance(encryptType.getEncryptType()); 85 | SecretKey secretKey = new SecretKeySpec(slatKey.getBytes(), DESEDE_ALGORITHM_NAME); 86 | if (Dec3Enum.CBC_NO_PADDING.equals(encryptType) || Dec3Enum.CBC_PKCS5PADDING.equals(encryptType)) 87 | { 88 | if (vectorKey == null || vectorKey.length() != VECTOR_KEY_LENGTH) 89 | { 90 | throw new Exception("vectorKey is null or vectorKey is not at " + VECTOR_KEY_LENGTH + "-bytes."); 91 | } 92 | IvParameterSpec iv = new IvParameterSpec(vectorKey.getBytes()); 93 | cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); 94 | } 95 | else 96 | { 97 | cipher.init(Cipher.DECRYPT_MODE, secretKey); 98 | } 99 | byte[] original = cipher.doFinal(content); 100 | String originalString = new String(original); 101 | result = originalString.trim(); 102 | } catch (Exception e) { 103 | // logger.error("Des3 Util decryption failed, errors: {}", e.getMessage()); 104 | throw new Exception(decryptException); 105 | } 106 | return result; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptTypeImpl/RsaUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptTypeImpl; 2 | 3 | import java.security.KeyFactory; 4 | import java.security.KeyPair; 5 | import java.security.KeyPairGenerator; 6 | import java.security.SecureRandom; 7 | import java.security.interfaces.RSAPrivateKey; 8 | import java.security.interfaces.RSAPublicKey; 9 | import java.security.spec.PKCS8EncodedKeySpec; 10 | import java.security.spec.X509EncodedKeySpec; 11 | 12 | import javax.crypto.Cipher; 13 | 14 | import com.github.encrypt.EncryptUtil; 15 | import com.github.encrypt.EncryptEnum.RSAEnum; 16 | import com.github.encrypt.encryptImp.CipherEencryptImp; 17 | 18 | /** 19 | *

RSA加密工具类,使用公匙加密,私匙解密

20 | *

该工具类使用slatKey作为SecureRandom的随机种子来创建公匙和私匙,此情况下只需记录下slatKey即可

21 | *

若需要使用 new SecureRandom() 来创建公匙和私匙,则创建公匙和私匙时,需要记录下公匙和私匙

22 | *

RSA加密算法,一般有:1)使用公匙加密,需要使用私匙解密;2)使用私匙加密,需要使用公匙解密

23 | */ 24 | public class RsaUtil extends CipherEencryptImp { 25 | public RsaUtil(RSAEnum defaultEncrypt) { 26 | this.defaultAlgorithm = defaultEncrypt; 27 | this.configSlat = EncryptUtil.RSA_SLAT; 28 | } 29 | // private final static Logger logger = LoggerFactory.getLogger(RsaUtil.class); 30 | private final static String RSA_ALGORITHM_NAME = "RSA"; 31 | private final static String SECURE_RANDOM_ALGORITHM = "SHA1PRNG"; 32 | private final static int SLAT_KEY_LENGTH = 1024;// or 2048, 2048加密比1024强 33 | @Override 34 | protected byte[] encrypt(String content, String slatKey, String vectorKey, RSAEnum encryptType) throws Exception { 35 | byte[] encrypted = null; 36 | try { 37 | if (slatKey == null) 38 | { 39 | throw new Exception("slatKey is null"); 40 | } 41 | if (encryptType == null) 42 | { 43 | throw new Exception("encryptType is null"); 44 | } 45 | Cipher cipher = Cipher.getInstance(encryptType.getEncryptType()); 46 | byte[] decoded = getPublicKey(slatKey); 47 | RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(RSA_ALGORITHM_NAME).generatePublic(new X509EncodedKeySpec(decoded)); 48 | cipher.init(Cipher.ENCRYPT_MODE, pubKey); 49 | encrypted = cipher.doFinal(content.getBytes()); 50 | } catch (Exception e) { 51 | // logger.error("Aes Util encryption failed, errors: {}", e.getMessage()); 52 | throw new Exception(encryptException); 53 | } 54 | return encrypted; 55 | } 56 | 57 | @Override 58 | protected String decrypt(byte[] content, String slatKey, String vectorKey, RSAEnum encryptType) throws Exception { 59 | String result = null; 60 | try { 61 | if (slatKey == null) 62 | { 63 | throw new Exception("slatKey is null"); 64 | } 65 | if (encryptType == null) 66 | { 67 | throw new Exception("encryptType is null"); 68 | } 69 | Cipher cipher = Cipher.getInstance(encryptType.getEncryptType()); 70 | byte[] decoded = getPrivateKey(slatKey); 71 | RSAPrivateKey pubKey = (RSAPrivateKey) KeyFactory.getInstance(RSA_ALGORITHM_NAME).generatePrivate(new PKCS8EncodedKeySpec(decoded)); 72 | cipher.init(Cipher.DECRYPT_MODE, pubKey); 73 | byte[] original = cipher.doFinal(content); 74 | result = new String(original).trim(); 75 | } catch (Exception e) { 76 | // logger.error("Aes Util encryption failed, errors: {}", e.getMessage()); 77 | throw new Exception(encryptException); 78 | } 79 | return result; 80 | } 81 | 82 | /** 83 | * 根据slatKey获取公匙,传入的slatKey作为SecureRandom的随机种子 84 | * 若使用new SecureRandom()创建公匙,则需要记录下私匙,解密时使用 85 | */ 86 | private static byte[] getPublicKey(String slatKey) throws Exception { 87 | KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM_NAME); 88 | SecureRandom random = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM); 89 | random.setSeed(slatKey.getBytes()); 90 | keyPairGenerator.initialize(SLAT_KEY_LENGTH, random); 91 | KeyPair keyPair = keyPairGenerator.generateKeyPair(); 92 | return keyPair.getPublic().getEncoded(); 93 | } 94 | 95 | /** 96 | * 根据slatKey获取私匙,传入的slatKey作为SecureRandom的随机种子 97 | */ 98 | private static byte[] getPrivateKey(String slatKey) throws Exception { 99 | KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM_NAME); 100 | SecureRandom random = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM); 101 | random.setSeed(slatKey.getBytes()); 102 | keyPairGenerator.initialize(SLAT_KEY_LENGTH, random); 103 | KeyPair keyPair = keyPairGenerator.generateKeyPair(); 104 | return keyPair.getPrivate().getEncoded(); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptTypeImpl/AesKgenUtil.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptTypeImpl; 2 | 3 | import java.security.Key; 4 | import java.security.SecureRandom; 5 | 6 | import javax.crypto.Cipher; 7 | import javax.crypto.KeyGenerator; 8 | import javax.crypto.spec.IvParameterSpec; 9 | 10 | import com.github.encrypt.EncryptUtil; 11 | import com.github.encrypt.EncryptEnum.AseEnum; 12 | import com.github.encrypt.encryptImp.CipherEencryptImp; 13 | 14 | 15 | /** 16 | * AES加密实现,key使用类KeyGenerator获取,与使用new SecretKeySpec()获取key的优点是传入的key长度可以是任意的,不需要固定16位byte 17 | * 18 | * 若出现 异常:Given final block not properly padded. Such issues can arise if a bad key is used during decryption. 19 | * 属于解密失败,原因可能是传入的slatKey或者vectorKey与加密时使用的不一致 20 | */ 21 | public class AesKgenUtil extends CipherEencryptImp { 22 | public AesKgenUtil(AseEnum defaultEncrypt) { 23 | this.defaultAlgorithm = defaultEncrypt == null ? AseEnum.CBC_NO_PADDING : defaultEncrypt; 24 | this.configSlat = EncryptUtil.AES_SLAT; 25 | this.configVectorKey = EncryptUtil.AES_SLAT; 26 | } 27 | 28 | // private final static Logger logger = LoggerFactory.getLogger(AesKgenUtil.class); 29 | private final static String AES_ALGORITHM_NAME = "AES"; 30 | private final static String SECURE_RANDOM_ALGORITHM = "SHA1PRNG"; 31 | private final static int SLAT_KEY_LENGTH = 128; 32 | private final static int VECTOR_KEY_LENGTH = 128; 33 | 34 | @Override 35 | protected byte[] encrypt(String content, String slatKey, String vectorKey, AseEnum encryptType) throws Exception { 36 | byte[] encrypted = null; 37 | try { 38 | if (slatKey == null) 39 | { 40 | throw new Exception("slatKey is null"); 41 | } 42 | if (encryptType == null) 43 | { 44 | throw new Exception("encryptType is null"); 45 | } 46 | Cipher cipher = Cipher.getInstance(encryptType.getEncryptType()); 47 | Key key = getSlatKey(slatKey); 48 | byte[] plaintext = null; 49 | if (AseEnum.CBC_NO_PADDING.equals(encryptType) || AseEnum.ECB_NO_PADDING.equals(encryptType)) 50 | { 51 | plaintext = handleNoPaddingEncryptFormat(cipher, content); 52 | } else { 53 | plaintext = content.getBytes(); 54 | } 55 | if (AseEnum.CBC_NO_PADDING.equals(encryptType) || AseEnum.CBC_PKCS5PADDING.equals(encryptType)) 56 | { 57 | if (vectorKey == null) 58 | { 59 | throw new Exception("vectorKey is null"); 60 | } 61 | IvParameterSpec iv = getVectorKey(vectorKey); 62 | cipher.init(Cipher.ENCRYPT_MODE, key, iv); 63 | } 64 | else 65 | { 66 | cipher.init(Cipher.ENCRYPT_MODE, key); 67 | } 68 | encrypted = cipher.doFinal(plaintext); 69 | } catch (Exception e) { 70 | // logger.error("Aes Util encryption failed, errors: {}", e.getMessage()); 71 | throw new Exception(encryptException); 72 | } 73 | return encrypted; 74 | } 75 | 76 | @Override 77 | protected String decrypt(byte[] content, String slatKey, String vectorKey, AseEnum encryptType) throws Exception { 78 | String result = null; 79 | try { 80 | if (slatKey == null) 81 | { 82 | throw new Exception("slatKey is null"); 83 | } 84 | if (encryptType == null) 85 | { 86 | throw new Exception("encryptType is null"); 87 | } 88 | Cipher cipher = Cipher.getInstance(encryptType.getEncryptType()); 89 | Key key = getSlatKey(slatKey); 90 | if (AseEnum.CBC_NO_PADDING.equals(encryptType) || AseEnum.CBC_PKCS5PADDING.equals(encryptType)) 91 | { 92 | if (vectorKey == null) 93 | { 94 | throw new Exception("vectorKey is null"); 95 | } 96 | IvParameterSpec iv = getVectorKey(vectorKey); 97 | cipher.init(Cipher.DECRYPT_MODE, key, iv); 98 | } 99 | else 100 | { 101 | cipher.init(Cipher.DECRYPT_MODE, key); 102 | } 103 | byte[] original = cipher.doFinal(content); 104 | result = new String(original).trim(); 105 | } catch (Exception e) { 106 | // logger.error("Aes Util decryption failed, errors: {}", e.getMessage()); 107 | throw new Exception(decryptException); 108 | } 109 | return result; 110 | } 111 | 112 | /** 113 | * 获取加密的密匙,传入的slatKey可以是任意长度的,作为SecureRandom的随机种子, 114 | * 而在KeyGenerator初始化时设置密匙的长度128bit(16位byte) 115 | */ 116 | private static Key getSlatKey(String slatKey) throws Exception { 117 | KeyGenerator kgen = KeyGenerator.getInstance(AES_ALGORITHM_NAME); 118 | SecureRandom random = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM); 119 | random.setSeed(slatKey.getBytes()); 120 | kgen.init(SLAT_KEY_LENGTH, random); 121 | Key key = kgen.generateKey(); 122 | return key; 123 | } 124 | 125 | /** 126 | * 获取加密的向量 127 | */ 128 | private static IvParameterSpec getVectorKey(String vectorKey) throws Exception { 129 | KeyGenerator kgen = KeyGenerator.getInstance(AES_ALGORITHM_NAME); 130 | SecureRandom random = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM); 131 | random.setSeed(vectorKey.getBytes()); 132 | kgen.init(VECTOR_KEY_LENGTH, random); 133 | IvParameterSpec iv = new IvParameterSpec(kgen.generateKey().getEncoded()); 134 | return iv; 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/com/github/encrypt/encryptImp/CipherEencryptImp.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt.encryptImp; 2 | 3 | import java.nio.charset.Charset; 4 | 5 | import javax.crypto.Cipher; 6 | 7 | import org.apache.commons.codec.binary.Base64; 8 | 9 | import com.github.encrypt.encryptTypeImpl.HexUtil; 10 | 11 | /** 12 | * 使用Cipher进行加密 13 | */ 14 | public abstract class CipherEencryptImp> implements ICipherEncrypt{ 15 | /** 16 | * 若加密失败, 抛出该异常信息 17 | */ 18 | protected static final String encryptException = "Encrypt Error!"; 19 | /** 20 | * 若解密失败, 抛出该异常信息 21 | */ 22 | protected static final String decryptException = "Decrypt Error!"; 23 | /** 24 | * 配置文件配置的盐 25 | */ 26 | protected String configSlat = null; 27 | /** 28 | * 配置文件配置的向量 29 | */ 30 | protected String configVectorKey = null; 31 | /** 32 | * 默认加密模式 33 | */ 34 | protected E defaultAlgorithm = null; 35 | 36 | /** 37 | * 加密, 若直接将返回结果转为String类型, 为乱码, 可以转为Base64或转为16进制 38 | * @param content : 加密内容 39 | * @param slatKey : 加密的盐 40 | * @param vectorKey : 加密向量 41 | * @param encryptType : 加密模式 42 | * @return 密文(byte[]) 43 | * @throws Exception 44 | */ 45 | protected abstract byte[] encrypt(String content, String slatKey, String vectorKey, E encryptType) throws Exception; 46 | /** 47 | * 解密 48 | * @param content : 解密内容 49 | * @param slatKey : 解密的盐 50 | * @param vectorKey : 解密向量 51 | * @param encryptType : 解密模式 52 | * @return 明文密码(String) 53 | * @throws Exception 54 | */ 55 | protected abstract String decrypt(byte[] content, String slatKey, String vectorKey, E encryptType) throws Exception; 56 | 57 | protected static byte[] handleNoPaddingEncryptFormat(Cipher cipher, String content) throws Exception { 58 | return handleNoPaddingEncryptFormat(cipher, content, Charset.defaultCharset()); 59 | } 60 | 61 | /** 62 | *

NoPadding加密模式, 加密内容必须是 8byte的倍数, 不足8位则末位补足0

63 | *

加密算法不提供该补码方式, 需要代码完成该补码方式

64 | * @param cipher 65 | * @param content :加密内容 66 | * @Param charset :指定的字符集 67 | * @return 符合加密的内容(byte[]) 68 | */ 69 | protected static byte[] handleNoPaddingEncryptFormat(Cipher cipher, String content, Charset charset) throws Exception { 70 | int blockSize = cipher.getBlockSize(); 71 | byte[] srawt = content.getBytes(charset); 72 | int plaintextLength = srawt.length; 73 | if (plaintextLength % blockSize != 0) { 74 | plaintextLength = plaintextLength + (blockSize - plaintextLength % blockSize); 75 | } 76 | byte[] plaintext = new byte[plaintextLength]; 77 | System.arraycopy(srawt, 0, plaintext, 0, srawt.length); 78 | return plaintext; 79 | } 80 | 81 | @Override 82 | public String encryptBase64(String content) throws Exception { 83 | return encryptBase64(content, configSlat, configVectorKey, defaultAlgorithm); 84 | } 85 | @Override 86 | public String decryptBase64(String content) throws Exception { 87 | return decryptBase64(content, configSlat, configVectorKey, defaultAlgorithm); 88 | } 89 | @Override 90 | public String encryptBase64(String content, String slatKey, String vectorKey) throws Exception { 91 | return encryptBase64(content, slatKey, vectorKey, defaultAlgorithm); 92 | } 93 | @Override 94 | public String decryptBase64(String content, String slatKey, String vectorKey) throws Exception { 95 | return decryptBase64(content, slatKey, vectorKey, defaultAlgorithm); 96 | } 97 | @Override 98 | public String encryptBase64(String content, E encryptType) throws Exception { 99 | return encryptBase64(content, configSlat, configVectorKey, encryptType); 100 | } 101 | @Override 102 | public String decryptBase64(String content, E encryptType) throws Exception { 103 | return decryptBase64(content, configSlat, configVectorKey, encryptType); 104 | } 105 | @Override 106 | public String encryptBase64(String content, String slatKey, String vectorKey, E encryptType) throws Exception { 107 | byte[] result = encrypt(content, slatKey, vectorKey, encryptType); 108 | return Base64.encodeBase64String(result); 109 | } 110 | @Override 111 | public String decryptBase64(String content, String slatKey, String vectorKey, E encryptType) throws Exception { 112 | byte[] byteContent = Base64.decodeBase64(content); 113 | return decrypt(byteContent, slatKey, vectorKey, encryptType); 114 | } 115 | @Override 116 | public String encryptHex(String content) throws Exception { 117 | return encryptHex(content, configSlat, configVectorKey, defaultAlgorithm); 118 | } 119 | @Override 120 | public String decryptHex(String content) throws Exception { 121 | return decryptHex(content, configSlat, configVectorKey, defaultAlgorithm); 122 | } 123 | @Override 124 | public String encryptHex(String content, String slatKey, String vectorKey) throws Exception { 125 | return encryptHex(content, slatKey, vectorKey, defaultAlgorithm); 126 | } 127 | @Override 128 | public String decryptHex(String content, String slatKey, String vectorKey) throws Exception { 129 | return decryptHex(content, slatKey, vectorKey, defaultAlgorithm); 130 | } 131 | @Override 132 | public String encryptHex(String content, E encryptType) throws Exception { 133 | return encryptHex(content, configSlat, configVectorKey, encryptType); 134 | } 135 | @Override 136 | public String decryptHex(String content, E encryptType) throws Exception { 137 | return decryptHex(content, configSlat, configVectorKey, encryptType); 138 | } 139 | @Override 140 | public String encryptHex(String content, String slatKey, String vectorKey, E encryptType) throws Exception { 141 | byte[] result = encrypt(content, slatKey, vectorKey, encryptType); 142 | return HexUtil.byteArrayToHexStr(result); 143 | } 144 | @Override 145 | public String decryptHex(String content, String slatKey, String vectorKey, E encryptType) throws Exception { 146 | byte[] byteContent = HexUtil.hexStrToByteArray(content); 147 | return decrypt(byteContent, slatKey, vectorKey, encryptType); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/com/github/encrypt/EncryptTest.java: -------------------------------------------------------------------------------- 1 | package com.github.encrypt; 2 | 3 | import com.github.encrypt.EncryptEnum.AseEnum; 4 | import com.github.encrypt.EncryptEnum.Dec3Enum; 5 | import com.github.encrypt.EncryptEnum.DecEnum; 6 | import com.github.encrypt.EncryptEnum.MDEnum; 7 | import com.github.encrypt.EncryptEnum.RSAEnum; 8 | import com.github.encrypt.EncryptEnum.ShaEnum; 9 | 10 | /** 11 | * 测试 12 | */ 13 | public class EncryptTest { 14 | public static void main(String[] args) throws Exception { 15 | // mdTest(); 16 | // shaTest(); 17 | // desBaseTest(); 18 | // desHexTest(); 19 | // des3BaseTest(); 20 | // desHexTest(); 21 | // aesBaseTest(); 22 | // aesHexTest(); 23 | // aesKGngBaseTest(); 24 | // aesKGngHexTest(); 25 | // rsaBaseTest(); 26 | rsaHexTest(); 27 | } 28 | 29 | public static void mdTest() { 30 | System.out.println(EncryptUtil.MD_ENCRYPT.encryptBase64("123456")); 31 | System.out.println(EncryptUtil.MD_ENCRYPT.encryptBase64("123456", MDEnum.MD2)); 32 | System.out.println(EncryptUtil.MD_ENCRYPT.encryptBase64("123456", MDEnum.MD5)); 33 | System.out.println(EncryptUtil.MD_ENCRYPT.encryptBase64("123456", "password")); 34 | System.out.println(EncryptUtil.MD_ENCRYPT.encryptBase64("123456", "password", MDEnum.MD5)); 35 | 36 | System.out.println(EncryptUtil.MD_ENCRYPT.encryptHex("123456")); 37 | System.out.println(EncryptUtil.MD_ENCRYPT.encryptHex("123456", MDEnum.MD2)); 38 | System.out.println(EncryptUtil.MD_ENCRYPT.encryptHex("123456", MDEnum.MD5)); 39 | System.out.println(EncryptUtil.MD_ENCRYPT.encryptHex("123456", "password")); 40 | System.out.println(EncryptUtil.MD_ENCRYPT.encryptHex("123456", "password", MDEnum.MD5)); 41 | } 42 | 43 | public static void shaTest() { 44 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptBase64("123456")); 45 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptBase64("123456", ShaEnum.SHA1)); 46 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptBase64("123456", ShaEnum.SHA224)); 47 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptBase64("123456", ShaEnum.SHA256)); 48 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptBase64("123456", ShaEnum.SHA384)); 49 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptBase64("123456", ShaEnum.SHA512)); 50 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptBase64("123456", "password")); 51 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptBase64("123456", "password", ShaEnum.SHA256)); 52 | 53 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptHex("123456")); 54 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptHex("123456", ShaEnum.SHA1)); 55 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptHex("123456", ShaEnum.SHA224)); 56 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptHex("123456", ShaEnum.SHA256)); 57 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptHex("123456", ShaEnum.SHA384)); 58 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptHex("123456", ShaEnum.SHA512)); 59 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptHex("123456", "password")); 60 | System.out.println(EncryptUtil.SHA_ENCRYPT.encryptHex("123456", "password", ShaEnum.SHA256)); 61 | } 62 | 63 | public static void desBaseTest() throws Exception { 64 | String s1 = EncryptUtil.DES_ENCRYPT.encryptBase64("123456"); 65 | System.out.println(s1); 66 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptBase64(s1)); 67 | 68 | String s2 = EncryptUtil.DES_ENCRYPT.encryptBase64("123456", "password", "passw0rd"); 69 | System.out.println(s2); 70 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptBase64(s2, "password", "passw0rd")); 71 | 72 | String s3 = EncryptUtil.DES_ENCRYPT.encryptBase64("123456", DecEnum.CBC_NO_PADDING); 73 | String s4 = EncryptUtil.DES_ENCRYPT.encryptBase64("123456", DecEnum.CBC_PKCS5PADDING); 74 | String s5 = EncryptUtil.DES_ENCRYPT.encryptBase64("123456", DecEnum.ECB_NO_PADDING); 75 | String s6 = EncryptUtil.DES_ENCRYPT.encryptBase64("123456", DecEnum.ECB_PKCS5PADDING); 76 | System.out.println(s3); 77 | System.out.println(s4); 78 | System.out.println(s5); 79 | System.out.println(s6); 80 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptBase64(s3, DecEnum.CBC_NO_PADDING)); 81 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptBase64(s4, DecEnum.CBC_PKCS5PADDING)); 82 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptBase64(s5, DecEnum.ECB_NO_PADDING)); 83 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptBase64(s6, DecEnum.ECB_PKCS5PADDING)); 84 | 85 | String s7 = EncryptUtil.DES_ENCRYPT.encryptBase64("123456", "password", "passw0rd", DecEnum.CBC_NO_PADDING); 86 | String s8 = EncryptUtil.DES_ENCRYPT.encryptBase64("123456", "password", "passw0rd", DecEnum.CBC_PKCS5PADDING); 87 | String s9 = EncryptUtil.DES_ENCRYPT.encryptBase64("123456", "password", null, DecEnum.ECB_NO_PADDING); 88 | String s10 = EncryptUtil.DES_ENCRYPT.encryptBase64("123456", "password", null, DecEnum.ECB_PKCS5PADDING); 89 | System.out.println(s7); 90 | System.out.println(s8); 91 | System.out.println(s9); 92 | System.out.println(s10); 93 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptBase64(s7, "password", "passw0rd", DecEnum.CBC_NO_PADDING)); 94 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptBase64(s8, "password", "passw0rd", DecEnum.CBC_PKCS5PADDING)); 95 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptBase64(s9, "password", null, DecEnum.ECB_NO_PADDING)); 96 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptBase64(s10, "password", null, DecEnum.ECB_PKCS5PADDING)); 97 | } 98 | 99 | public static void desHexTest() throws Exception { 100 | String s1 = EncryptUtil.DES_ENCRYPT.encryptHex("123456"); 101 | System.out.println(s1); 102 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptHex(s1)); 103 | 104 | String s2 = EncryptUtil.DES_ENCRYPT.encryptHex("123456", "password", "passw0rd"); 105 | System.out.println(s2); 106 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptHex(s2, "password", "passw0rd")); 107 | 108 | String s3 = EncryptUtil.DES_ENCRYPT.encryptHex("123456", DecEnum.CBC_NO_PADDING); 109 | String s4 = EncryptUtil.DES_ENCRYPT.encryptHex("123456", DecEnum.CBC_PKCS5PADDING); 110 | String s5 = EncryptUtil.DES_ENCRYPT.encryptHex("123456", DecEnum.ECB_NO_PADDING); 111 | String s6 = EncryptUtil.DES_ENCRYPT.encryptHex("123456", DecEnum.ECB_PKCS5PADDING); 112 | System.out.println(s3); 113 | System.out.println(s4); 114 | System.out.println(s5); 115 | System.out.println(s6); 116 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptHex(s3, DecEnum.CBC_NO_PADDING)); 117 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptHex(s4, DecEnum.CBC_PKCS5PADDING)); 118 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptHex(s5, DecEnum.ECB_NO_PADDING)); 119 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptHex(s6, DecEnum.ECB_PKCS5PADDING)); 120 | 121 | String s7 = EncryptUtil.DES_ENCRYPT.encryptHex("123456", "password", "passw0rd", DecEnum.CBC_NO_PADDING); 122 | String s8 = EncryptUtil.DES_ENCRYPT.encryptHex("123456", "password", "passw0rd", DecEnum.CBC_PKCS5PADDING); 123 | String s9 = EncryptUtil.DES_ENCRYPT.encryptHex("123456", "password", null, DecEnum.ECB_NO_PADDING); 124 | String s10 = EncryptUtil.DES_ENCRYPT.encryptHex("123456", "password", null, DecEnum.ECB_PKCS5PADDING); 125 | System.out.println(s7); 126 | System.out.println(s8); 127 | System.out.println(s9); 128 | System.out.println(s10); 129 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptHex(s7, "password", "passw0rd", DecEnum.CBC_NO_PADDING)); 130 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptHex(s8, "password", "passw0rd", DecEnum.CBC_PKCS5PADDING)); 131 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptHex(s9, "password", null, DecEnum.ECB_NO_PADDING)); 132 | System.out.println(EncryptUtil.DES_ENCRYPT.decryptHex(s10, "password", null, DecEnum.ECB_PKCS5PADDING)); 133 | } 134 | 135 | public static void des3BaseTest() throws Exception { 136 | String s1 = EncryptUtil.DES3_ENCRYPT.encryptBase64("123456"); 137 | System.out.println(s1); 138 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptBase64(s1)); 139 | 140 | String s2 = EncryptUtil.DES3_ENCRYPT.encryptBase64("123456", "passwordpasswordpassword", "passw0rd"); 141 | System.out.println(s2); 142 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptBase64(s2, "passwordpasswordpassword", "passw0rd")); 143 | 144 | String s3 = EncryptUtil.DES3_ENCRYPT.encryptBase64("123456", Dec3Enum.CBC_NO_PADDING); 145 | String s4 = EncryptUtil.DES3_ENCRYPT.encryptBase64("123456", Dec3Enum.CBC_PKCS5PADDING); 146 | String s5 = EncryptUtil.DES3_ENCRYPT.encryptBase64("123456", Dec3Enum.ECB_NO_PADDING); 147 | String s6 = EncryptUtil.DES3_ENCRYPT.encryptBase64("123456", Dec3Enum.ECB_PKCS5PADDING); 148 | System.out.println(s3); 149 | System.out.println(s4); 150 | System.out.println(s5); 151 | System.out.println(s6); 152 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptBase64(s3, Dec3Enum.CBC_NO_PADDING)); 153 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptBase64(s4, Dec3Enum.CBC_PKCS5PADDING)); 154 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptBase64(s5, Dec3Enum.ECB_NO_PADDING)); 155 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptBase64(s6, Dec3Enum.ECB_PKCS5PADDING)); 156 | 157 | String s7 = EncryptUtil.DES3_ENCRYPT.encryptBase64("123456", "passwordpasswordpassword", "passw0rd", Dec3Enum.CBC_NO_PADDING); 158 | String s8 = EncryptUtil.DES3_ENCRYPT.encryptBase64("123456", "passwordpasswordpassword", "passw0rd", Dec3Enum.CBC_PKCS5PADDING); 159 | String s9 = EncryptUtil.DES3_ENCRYPT.encryptBase64("123456", "passwordpasswordpassword", null, Dec3Enum.ECB_NO_PADDING); 160 | String s10 = EncryptUtil.DES3_ENCRYPT.encryptBase64("123456", "passwordpasswordpassword", null, Dec3Enum.ECB_PKCS5PADDING); 161 | System.out.println(s7); 162 | System.out.println(s8); 163 | System.out.println(s9); 164 | System.out.println(s10); 165 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptBase64(s7, "passwordpasswordpassword", "passw0rd", Dec3Enum.CBC_NO_PADDING)); 166 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptBase64(s8, "passwordpasswordpassword", "passw0rd", Dec3Enum.CBC_PKCS5PADDING)); 167 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptBase64(s9, "passwordpasswordpassword", null, Dec3Enum.ECB_NO_PADDING)); 168 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptBase64(s10, "passwordpasswordpassword", null, Dec3Enum.ECB_PKCS5PADDING)); 169 | } 170 | 171 | public static void des3HexTest() throws Exception { 172 | String s1 = EncryptUtil.DES3_ENCRYPT.encryptHex("123456"); 173 | System.out.println(s1); 174 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptHex(s1)); 175 | 176 | String s2 = EncryptUtil.DES3_ENCRYPT.encryptHex("123456", "passwordpasswordpassword", "passw0rd"); 177 | System.out.println(s2); 178 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptHex(s2, "passwordpasswordpassword", "passw0rd")); 179 | 180 | String s3 = EncryptUtil.DES3_ENCRYPT.encryptHex("123456", Dec3Enum.CBC_NO_PADDING); 181 | String s4 = EncryptUtil.DES3_ENCRYPT.encryptHex("123456", Dec3Enum.CBC_PKCS5PADDING); 182 | String s5 = EncryptUtil.DES3_ENCRYPT.encryptHex("123456", Dec3Enum.ECB_NO_PADDING); 183 | String s6 = EncryptUtil.DES3_ENCRYPT.encryptHex("123456", Dec3Enum.ECB_PKCS5PADDING); 184 | System.out.println(s3); 185 | System.out.println(s4); 186 | System.out.println(s5); 187 | System.out.println(s6); 188 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptHex(s3, Dec3Enum.CBC_NO_PADDING)); 189 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptHex(s4, Dec3Enum.CBC_PKCS5PADDING)); 190 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptHex(s5, Dec3Enum.ECB_NO_PADDING)); 191 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptHex(s6, Dec3Enum.ECB_PKCS5PADDING)); 192 | 193 | String s7 = EncryptUtil.DES3_ENCRYPT.encryptHex("123456", "passwordpasswordpassword", "passw0rd", Dec3Enum.CBC_NO_PADDING); 194 | String s8 = EncryptUtil.DES3_ENCRYPT.encryptHex("123456", "passwordpasswordpassword", "passw0rd", Dec3Enum.CBC_PKCS5PADDING); 195 | String s9 = EncryptUtil.DES3_ENCRYPT.encryptHex("123456", "passwordpasswordpassword", null, Dec3Enum.ECB_NO_PADDING); 196 | String s10 = EncryptUtil.DES3_ENCRYPT.encryptHex("123456", "passwordpasswordpassword", null, Dec3Enum.ECB_PKCS5PADDING); 197 | System.out.println(s7); 198 | System.out.println(s8); 199 | System.out.println(s9); 200 | System.out.println(s10); 201 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptHex(s7, "passwordpasswordpassword", "passw0rd", Dec3Enum.CBC_NO_PADDING)); 202 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptHex(s8, "passwordpasswordpassword", "passw0rd", Dec3Enum.CBC_PKCS5PADDING)); 203 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptHex(s9, "passwordpasswordpassword", null, Dec3Enum.ECB_NO_PADDING)); 204 | System.out.println(EncryptUtil.DES3_ENCRYPT.decryptHex(s10, "passwordpasswordpassword", null, Dec3Enum.ECB_PKCS5PADDING)); 205 | } 206 | 207 | public static void aesBaseTest() throws Exception { 208 | String s1 = EncryptUtil.AES_ENCRYPT.encryptBase64("123456"); 209 | System.out.println(s1); 210 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptBase64(s1)); 211 | 212 | String s2 = EncryptUtil.AES_ENCRYPT.encryptBase64("123456", "passwordpassword", "passw0rdpassw0rd"); 213 | System.out.println(s2); 214 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptBase64(s2, "passwordpassword", "passw0rdpassw0rd")); 215 | 216 | String s3 = EncryptUtil.AES_ENCRYPT.encryptBase64("123456", AseEnum.CBC_NO_PADDING); 217 | String s4 = EncryptUtil.AES_ENCRYPT.encryptBase64("123456", AseEnum.CBC_PKCS5PADDING); 218 | String s5 = EncryptUtil.AES_ENCRYPT.encryptBase64("123456", AseEnum.ECB_NO_PADDING); 219 | String s6 = EncryptUtil.AES_ENCRYPT.encryptBase64("123456", AseEnum.ECB_PKCS5PADDING); 220 | System.out.println(s3); 221 | System.out.println(s4); 222 | System.out.println(s5); 223 | System.out.println(s6); 224 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptBase64(s3, AseEnum.CBC_NO_PADDING)); 225 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptBase64(s4, AseEnum.CBC_PKCS5PADDING)); 226 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptBase64(s5, AseEnum.ECB_NO_PADDING)); 227 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptBase64(s6, AseEnum.ECB_PKCS5PADDING)); 228 | 229 | String s7 = EncryptUtil.AES_ENCRYPT.encryptBase64("123456", "passwordpassword", "passw0rdpassw0rd", AseEnum.CBC_NO_PADDING); 230 | String s8 = EncryptUtil.AES_ENCRYPT.encryptBase64("123456", "passwordpassword", "passw0rdpassw0rd", AseEnum.CBC_PKCS5PADDING); 231 | String s9 = EncryptUtil.AES_ENCRYPT.encryptBase64("123456", "passwordpassword", null, AseEnum.ECB_NO_PADDING); 232 | String s10 = EncryptUtil.AES_ENCRYPT.encryptBase64("123456", "passwordpassword", null, AseEnum.ECB_PKCS5PADDING); 233 | System.out.println(s7); 234 | System.out.println(s8); 235 | System.out.println(s9); 236 | System.out.println(s10); 237 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptBase64(s7, "passwordpassword", "passw0rdpassw0rd", AseEnum.CBC_NO_PADDING)); 238 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptBase64(s8, "passwordpassword", "passw0rdpassw0rd", AseEnum.CBC_PKCS5PADDING)); 239 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptBase64(s9, "passwordpassword", null, AseEnum.ECB_NO_PADDING)); 240 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptBase64(s10, "passwordpassword", null, AseEnum.ECB_PKCS5PADDING)); 241 | } 242 | 243 | public static void aesHexTest() throws Exception { 244 | String s1 = EncryptUtil.AES_ENCRYPT.encryptHex("123456"); 245 | System.out.println(s1); 246 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptHex(s1)); 247 | 248 | String s2 = EncryptUtil.AES_ENCRYPT.encryptHex("123456", "passwordpassword", "passw0rdpassw0rd"); 249 | System.out.println(s2); 250 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptHex(s2, "passwordpassword", "passw0rdpassw0rd")); 251 | 252 | String s3 = EncryptUtil.AES_ENCRYPT.encryptHex("123456", AseEnum.CBC_NO_PADDING); 253 | String s4 = EncryptUtil.AES_ENCRYPT.encryptHex("123456", AseEnum.CBC_PKCS5PADDING); 254 | String s5 = EncryptUtil.AES_ENCRYPT.encryptHex("123456", AseEnum.ECB_NO_PADDING); 255 | String s6 = EncryptUtil.AES_ENCRYPT.encryptHex("123456", AseEnum.ECB_PKCS5PADDING); 256 | System.out.println(s3); 257 | System.out.println(s4); 258 | System.out.println(s5); 259 | System.out.println(s6); 260 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptHex(s3, AseEnum.CBC_NO_PADDING)); 261 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptHex(s4, AseEnum.CBC_PKCS5PADDING)); 262 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptHex(s5, AseEnum.ECB_NO_PADDING)); 263 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptHex(s6, AseEnum.ECB_PKCS5PADDING)); 264 | 265 | String s7 = EncryptUtil.AES_ENCRYPT.encryptHex("123456", "passwordpassword", "passw0rdpassw0rd", AseEnum.CBC_NO_PADDING); 266 | String s8 = EncryptUtil.AES_ENCRYPT.encryptHex("123456", "passwordpassword", "passw0rdpassw0rd", AseEnum.CBC_PKCS5PADDING); 267 | String s9 = EncryptUtil.AES_ENCRYPT.encryptHex("123456", "passwordpassword", null, AseEnum.ECB_NO_PADDING); 268 | String s10 = EncryptUtil.AES_ENCRYPT.encryptHex("123456", "passwordpassword", null, AseEnum.ECB_PKCS5PADDING); 269 | System.out.println(s7); 270 | System.out.println(s8); 271 | System.out.println(s9); 272 | System.out.println(s10); 273 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptHex(s7, "passwordpassword", "passw0rdpassw0rd", AseEnum.CBC_NO_PADDING)); 274 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptHex(s8, "passwordpassword", "passw0rdpassw0rd", AseEnum.CBC_PKCS5PADDING)); 275 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptHex(s9, "passwordpassword", null, AseEnum.ECB_NO_PADDING)); 276 | System.out.println(EncryptUtil.AES_ENCRYPT.decryptHex(s10, "passwordpassword", null, AseEnum.ECB_PKCS5PADDING)); 277 | } 278 | 279 | public static void aesKGngBaseTest() throws Exception { 280 | String s1 = EncryptUtil.AES_KGEN_ENCRYPT.encryptBase64("123456"); 281 | System.out.println(s1); 282 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptBase64(s1)); 283 | 284 | String s2 = EncryptUtil.AES_KGEN_ENCRYPT.encryptBase64("123456", "password", "passw0rd"); 285 | System.out.println(s2); 286 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptBase64(s2, "password", "passw0rd")); 287 | 288 | String s3 = EncryptUtil.AES_KGEN_ENCRYPT.encryptBase64("123456", AseEnum.CBC_NO_PADDING); 289 | String s4 = EncryptUtil.AES_KGEN_ENCRYPT.encryptBase64("123456", AseEnum.CBC_PKCS5PADDING); 290 | String s5 = EncryptUtil.AES_KGEN_ENCRYPT.encryptBase64("123456", AseEnum.ECB_NO_PADDING); 291 | String s6 = EncryptUtil.AES_KGEN_ENCRYPT.encryptBase64("123456", AseEnum.ECB_PKCS5PADDING); 292 | System.out.println(s3); 293 | System.out.println(s4); 294 | System.out.println(s5); 295 | System.out.println(s6); 296 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptBase64(s3, AseEnum.CBC_NO_PADDING)); 297 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptBase64(s4, AseEnum.CBC_PKCS5PADDING)); 298 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptBase64(s5, AseEnum.ECB_NO_PADDING)); 299 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptBase64(s6, AseEnum.ECB_PKCS5PADDING)); 300 | 301 | String s7 = EncryptUtil.AES_KGEN_ENCRYPT.encryptBase64("123456", "password", "passw0rd", AseEnum.CBC_NO_PADDING); 302 | String s8 = EncryptUtil.AES_KGEN_ENCRYPT.encryptBase64("123456", "password", "passw0rd", AseEnum.CBC_PKCS5PADDING); 303 | String s9 = EncryptUtil.AES_KGEN_ENCRYPT.encryptBase64("123456", "password", null, AseEnum.ECB_NO_PADDING); 304 | String s10 = EncryptUtil.AES_KGEN_ENCRYPT.encryptBase64("123456", "password", null, AseEnum.ECB_PKCS5PADDING); 305 | System.out.println(s7); 306 | System.out.println(s8); 307 | System.out.println(s9); 308 | System.out.println(s10); 309 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptBase64(s7, "password", "passw0rd", AseEnum.CBC_NO_PADDING)); 310 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptBase64(s8, "password", "passw0rd", AseEnum.CBC_PKCS5PADDING)); 311 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptBase64(s9, "password", null, AseEnum.ECB_NO_PADDING)); 312 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptBase64(s10, "password", null, AseEnum.ECB_PKCS5PADDING)); 313 | } 314 | 315 | public static void aesKGngHexTest() throws Exception { 316 | String s1 = EncryptUtil.AES_KGEN_ENCRYPT.encryptHex("123456"); 317 | System.out.println(s1); 318 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptHex(s1)); 319 | 320 | String s2 = EncryptUtil.AES_KGEN_ENCRYPT.encryptHex("123456", "password", "passw0rd"); 321 | System.out.println(s2); 322 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptHex(s2, "password", "passw0rd")); 323 | 324 | String s3 = EncryptUtil.AES_KGEN_ENCRYPT.encryptHex("123456", AseEnum.CBC_NO_PADDING); 325 | String s4 = EncryptUtil.AES_KGEN_ENCRYPT.encryptHex("123456", AseEnum.CBC_PKCS5PADDING); 326 | String s5 = EncryptUtil.AES_KGEN_ENCRYPT.encryptHex("123456", AseEnum.ECB_NO_PADDING); 327 | String s6 = EncryptUtil.AES_KGEN_ENCRYPT.encryptHex("123456", AseEnum.ECB_PKCS5PADDING); 328 | System.out.println(s3); 329 | System.out.println(s4); 330 | System.out.println(s5); 331 | System.out.println(s6); 332 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptHex(s3, AseEnum.CBC_NO_PADDING)); 333 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptHex(s4, AseEnum.CBC_PKCS5PADDING)); 334 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptHex(s5, AseEnum.ECB_NO_PADDING)); 335 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptHex(s6, AseEnum.ECB_PKCS5PADDING)); 336 | 337 | String s7 = EncryptUtil.AES_KGEN_ENCRYPT.encryptHex("123456", "password", "passw0rd", AseEnum.CBC_NO_PADDING); 338 | String s8 = EncryptUtil.AES_KGEN_ENCRYPT.encryptHex("123456", "password", "passw0rd", AseEnum.CBC_PKCS5PADDING); 339 | String s9 = EncryptUtil.AES_KGEN_ENCRYPT.encryptHex("123456", "password", null, AseEnum.ECB_NO_PADDING); 340 | String s10 = EncryptUtil.AES_KGEN_ENCRYPT.encryptHex("123456", "password", null, AseEnum.ECB_PKCS5PADDING); 341 | System.out.println(s7); 342 | System.out.println(s8); 343 | System.out.println(s9); 344 | System.out.println(s10); 345 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptHex(s7, "password", "passw0rd", AseEnum.CBC_NO_PADDING)); 346 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptHex(s8, "password", "passw0rd", AseEnum.CBC_PKCS5PADDING)); 347 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptHex(s9, "password", null, AseEnum.ECB_NO_PADDING)); 348 | System.out.println(EncryptUtil.AES_KGEN_ENCRYPT.decryptHex(s10, "password", null, AseEnum.ECB_PKCS5PADDING)); 349 | } 350 | 351 | public static void rsaBaseTest() throws Exception { 352 | String s1 = EncryptUtil.RSA_ENCRYPT.encryptBase64("123456"); 353 | System.out.println(s1); 354 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptBase64(s1)); 355 | 356 | String s2 = EncryptUtil.RSA_ENCRYPT.encryptBase64("123456", "password", "passw0rd"); 357 | System.out.println(s2); 358 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptBase64(s2, "password", "passw0rd")); 359 | 360 | String s3 = EncryptUtil.RSA_ENCRYPT.encryptBase64("123456", RSAEnum.ECB_OAEP_WITH_SHA1_AND_MGF_1PADDING); 361 | String s4 = EncryptUtil.RSA_ENCRYPT.encryptBase64("123456", RSAEnum.ECB_OAEP_WITH_SHA256_AND_MGF_1PADDING); 362 | String s5 = EncryptUtil.RSA_ENCRYPT.encryptBase64("123456", RSAEnum.ECB_PKCS1PADDING); 363 | System.out.println(s3); 364 | System.out.println(s4); 365 | System.out.println(s5); 366 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptBase64(s3, RSAEnum.ECB_OAEP_WITH_SHA1_AND_MGF_1PADDING)); 367 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptBase64(s4, RSAEnum.ECB_OAEP_WITH_SHA256_AND_MGF_1PADDING)); 368 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptBase64(s5, RSAEnum.ECB_PKCS1PADDING)); 369 | 370 | String s6 = EncryptUtil.RSA_ENCRYPT.encryptBase64("123456", "password", null, RSAEnum.ECB_OAEP_WITH_SHA1_AND_MGF_1PADDING); 371 | String s7 = EncryptUtil.RSA_ENCRYPT.encryptBase64("123456", "password", null, RSAEnum.ECB_OAEP_WITH_SHA256_AND_MGF_1PADDING); 372 | String s8 = EncryptUtil.RSA_ENCRYPT.encryptBase64("123456", "password", null, RSAEnum.ECB_PKCS1PADDING); 373 | System.out.println(s6); 374 | System.out.println(s7); 375 | System.out.println(s8); 376 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptBase64(s6, "password", null, RSAEnum.ECB_OAEP_WITH_SHA1_AND_MGF_1PADDING)); 377 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptBase64(s7, "password", null, RSAEnum.ECB_OAEP_WITH_SHA256_AND_MGF_1PADDING)); 378 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptBase64(s8, "password", null, RSAEnum.ECB_PKCS1PADDING)); 379 | } 380 | 381 | public static void rsaHexTest() throws Exception { 382 | String s1 = EncryptUtil.RSA_ENCRYPT.encryptHex("123456"); 383 | System.out.println(s1); 384 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptHex(s1)); 385 | 386 | String s2 = EncryptUtil.RSA_ENCRYPT.encryptHex("123456", "password", "passw0rd"); 387 | System.out.println(s2); 388 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptHex(s2, "password", "passw0rd")); 389 | 390 | String s3 = EncryptUtil.RSA_ENCRYPT.encryptHex("123456", RSAEnum.ECB_OAEP_WITH_SHA1_AND_MGF_1PADDING); 391 | String s4 = EncryptUtil.RSA_ENCRYPT.encryptHex("123456", RSAEnum.ECB_OAEP_WITH_SHA256_AND_MGF_1PADDING); 392 | String s5 = EncryptUtil.RSA_ENCRYPT.encryptHex("123456", RSAEnum.ECB_PKCS1PADDING); 393 | System.out.println(s3); 394 | System.out.println(s4); 395 | System.out.println(s5); 396 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptHex(s3, RSAEnum.ECB_OAEP_WITH_SHA1_AND_MGF_1PADDING)); 397 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptHex(s4, RSAEnum.ECB_OAEP_WITH_SHA256_AND_MGF_1PADDING)); 398 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptHex(s5, RSAEnum.ECB_PKCS1PADDING)); 399 | 400 | String s6 = EncryptUtil.RSA_ENCRYPT.encryptHex("123456", "password", null, RSAEnum.ECB_OAEP_WITH_SHA1_AND_MGF_1PADDING); 401 | String s7 = EncryptUtil.RSA_ENCRYPT.encryptHex("123456", "password", null, RSAEnum.ECB_OAEP_WITH_SHA256_AND_MGF_1PADDING); 402 | String s8 = EncryptUtil.RSA_ENCRYPT.encryptHex("123456", "password", null, RSAEnum.ECB_PKCS1PADDING); 403 | System.out.println(s6); 404 | System.out.println(s7); 405 | System.out.println(s8); 406 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptHex(s6, "password", null, RSAEnum.ECB_OAEP_WITH_SHA1_AND_MGF_1PADDING)); 407 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptHex(s7, "password", null, RSAEnum.ECB_OAEP_WITH_SHA256_AND_MGF_1PADDING)); 408 | System.out.println(EncryptUtil.RSA_ENCRYPT.decryptHex(s8, "password", null, RSAEnum.ECB_PKCS1PADDING)); 409 | } 410 | } 411 | --------------------------------------------------------------------------------