org.springframework.security.crypto.encrypt.Encryptors Java Examples

The following examples show how to use org.springframework.security.crypto.encrypt.Encryptors. 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: StringEncryptorHolder.java    From summerframework with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        TextEncryptor encryptor =
            Encryptors.delux("pass", new String(Hex.encode("salt".getBytes(Charset.forName("utf-8")))));
        System.out.println(encryptor.encrypt("sadfsadfasfsadf"));
        System.out.println(encryptor.encrypt("sadfsadfasfsadf"));
        System.out.println(encryptor.decrypt(encryptor.encrypt("这是密码")));
    }
 
Example #2
Source File: EnvironmentDecryptApplicationInitializerTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecryptCompositePropertySource() {
	ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
	EnvironmentDecryptApplicationInitializer initializer = new EnvironmentDecryptApplicationInitializer(
			Encryptors.noOpText());

	MapPropertySource devProfile = new MapPropertySource("dev-profile",
			Collections.singletonMap("key", "{cipher}value1"));

	MapPropertySource defaultProfile = new MapPropertySource("default-profile",
			Collections.singletonMap("key", "{cipher}value2"));

	CompositePropertySource cps = mock(CompositePropertySource.class);
	when(cps.getPropertyNames()).thenReturn(devProfile.getPropertyNames());
	when(cps.getPropertySources())
			.thenReturn(Arrays.asList(devProfile, defaultProfile));
	ctx.getEnvironment().getPropertySources().addLast(cps);

	initializer.initialize(ctx);
	then(ctx.getEnvironment().getProperty("key")).isEqualTo("value1");
}
 
Example #3
Source File: EnvironmentDecryptApplicationInitializerTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecryptNonStandardParent() {
	ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
	EnvironmentDecryptApplicationInitializer initializer = new EnvironmentDecryptApplicationInitializer(
			Encryptors.noOpText());

	TestPropertyValues.of("key:{cipher}value").applyTo(ctx);

	ApplicationContext ctxParent = mock(ApplicationContext.class);
	when(ctxParent.getEnvironment()).thenReturn(mock(Environment.class));

	ctx.setParent(ctxParent);

	initializer.initialize(ctx);

	then(ctx.getEnvironment().getProperty("key")).isEqualTo("value");
}
 
Example #4
Source File: EnvironmentDecryptApplicationInitializerTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void errorOnDecrypt() {
	this.listener = new EnvironmentDecryptApplicationInitializer(
			Encryptors.text("deadbeef", "AFFE37"));
	ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
	TestPropertyValues.of("foo: {cipher}bar").applyTo(context);
	// catch IllegalStateException and verify
	try {
		this.listener.initialize(context);
	}
	catch (Exception e) {
		then(e).isInstanceOf(IllegalStateException.class);
	}
	// Assert logs contain warning even when exception thrown
	String sysOutput = this.outputCapture.toString();
	then(sysOutput).contains("Cannot decrypt: key=foo");
}
 
Example #5
Source File: SocialAutoConfiguration.java    From cola with MIT License 5 votes vote down vote up
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
	JpaUsersConnectRepository repository = new JpaUsersConnectRepository(userConnectionRepository,
			connectionFactoryLocator, Encryptors.noOpText());
	if (connectionSignUp != null) {
		repository.setConnectionSignUp(connectionSignUp);
	}
	return repository;
}
 
Example #6
Source File: SocialConfig.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
    MongoUsersConnectionRepositoryImpl repository = new MongoUsersConnectionRepositoryImpl(
            userSocialConnectionRepository, (SocialAuthenticationServiceLocator)connectionFactoryLocator, Encryptors.noOpText());
    repository.setConnectionSignUp(autoConnectionSignUp());
    return repository;
}
 
Example #7
Source File: EncryptionControllerMultiTextEncryptorTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncryptUsingApplicationAndProfiles() {

	this.controller = new EncryptionController(
			new SingleTextEncryptorLocator(Encryptors.text("application", "11")));

	// when
	String encrypted = this.controller.encrypt(this.application, this.profiles,
			this.data, TEXT_PLAIN);

	// then
	assertThat(this.controller.decrypt(this.application, this.profiles, encrypted,
			TEXT_PLAIN)).isEqualTo(this.data);
}
 
Example #8
Source File: EncryptionAutoConfiguration.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Bean
public TextEncryptor defaultTextEncryptor() {
	if (this.locator != null) {
		return new LocatorTextEncryptor(this.locator);
	}
	if (StringUtils.hasText(this.key.getKey())) {
		return new EncryptorFactory(this.key.getSalt()).create(this.key.getKey());
	}
	return Encryptors.noOpText();
}
 
Example #9
Source File: EnvironmentDecryptApplicationInitializerTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void errorOnDecryptWithEmpty() {
	this.listener = new EnvironmentDecryptApplicationInitializer(
			Encryptors.text("deadbeef", "AFFE37"));
	this.listener.setFailOnError(false);
	ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
	TestPropertyValues.of("foo: {cipher}bar").applyTo(context);
	this.listener.initialize(context);
	// Assert logs contain warning
	String sysOutput = this.outputCapture.toString();
	then(sysOutput).contains("Cannot decrypt: key=foo");
	// Empty is safest fallback for undecryptable cipher
	then(context.getEnvironment().getProperty("foo")).isEqualTo("");
}
 
Example #10
Source File: DefaultTextEncryptor.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns decrypted text from a Base64 string composed by the salt followed
 * by the encrypted data.
 */
@Override
public String decrypt(String base64Data) {

    byte[] bytes = Base64.getDecoder().decode(base64Data);
    byte[] saltBytes = ArrayUtils.subarray(bytes, 0, 8);
    byte[] encryptedBytes = ArrayUtils.subarray(bytes, 8, bytes.length);

    String salt = new String(Hex.encode(saltBytes));
    BytesEncryptor encryptor = Encryptors.standard(key, salt);

    return new String(encryptor.decrypt(encryptedBytes));
}
 
Example #11
Source File: DefaultTextEncryptor.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns a Base64 string composed by the salt followed by the encrypted
 * data.
 */
@Override
public String encrypt(String plainText) {

    // default StringKeyGenerator returns a 8 bytes hex-encoded string
    String salt = KeyGenerators.string().generateKey();

    BytesEncryptor encryptor = Encryptors.standard(key, salt);
    byte[] encrypted = encryptor.encrypt(plainText.getBytes());

    byte[] saltAndSecret = ArrayUtils.addAll(Hex.decode(salt), encrypted);
    return Base64.getEncoder().encodeToString(saltAndSecret);
}
 
Example #12
Source File: EncrypterUtil.java    From SMSC with Apache License 2.0 5 votes vote down vote up
/**
 * Method to decrypt fields based on {@link Encrypt} annotation.
 *
 * @param obj entity object
 */
public static void decrypt(Object obj) throws IllegalAccessException {
    CharSequence salt = getSalt(obj);

    TextEncryptor encryptor = Encryptors.text(secretKey, salt);
    for (Field field : obj.getClass().getDeclaredFields()) {
        if (field.isAnnotationPresent(Encrypt.class)) {
            field.setAccessible(true);
            field.set(obj, encryptor.decrypt((String) field.get(obj)));
            field.setAccessible(false);
        }
    }
}
 
Example #13
Source File: EncrypterUtil.java    From SMSC with Apache License 2.0 5 votes vote down vote up
/**
 * Method to encrypt fields based on {@link Encrypt} annotation.
 *
 * @param obj entity object
 */
public static void encrypt(Object obj) throws IllegalAccessException {
    CharSequence salt = getSalt(obj);

    TextEncryptor encryptor = Encryptors.text(secretKey, salt);
    for (Field field : obj.getClass().getDeclaredFields()) {
        if (field.isAnnotationPresent(Encrypt.class)) {
            field.setAccessible(true);
            field.set(obj, encryptor.encrypt((String) field.get(obj)));
            field.setAccessible(false);
        }
    }
}
 
Example #14
Source File: EncryptionUtility.java    From blackduck-alert with Apache License 2.0 5 votes vote down vote up
public String decrypt(String encryptedValue) {
    try {
        String password = getPassword();
        String salt = getEncodedSalt();
        if (StringUtils.isNotBlank(encryptedValue) && StringUtils.isNotBlank(password) && StringUtils.isNotBlank(salt)) {
            TextEncryptor decryptor = Encryptors.delux(password, salt);
            return decryptor.decrypt(encryptedValue);
        }
    } catch (IllegalArgumentException | IllegalStateException | NullPointerException ex) {
        logger.error("Error decrypting value", ex);
    }
    return StringUtils.EMPTY;
}
 
Example #15
Source File: EncryptionUtility.java    From blackduck-alert with Apache License 2.0 5 votes vote down vote up
public String encrypt(String value) {
    String password = getPassword();
    String salt = getEncodedSalt();
    if (StringUtils.isNotBlank(value) && StringUtils.isNotBlank(password) && StringUtils.isNotBlank(salt)) {
        TextEncryptor encryptor = Encryptors.delux(password, salt);
        return encryptor.encrypt(value);
    }
    return StringUtils.EMPTY;
}
 
Example #16
Source File: SocialConfig.java    From pre with GNU General Public License v3.0 5 votes vote down vote up
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
    PreJdbcUsersConnectionRepository repository = new PreJdbcUsersConnectionRepository(dataSource,
            connectionFactoryLocator, Encryptors.noOpText());
    repository.setTablePrefix("social_");
    return repository;
}
 
Example #17
Source File: EncryptionComponent.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public EncryptionComponent(String encryptKey) {
    if (encryptKey != null) {
        this.textEncryptor = Encryptors.text(encryptKey, "deadbeef"); //$NON-NLS-1$
    } else {
        this.textEncryptor = null;
    }
}
 
Example #18
Source File: SocialConfig.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Gets users connection repository.
 *
 * @param connectionFactoryLocator the connection factory locator
 *
 * @return the users connection repository
 */
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
	JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
	repository.setTablePrefix("pc_uac_");
	if (connectionSignUp != null) {
		repository.setConnectionSignUp(connectionSignUp);
	}
	return repository;
}
 
Example #19
Source File: CredentialsIntegrationTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Bean
public TextEncryptor getTextEncryptor() {
    return Encryptors.noOpText();
}
 
Example #20
Source File: AesGcmPasswordEncoder.java    From airsonic-advanced with GNU General Public License v3.0 4 votes vote down vote up
public AesGcmPasswordEncoder(String password, String salt) {
    this.encryptor = Encryptors.delux(password, salt);
}
 
Example #21
Source File: SocialConfig.java    From AIDR with GNU Affero General Public License v3.0 4 votes vote down vote up
@Bean
public TextEncryptor textEncryptor() {
    return Encryptors.noOpText();
}
 
Example #22
Source File: SocialConfig.java    From lolibox with Apache License 2.0 4 votes vote down vote up
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
    return new JpaUsersConnectionRepository(jpaTemplate, connectionFactoryLocator, Encryptors.noOpText());
}
 
Example #23
Source File: SocialConfig.java    From blog-social-login-with-spring-social with Apache License 2.0 4 votes vote down vote up
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
    JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource,connectionFactoryLocator, Encryptors.noOpText());
    repository.setConnectionSignUp(new AccountConnectionSignUpService(usersDao));
    return repository;
}
 
Example #24
Source File: StandardStringEncryptor.java    From summerframework with Apache License 2.0 4 votes vote down vote up
public StandardStringEncryptor(String password, String salt) {
    super(password, salt);
    encryptor = Encryptors.text(super.getPassword(), super.getSalt());
}
 
Example #25
Source File: SingleTextEncryptorLocator.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
public SingleTextEncryptorLocator(TextEncryptor encryptor) {
	this.encryptor = encryptor == null ? Encryptors.noOpText() : encryptor;
}
 
Example #26
Source File: SecurityConfig.java    From Vaadin4Spring-MVP-Sample-SpringSecurity with Apache License 2.0 4 votes vote down vote up
@Bean
public TextEncryptor textEncryptor() {
	return Encryptors.noOpText();
}
 
Example #27
Source File: StrongStringEncryptor.java    From summerframework with Apache License 2.0 4 votes vote down vote up
public StrongStringEncryptor(String password, String salt) {
    super(password, salt);
    encryptor = Encryptors.delux(super.getPassword(), super.getSalt());
}
 
Example #28
Source File: FebsSocialConfig.java    From FEBS-Security with Apache License 2.0 4 votes vote down vote up
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
    JdbcUsersConnectionRepository jdbcUsersConnectionRepository = new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
    jdbcUsersConnectionRepository.setTablePrefix("t_");
    return jdbcUsersConnectionRepository;
}
 
Example #29
Source File: DvAutoConfiguration.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Bean
public TextEncryptor getTextEncryptor() {
    return Encryptors.text(encryptKey, "deadbeef");
}
 
Example #30
Source File: ClusterConfigFetcherTest.java    From haven-platform with Apache License 2.0 4 votes vote down vote up
@Bean
TextEncryptor textEncryptor() {
    return Encryptors.noOpText();
}