【问题标题】:Jasypt: How to decrypt property stored in Map<String, String>?Jasypt:如何解密存储在 Map<String, String> 中的属性?
【发布时间】:2019-08-07 11:06:56
【问题描述】:

同学们,你能帮忙用jasypt从属性文件中解密密码(它是stmCredentials映射中的一个值)吗?

我的属性文件中有下一个字符串:

creds.users={testLogin: 'ENC(w0H***pgsj)'}
user2.login = ENC(9j3fHz5c****cLRCVvLTQmr5)
user2.pass  = ENC(w0HxpKq7V3Lf***g3zs/hpgsj)

我在调试模式下运行测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("dev")
@Slf4j
public class CredentialsTest {


    @BeforeClass
    public static void beforeClass(){
        System.setProperty("jasypt.encryptor.password", "C*******L");
    }


    @Autowired
    StmCredentials stmCredentials;


    @Value("${user2.login}")
    private String user2Login;
    @Value("${user2.pass}")
    private String user2Pass;

    @Test
    public void getCredspairs() {
        HashMap<String, String> credspairs = stmCredentials.getCredspairs();
    }
}

运行后我在变量中有下一个值:

credspairs:
  key: "testLogin" 
  value:"ENC(w0HxpKq7V3LfEPsU5mbd0Vg3zs/hpgsj)" //it wasn't decrypt =(

和(注意!

user2Login = testLogin   //it was decrypt
user2Pass = K1212Zrde

我的属性文件中似乎有问题,在creds.users 属性中。我尝试使用“单引号,双引号”,但没有帮助。

StmCredentials bean 看起来像:

@Component
@EnableConfigurationProperties
public class StmCredentials {

    @Value("#{${creds.users}}")
    private HashMap<String, String> credspairs;
    public HashMap<String, String> getCredspairs() {
        return credspairs;
    }
    public void setCredspairs(HashMap<String, String> somedata) {
        this.credspairs = somedata;
    }
}

如何解密存储在StmCredentials(值)中的密码? 谢谢你的建议。

【问题讨论】:

    标签: spring-boot properties-file jasypt


    【解决方案1】:

    希望这会有所帮助:

    我认为 Jasypt 不能以这种方式检测和解密属性文件中的值(根据我的知识范围)。如果可以,您可以尝试将其放入application.yml 文件中。它应该在那里工作。无论如何,这是我们可以做的:

    这实际上不是解决方案,而是一种可能的解决方法。如果 Jasypt 不会自动为我们解密,我们可以自己创建一个类来解密这些值。

    import org.jasypt.encryption.StringEncryptor;
    import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
    import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
    
    public class MyStringEncryptor {
        private StringEncryptor encryptor;
    
        public MyStringEncryptor(String password) {
            PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
           SimpleStringPBEConfig config = new SimpleStringPBEConfig();
           config.setPassword(password);
           config.setAlgorithm("PBEWITHMD5ANDDES");
           config.setKeyObtentionIterations("1000");
           config.setPoolSize("1");
           config.setProviderName("SunJCE");
           config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
           config.setStringOutputType("base64");
           encryptor.setConfig(config);
           this.encryptor = encryptor;
        }
    
        public String encrypt(String message) {
            return encryptor.encrypt(message);
        }
    
        public String decrypt(String message) {
            return encryptor.decrypt(message);
        }
    }
    

    现在我们可以创建一个MyStringEncryptor类的对象,并使用decrypt方法来解密我们想要的值。

    MyStringEncryptor encryptor = new MyStringEncryptor("mysecretpass"); // You can pass the password from properties file using @Value
    
    String decryptedValue = encryptor.decerypt(encrypted-message);
    

    【讨论】:

      猜你喜欢
      • 2023-02-07
      • 2013-01-15
      • 2011-03-24
      • 1970-01-01
      • 2013-12-18
      • 2015-11-17
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      相关资源
      最近更新 更多