└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # jasypt Decrypt Encrypt 2 | 3 | 常见的加密算法: 4 | ``` 5 | PBEWithMD5AndDES 6 | PBEWithSHA1AndDESede 7 | PBEWithSHA1AndRC2_40 8 | PBEWithSHA1AndRC2_128 9 | PBEWithSHA1AndRC4_40 10 | PBEWithSHA1AndRC4_128 11 | PBEWithMD5AndTripleDES 12 | PBEWithHmacSHA1AndAES_128 13 | PBEWithHmacSHA224AndAES_128 14 | PBEWithHmacSHA256AndAES_128 15 | PBEWithHmacSHA384AndAES_128 16 | PBEWithHmacSHA512AndAES_128 17 | PBEWithHmacSHA1AndAES_256 18 | PBEWithHmacSHA224AndAES_256 19 | PBEWithHmacSHA256AndAES_256 20 | PBEWithHmacSHA384AndAES_256 21 | PBEWithHmacSHA512AndAES_256 22 | ``` 23 | ## e.g. 24 | 25 | 26 | `application.yml` 27 | 28 | ``` 29 | #mysql数据库配置 30 | datasource: 31 | url: jdbc:mysql://127.0.0.1:3306/secert?useServerPrepStmts=true 32 | username: root 33 | password: ENC(wQ2RWioogVIXRdfTipVT6UW/J0Waxa0n) 34 | driver-class-name: com.mysql.cj.jdbc.Driver 35 | ``` 36 | 37 | `ENC(wQ2RWioogVIXRdfTipVT6UW/J0Waxa0n)` need `jas502n` to Decrypt 38 | 39 | ## Encrypt 40 | 41 | ``` 42 | ╰─$ java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="ThisIsMySecert" password="jas502n" algorithm=PBEWithMD5AndDES 43 | 44 | ----ENVIRONMENT----------------- 45 | 46 | Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.60-b23 47 | 48 | 49 | 50 | ----ARGUMENTS------------------- 51 | 52 | algorithm: PBEWithMD5AndDES 53 | input: ThisIsMySecert 54 | password: jas502n 55 | 56 | 57 | 58 | ----OUTPUT---------------------- 59 | 60 | wQ2RWioogVIXRdfTipVT6UW/J0Waxa0n 61 | ``` 62 | 63 | ## Decrypt 64 | 65 | ``` 66 | ╰─$ java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="wQ2RWioogVIXRdfTipVT6UW/J0Waxa0n" password="jas502n" algorithm=PBEWithMD5AndDES 67 | 68 | ----ENVIRONMENT----------------- 69 | 70 | Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.60-b23 71 | 72 | 73 | 74 | ----ARGUMENTS------------------- 75 | 76 | algorithm: PBEWithMD5AndDES 77 | input: wQ2RWioogVIXRdfTipVT6UW/J0Waxa0n 78 | password: jas502n 79 | 80 | 81 | 82 | ----OUTPUT---------------------- 83 | 84 | ThisIsMySecert 85 | 86 | ``` 87 | 88 | ## JasyptPBEStringDecryptionCLI 89 | 90 | ``` 91 | // 92 | // Source code recreated from a .class file by IntelliJ IDEA 93 | // (powered by Fernflower decompiler) 94 | // 95 | 96 | package org.jasypt.intf.cli; 97 | 98 | import java.util.Properties; 99 | import org.jasypt.intf.service.JasyptStatelessService; 100 | 101 | public final class JasyptPBEStringDecryptionCLI { 102 | private static final String[][] VALID_REQUIRED_ARGUMENTS = new String[][]{{"input"}, {"password"}}; 103 | private static final String[][] VALID_OPTIONAL_ARGUMENTS = new String[][]{{"verbose"}, {"algorithm"}, {"keyObtentionIterations"}, {"saltGeneratorClassName"}, {"providerName"}, {"providerClassName"}, {"stringOutputType"}, {"ivGeneratorClassName"}}; 104 | 105 | public static void main(String[] args) { 106 | boolean verbose = CLIUtils.getVerbosity(args); 107 | 108 | try { 109 | String applicationName = null; 110 | String[] arguments = null; 111 | if (args[0] != null && args[0].indexOf("=") == -1) { 112 | applicationName = args[0]; 113 | arguments = new String[args.length - 1]; 114 | System.arraycopy(args, 1, arguments, 0, args.length - 1); 115 | } else { 116 | applicationName = JasyptPBEStringDecryptionCLI.class.getName(); 117 | arguments = args; 118 | } 119 | 120 | Properties argumentValues = CLIUtils.getArgumentValues(applicationName, arguments, VALID_REQUIRED_ARGUMENTS, VALID_OPTIONAL_ARGUMENTS); 121 | CLIUtils.showEnvironment(verbose); 122 | JasyptStatelessService service = new JasyptStatelessService(); 123 | String input = argumentValues.getProperty("input"); 124 | CLIUtils.showArgumentDescription(argumentValues, verbose); 125 | String result = service.decrypt(input, argumentValues.getProperty("password"), (String)null, (String)null, argumentValues.getProperty("algorithm"), (String)null, (String)null, argumentValues.getProperty("keyObtentionIterations"), (String)null, (String)null, argumentValues.getProperty("saltGeneratorClassName"), (String)null, (String)null, argumentValues.getProperty("providerName"), (String)null, (String)null, argumentValues.getProperty("providerClassName"), (String)null, (String)null, argumentValues.getProperty("stringOutputType"), (String)null, (String)null, argumentValues.getProperty("ivGeneratorClassName"), (String)null, (String)null); 126 | CLIUtils.showOutput(result, verbose); 127 | } catch (Throwable var8) { 128 | CLIUtils.showError(var8, verbose); 129 | } 130 | 131 | } 132 | 133 | private JasyptPBEStringDecryptionCLI() { 134 | } 135 | } 136 | 137 | ``` 138 | 139 | #### other 场景下的加密解密 140 | 141 | ##### PBEWITHHMACSHA512ANDAES_256 142 | ```java 143 | import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; 144 | import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; 145 | 146 | public class JasyptUtil { 147 | 148 | 149 | public static String encyptPwd(String password, String value) { 150 | PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); 151 | encryptor.setConfig(cryptor(password)); 152 | String result = encryptor.encrypt(value); 153 | return result; 154 | } 155 | 156 | public static String decyptPwd(String password, String value) { 157 | PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); 158 | encryptor.setConfig(cryptor(password)); 159 | encryptor.decrypt(value); 160 | String result = encryptor.decrypt(value); 161 | return result; 162 | } 163 | 164 | public static SimpleStringPBEConfig cryptor(String password) { 165 | SimpleStringPBEConfig config = new SimpleStringPBEConfig(); 166 | config.setPassword(password); 167 | config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256"); 168 | config.setKeyObtentionIterations("1000"); 169 | config.setPoolSize(1); 170 | config.setProviderName("SunJCE"); 171 | config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); 172 | config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator"); 173 | config.setStringOutputType("base64"); 174 | return config; 175 | } 176 | 177 | public static void main(String[] args) { 178 | String key = "jas502n"; 179 | String rawPassword = "123456"; 180 | // rTv1dSH1yEVRC6ztDqTEGR8Gd35wrRwERoJivHq3sChy1l6E+Sgu24M1FpXH+Cfd 181 | System.out.println(encyptPwd(key, rawPassword)); 182 | 183 | System.out.println(decyptPwd(key,"rTv1dSH1yEVRC6ztDqTEGR8Gd35wrRwERoJivHq3sChy1l6E+Sgu24M1FpXH+Cfd")); 184 | } 185 | } 186 | 187 | ``` 188 | 189 | 输出结果: 190 | ```java 191 | 192 | PYqKC1KBDiMBfV0cS+SFNM5X/7mOh9EWVKDVGA8Mpi2kvdE7b6n8kr+5JijIi6NH 193 | 123456 194 | ``` 195 | 196 | 197 | 198 | ## online decrypt 199 | 200 | https://www.devglan.com/online-tools/jasypt-online-encryption-decryption 201 | --------------------------------------------------------------------------------