org.jasypt.encryption.pbe.PooledPBEStringEncryptor Java Examples

The following examples show how to use org.jasypt.encryption.pbe.PooledPBEStringEncryptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JasyptConfig.java    From spring-boot-vue-admin with Apache License 2.0 6 votes vote down vote up
@Bean
public StringEncryptor myStringEncryptor() throws Exception {
  // Base64 + RSA 加密的密码
  final byte[] passwordEncryptedByRSA =
      Base64Utils.decodeFromString(this.passwordEncryptedByBase64AndRSA);
  final String password = new String(this.rsaUtils.decrypt(passwordEncryptedByRSA));
  // 配置
  final SimpleStringPBEConfig config =
      new SimpleStringPBEConfig() {
        {
          this.setPassword(password);
          // 加密算法
          this.setAlgorithm("PBEWithMD5AndDES");
          this.setKeyObtentionIterations("1000");
          this.setPoolSize("1");
          this.setProviderName("SunJCE");
          this.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
          this.setStringOutputType("base64");
        }
      };
  final PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
  encryptor.setConfig(config);
  return encryptor;
}
 
Example #2
Source File: BootTest.java    From seed with Apache License 2.0 6 votes vote down vote up
/**
 * Jasypt加解密屬性文件
 */
@Test
public void jasyptTest(){
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    config.setPassword("https://jadyer.cn/");
    config.setAlgorithm("PBEWithMD5AndDES");
    config.setKeyObtentionIterations("1000");
    config.setPoolSize("1");
    config.setProviderName("SunJCE");
    config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
    config.setStringOutputType("hexadecimal");
    encryptor.setConfig(config);
    String encryptStr = encryptor.encrypt("xuanyu");
    System.out.println("encryptStr=" + encryptStr);
    String decryptStr = encryptor.decrypt(encryptStr);
    Assert.assertEquals("xuanyu", decryptStr);
}
 
Example #3
Source File: JasyptUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
@Ignore("should have installed local_policy.jar")
public void givenTextPrivateData_whenDecryptOnHighPerformance_thenDecrypt() {
    // given
    String privateData = "secret-data";
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    encryptor.setPoolSize(4);
    encryptor.setPassword("some-random-data");
    encryptor.setAlgorithm("PBEWithMD5AndTripleDES");

    // when
    String encryptedText = encryptor.encrypt(privateData);
    assertNotSame(privateData, encryptedText);

    // then
    String plainText = encryptor.decrypt(encryptedText);
    assertEquals(plainText, privateData);
}
 
Example #4
Source File: JasyptConfiguration.java    From seed with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * BeanPostProcessorBeanFactoryPostProcessor區別可參考http://www.shouce.ren/api/spring2.5/ch03s07.html<br/>
 * 大致就是BeanFactoryPostProcessor用於Spring註冊BeanDefinition之後,實例化BeanDefinition之前,可修改Bean配置<br/>
 * 比如常見的配置數據庫連接池dataSource,用戶名和密碼放在獨立的一個配置文件中,然後用${jdbc.name}引用裏面的配置
 * </p>
 */
@Bean
public BeanFactoryPostProcessor propertySourcesPostProcessor(){
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();

    //Master Password used for Encryption/Decryption of properties.
    config.setPassword(this.getProperty(this.environment, "jasypt.encryptor.password", "https://jadyer.cn/"));

    //Encryption/Decryption Algorithm to be used by Jasypt.
    //For more info on how to get available algorithms visit: <a href="http://www.jasypt.org/cli.html"/>Jasypt CLI Tools Page</a>.
    config.setAlgorithm(this.getProperty(this.environment, "jasypt.encryptor.algorithm", "PBEWithMD5AndDES"));

    //Number of hashing iterations to obtain the signing key.
    config.setKeyObtentionIterations(this.getProperty(this.environment, "jasypt.encryptor.keyObtentionIterations", "1000"));

    //The size of the pool of encryptors to be created.
    config.setPoolSize(this.getProperty(this.environment, "jasypt.encryptor.poolSize", "1"));

    //The name of the {@link java.security.Provider} implementation to be used by the encryptor for obtaining the encryption algorithm.
    config.setProviderName(this.getProperty(this.environment, "jasypt.encryptor.providerName", "SunJCE"));

    //A {@link org.jasypt.salt.SaltGenerator} implementation to be used by the encryptor.
    config.setSaltGeneratorClassName(this.getProperty(this.environment, "jasypt.encryptor.saltGeneratorClassname", "org.jasypt.salt.RandomSaltGenerator"));

    //Specify the form in which String output will be encoded. {@code "base64"} or {@code "hexadecimal"}.
    config.setStringOutputType(this.getProperty(this.environment, "jasypt.encryptor.stringOutputType", "hexadecimal"));

    encryptor.setConfig(config);
    return new EnableEncryptablePropertySourcesPostProcessor(encryptor);
}
 
Example #5
Source File: JasyptTests.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void multipleTextsOneInstancePooled() {
    List<String> strings = getRandomStrings(DIFFICULTY);
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    encryptor.setPoolSize(4);
    encryptor.setPassword(PASSWORD);
    encryptor.setAlgorithm("PBEWithHMACSHA512AndAES_256");
    encryptor.setIvGenerator(new RandomIvGenerator());
    strings.parallelStream().forEach(encryptor::encrypt);
}
 
Example #6
Source File: EncryptionConfig.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean( "tripleDesStringEncryptor" )
public PooledPBEStringEncryptor tripleDesStringEncryptor()
{
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    encryptor.setAlgorithm( "PBEWithSHA1AndDESede" );
    encryptor.setPassword( password );
    encryptor.setPoolSize( 4 );
    encryptor.setSaltGenerator( new StringFixedSaltGenerator( "H7g0oLkEw3wf52fs52g3hbG" ) );

    return encryptor;
}
 
Example #7
Source File: EncryptionConfig.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean( "aes128StringEncryptor" )
public PooledPBEStringEncryptor aes128StringEncryptor()
{
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    encryptor.setAlgorithm( "PBEWITHSHA256AND128BITAES-CBC-BC" );
    encryptor.setPassword( password );
    encryptor.setPoolSize( 4 );
    encryptor.setSaltGenerator( new RandomSaltGenerator() );

    return encryptor;
}
 
Example #8
Source File: StringEncryptorBuilder.java    From jasypt-spring-boot with MIT License 5 votes vote down vote up
private StringEncryptor createPBEDefault() {
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    config.setPassword(getRequired(configProps::getPassword, propertyPrefix + ".password"));
    config.setAlgorithm(get(configProps::getAlgorithm, propertyPrefix + ".algorithm", "PBEWITHHMACSHA512ANDAES_256"));
    config.setKeyObtentionIterations(get(configProps::getKeyObtentionIterations, propertyPrefix + ".key-obtention-iterations", "1000"));
    config.setPoolSize(get(configProps::getPoolSize, propertyPrefix + ".pool-size", "1"));
    config.setProviderName(get(configProps::getProviderName, propertyPrefix + ".provider-name", null));
    config.setProviderClassName(get(configProps::getProviderClassName, propertyPrefix + ".provider-class-name", null));
    config.setSaltGeneratorClassName(get(configProps::getSaltGeneratorClassname, propertyPrefix + ".salt-generator-classname", "org.jasypt.salt.RandomSaltGenerator"));
    config.setIvGeneratorClassName(get(configProps::getIvGeneratorClassname, propertyPrefix + ".iv-generator-classname", "org.jasypt.iv.RandomIvGenerator"));
    config.setStringOutputType(get(configProps::getStringOutputType, propertyPrefix + ".string-output-type", "base64"));
    encryptor.setConfig(config);
    return encryptor;
}
 
Example #9
Source File: Main.java    From tutorials with MIT License 5 votes vote down vote up
@Bean(name = "encryptorBean")
public StringEncryptor stringEncryptor() {
    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);
    return encryptor;
}
 
Example #10
Source File: BasicToken.java    From robe with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static PooledPBEStringEncryptor getEncryptor() {
    return encryptor;
}