org.springframework.security.rsa.crypto.RsaSecretEncryptor Java Examples

The following examples show how to use org.springframework.security.rsa.crypto.RsaSecretEncryptor. 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: EncryptionBootstrapConfiguration.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean(TextEncryptor.class)
public TextEncryptor textEncryptor() {
	KeyStore keyStore = this.key.getKeyStore();
	if (keyStore.getLocation() != null) {
		if (keyStore.getLocation().exists()) {
			return new RsaSecretEncryptor(
					new KeyStoreKeyFactory(keyStore.getLocation(),
							keyStore.getPassword().toCharArray()).getKeyPair(
									keyStore.getAlias(),
									keyStore.getSecret().toCharArray()),
					this.rsaProperties.getAlgorithm(),
					this.rsaProperties.getSalt(), this.rsaProperties.isStrong());
		}

		throw new IllegalStateException("Invalid keystore location");
	}

	return new EncryptorFactory(this.key.getSalt()).create(this.key.getKey());
}
 
Example #2
Source File: EncryptionControllerTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void addEnvironment() {
	TextEncryptorLocator locator = new TextEncryptorLocator() {

		private RsaSecretEncryptor encryptor = new RsaSecretEncryptor();

		@Override
		public TextEncryptor locate(Map<String, String> keys) {
			return this.encryptor;
		}
	};
	this.controller = new EncryptionController(locator);
	// Add space to input
	String cipher = this.controller.encrypt("app", "default", "foo bar",
			MediaType.TEXT_PLAIN);
	assertThat(cipher.contains("{name:app}")).as("Wrong cipher: " + cipher).isFalse();
	String decrypt = this.controller.decrypt("app", "default", cipher,
			MediaType.TEXT_PLAIN);
	assertThat(decrypt).as("Wrong decrypted plaintext: " + decrypt)
			.isEqualTo("foo bar");
}
 
Example #3
Source File: DecryptCommandTests.java    From spring-cloud-cli with Apache License 2.0 5 votes vote down vote up
@Test
public void decryptsFromRsaKey() throws Exception {
	RsaSecretEncryptor encryptor = new RsaSecretEncryptor(StreamUtils
			.copyToString(new ClassPathResource("private.pem").getInputStream(),
					Charset.forName("UTF-8"))
			.replaceAll("\n", ""));
	String cipher = encryptor.encrypt("foo");
	assertEquals(ExitStatus.OK,
			command.run("-k", "@src/test/resources/private.pem", cipher));
}
 
Example #4
Source File: DecryptCommandTests.java    From spring-cloud-cli with Apache License 2.0 5 votes vote down vote up
@Test
public void decryptsFromRsaKeyWithKeyStore() throws Exception {
	KeyStoreKeyFactory factory = new KeyStoreKeyFactory(
			new ClassPathResource("keystore.jks"), "letmein".toCharArray());
	RsaSecretEncryptor encryptor = new RsaSecretEncryptor(
			factory.getKeyPair("mytestkey", "changeme".toCharArray()));
	String cipher = encryptor.encrypt("foo");
	assertEquals(ExitStatus.OK,
			command.run("-k", "src/test/resources/keystore.jks", "--password",
					"letmein", "--keypass", "changeme", "--alias", "mytestkey",
					cipher));
}
 
Example #5
Source File: EncryptionControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test(expected = InvalidCipherException.class)
public void shouldThrowExceptionOnDecryptWrongKey() {
	RsaSecretEncryptor encryptor = new RsaSecretEncryptor();
	this.controller = new EncryptionController(
			new SingleTextEncryptorLocator(new RsaSecretEncryptor()));
	this.controller.decrypt(encryptor.encrypt("foo"), MediaType.TEXT_PLAIN);
}
 
Example #6
Source File: EncryptionControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void sunnyDayRsaKey() {
	this.controller = new EncryptionController(
			new SingleTextEncryptorLocator(new RsaSecretEncryptor()));
	String cipher = this.controller.encrypt("foo", MediaType.TEXT_PLAIN);
	assertThat(this.controller.decrypt(cipher, MediaType.TEXT_PLAIN))
			.isEqualTo("foo");
}
 
Example #7
Source File: EncryptionControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void publicKey() {
	this.controller = new EncryptionController(
			new SingleTextEncryptorLocator(new RsaSecretEncryptor()));
	String key = this.controller.getPublicKey();
	assertThat(key.startsWith("ssh-rsa")).as("Wrong key format: " + key).isTrue();
}
 
Example #8
Source File: EncryptionControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void appAndProfile() {
	this.controller = new EncryptionController(
			new SingleTextEncryptorLocator(new RsaSecretEncryptor()));
	// Add space to input
	String cipher = this.controller.encrypt("app", "default", "foo bar",
			MediaType.TEXT_PLAIN);
	String decrypt = this.controller.decrypt("app", "default", cipher,
			MediaType.TEXT_PLAIN);
	assertThat(decrypt).as("Wrong decrypted plaintext: " + decrypt)
			.isEqualTo("foo bar");
}
 
Example #9
Source File: EncryptionControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void formDataIn() {
	this.controller = new EncryptionController(
			new SingleTextEncryptorLocator(new RsaSecretEncryptor()));
	// Add space to input
	String cipher = this.controller.encrypt("foo bar=",
			MediaType.APPLICATION_FORM_URLENCODED);
	String decrypt = this.controller.decrypt(cipher + "=",
			MediaType.APPLICATION_FORM_URLENCODED);
	assertThat(decrypt).as("Wrong decrypted plaintext: " + decrypt)
			.isEqualTo("foo bar");
}
 
Example #10
Source File: EncryptionControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void formDataInWithPrefix() {
	this.controller = new EncryptionController(
			new SingleTextEncryptorLocator(new RsaSecretEncryptor()));
	// Add space to input
	String cipher = this.controller.encrypt("{key:test}foo bar=",
			MediaType.APPLICATION_FORM_URLENCODED);
	String decrypt = this.controller.decrypt(cipher + "=",
			MediaType.APPLICATION_FORM_URLENCODED);
	assertThat(decrypt).as("Wrong decrypted plaintext: " + decrypt)
			.isEqualTo("foo bar");
}
 
Example #11
Source File: EncryptionControllerTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void encryptDecyptTextWithCurlyBrace() {
	this.controller = new EncryptionController(
			new SingleTextEncryptorLocator(new RsaSecretEncryptor()));

	String plain = "textwith}brace";

	String cipher = this.controller.encrypt(plain,
			MediaType.APPLICATION_FORM_URLENCODED);
	String decrypt = this.controller.decrypt(cipher,
			MediaType.APPLICATION_FORM_URLENCODED);
	assertThat(decrypt).isEqualTo(plain);
}
 
Example #12
Source File: KeyStoreTextEncryptorLocator.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
private RsaSecretEncryptor rsaSecretEncryptor(String alias, String secret) {
	return new RsaSecretEncryptor(
			this.keys.getKeyPair(alias, this.secretLocator.locate(secret)),
			this.rsaAlgorithm, this.salt, this.strong);
}
 
Example #13
Source File: EncryptionController.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
private void checkDecryptionPossible(TextEncryptor textEncryptor) {
	if (textEncryptor instanceof RsaSecretEncryptor
			&& !((RsaSecretEncryptor) textEncryptor).canDecrypt()) {
		throw new DecryptionNotSupportedException();
	}
}
 
Example #14
Source File: EncryptionControllerTests.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
@Test(expected = InvalidCipherException.class)
public void shouldThrowExceptionOnDecryptInvalidData() {
	this.controller = new EncryptionController(
			new SingleTextEncryptorLocator(new RsaSecretEncryptor()));
	this.controller.decrypt("foo", MediaType.TEXT_PLAIN);
}