1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package com.example.utils;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig; import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
public class JasyptUtil {
public static String encrypt(String secretKey, String message) { return stringEncryptor(secretKey, message, true); }
public static String decrypt(String secretKey, String message) { return stringEncryptor(secretKey, message, false); }
private static String stringEncryptor(String secretKey, String message, boolean isEncrypt) { PooledPBEStringEncryptor pooledPBEStringEncryptor = new PooledPBEStringEncryptor(); pooledPBEStringEncryptor.setConfig(getSimpleStringPBEConfig(secretKey)); String result = isEncrypt ? pooledPBEStringEncryptor.encrypt(message) : pooledPBEStringEncryptor.decrypt(message); return result; }
private static SimpleStringPBEConfig getSimpleStringPBEConfig(String secretKey) { SimpleStringPBEConfig config = new SimpleStringPBEConfig(); config.setPassword(secretKey); config.setAlgorithm("PBEWithMD5AndDES"); config.setPoolSize("1"); config.setKeyObtentionIterations("1000"); config.setProviderName("SunJCE"); config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator"); config.setStringOutputType("base64"); return config; }
}
|