RSA加密工具类,使用公匙加密,私匙解密
20 | *该工具类使用slatKey作为SecureRandom的随机种子来创建公匙和私匙,此情况下只需记录下slatKey即可
21 | *若需要使用 new SecureRandom() 来创建公匙和私匙,则创建公匙和私匙时,需要记录下公匙和私匙
22 | *RSA加密算法,一般有:1)使用公匙加密,需要使用私匙解密;2)使用私匙加密,需要使用公匙解密
23 | */ 24 | public class RsaUtil extends CipherEencryptImpNoPadding加密模式, 加密内容必须是 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 | --------------------------------------------------------------------------------