Spring Security Crypto provides cryptographic utilities including password encoding, key generation, encryption, and various hashing functions
—
Symmetric encryption services for both text and binary data using AES and RSA algorithms with various cipher modes and configurations.
Service interface for symmetric data encryption of byte arrays.
/**
* Service interface for symmetric data encryption
*/
interface BytesEncryptor {
/**
* Encrypt the byte array
* @param byteArray the bytes to encrypt
* @return the encrypted bytes
*/
byte[] encrypt(byte[] byteArray);
/**
* Decrypt the byte array
* @param encryptedByteArray the encrypted bytes
* @return the decrypted bytes
*/
byte[] decrypt(byte[] encryptedByteArray);
}Service interface for symmetric text encryption.
/**
* Service interface for symmetric text encryption
*/
interface TextEncryptor {
/**
* Encrypt the text string
* @param text the text to encrypt
* @return the encrypted text
*/
String encrypt(String text);
/**
* Decrypt the text string
* @param encryptedText the encrypted text
* @return the decrypted text
*/
String decrypt(String encryptedText);
}Factory for commonly used encryptor implementations.
/**
* Factory for commonly used encryptors
*/
class Encryptors {
/**
* Creates a 256-bit AES encryptor using GCM mode
* @param password the password to use to generate the key
* @param salt the salt to use to generate the key
* @return the bytes encryptor
*/
static BytesEncryptor stronger(CharSequence password, CharSequence salt);
/**
* Creates a standard 256-bit AES encryptor
* @param password the password to use to generate the key
* @param salt the salt to use to generate the key
* @return the bytes encryptor
*/
static BytesEncryptor standard(CharSequence password, CharSequence salt);
/**
* Creates a stronger text encryptor using AES/GCM
* @param password the password to use to generate the key
* @param salt the salt to use to generate the key
* @return the text encryptor
*/
static TextEncryptor delux(CharSequence password, CharSequence salt);
/**
* Creates a standard text encryptor using AES/CBC
* @param password the password to use to generate the key
* @param salt the salt to use to generate the key
* @return the text encryptor
*/
static TextEncryptor text(CharSequence password, CharSequence salt);
/**
* Creates a no-operation text encryptor (for testing)
* @return the no-op text encryptor
*/
static TextEncryptor noOpText();
}Usage Example:
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.crypto.encrypt.BytesEncryptor;
// Text encryption
TextEncryptor textEncryptor = Encryptors.text("myPassword", "mySalt");
String encrypted = textEncryptor.encrypt("Hello World");
String decrypted = textEncryptor.decrypt(encrypted);
// Stronger text encryption with GCM
TextEncryptor strongerTextEncryptor = Encryptors.delux("myPassword", "mySalt");
String strongerEncrypted = strongerTextEncryptor.encrypt("Sensitive Data");
// Bytes encryption
BytesEncryptor bytesEncryptor = Encryptors.standard("myPassword", "mySalt");
byte[] data = "Hello World".getBytes();
byte[] encryptedBytes = bytesEncryptor.encrypt(data);
byte[] decryptedBytes = bytesEncryptor.decrypt(encryptedBytes);AES-based bytes encryptor with configurable cipher algorithms.
/**
* An Encryptor equivalent to AES/CBC/PKCS5Padding or AES/GCM/NoPadding
*/
class AesBytesEncryptor implements BytesEncryptor {
/**
* Creates an AES bytes encryptor
* @param password the password to derive the key from
* @param salt the salt to use for key derivation
*/
AesBytesEncryptor(String password, CharSequence salt);
/**
* Creates an AES bytes encryptor with cipher algorithm
* @param password the password to derive the key from
* @param salt the salt to use for key derivation
* @param algorithm the cipher algorithm to use
*/
AesBytesEncryptor(String password, CharSequence salt, CipherAlgorithm algorithm);
enum CipherAlgorithm {
CBC("AES/CBC/PKCS5Padding"),
GCM("AES/GCM/NoPadding");
private final String algorithm;
CipherAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
public String getAlgorithm() {
return this.algorithm;
}
}
}RSA-based encryption implementations for asymmetric cryptography.
/**
* Interface for RSA key holders
*/
interface RsaKeyHolder {
String getPublicKey();
PrivateKey getPrivateKey();
}
/**
* RSA raw encryption implementation
*/
class RsaRawEncryptor implements BytesEncryptor, TextEncryptor, RsaKeyHolder {
/**
* Creates an RSA raw encryptor
* @param publicKey the public key for encryption
* @param privateKey the private key for decryption
*/
RsaRawEncryptor(RSAPublicKey publicKey, RSAPrivateKey privateKey);
/**
* Creates an RSA raw encryptor with algorithm
* @param publicKey the public key for encryption
* @param privateKey the private key for decryption
* @param algorithm the RSA algorithm to use
*/
RsaRawEncryptor(RSAPublicKey publicKey, RSAPrivateKey privateKey, RsaAlgorithm algorithm);
}
/**
* RSA secret encryption implementation
*/
class RsaSecretEncryptor implements BytesEncryptor, TextEncryptor, RsaKeyHolder {
/**
* Creates an RSA secret encryptor
* @param publicKey the public key for encryption
* @param privateKey the private key for decryption
* @param secret the secret key for AES encryption
* @param salt the salt for key derivation
*/
RsaSecretEncryptor(RSAPublicKey publicKey, RSAPrivateKey privateKey, String secret, CharSequence salt);
}
/**
* RSA algorithm specifications
*/
enum RsaAlgorithm {
DEFAULT("RSA"),
OAEP("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
private final String algorithm;
RsaAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
public String getAlgorithm() {
return this.algorithm;
}
}BouncyCastle-based AES encryptor implementations.
/**
* BouncyCastle AES bytes encryptor base class
*/
abstract class BouncyCastleAesBytesEncryptor implements BytesEncryptor {
/**
* Creates a BouncyCastle AES encryptor
* @param password the password for key derivation
* @param salt the salt for key derivation
*/
BouncyCastleAesBytesEncryptor(String password, CharSequence salt);
}
/**
* BouncyCastle AES CBC implementation
*/
class BouncyCastleAesCbcBytesEncryptor extends BouncyCastleAesBytesEncryptor {
BouncyCastleAesCbcBytesEncryptor(String password, CharSequence salt);
}
/**
* BouncyCastle AES GCM implementation
*/
class BouncyCastleAesGcmBytesEncryptor extends BouncyCastleAesBytesEncryptor {
BouncyCastleAesGcmBytesEncryptor(String password, CharSequence salt);
}Text encryptor that applies hex encoding to encrypted bytes.
/**
* A TextEncryptor that applies hex encoding to encrypted bytes
*/
class HexEncodingTextEncryptor implements TextEncryptor {
/**
* Creates a hex encoding text encryptor
* @param encryptor the underlying bytes encryptor
*/
HexEncodingTextEncryptor(BytesEncryptor encryptor);
}Factory for creating keys from a keystore.
/**
* A factory for creating key pairs and public/private keys from a keystore
*/
class KeyStoreKeyFactory {
/**
* Creates a keystore key factory
* @param resource the keystore resource
* @param password the keystore password
*/
KeyStoreKeyFactory(Resource resource, char[] password);
/**
* Gets a key pair from the keystore
* @param alias the key alias
* @return the key pair
*/
KeyPair getKeyPair(String alias);
/**
* Gets a key pair with custom password
* @param alias the key alias
* @param password the key password
* @return the key pair
*/
KeyPair getKeyPair(String alias, char[] password);
}Usage Example:
import org.springframework.security.crypto.encrypt.*;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
// RSA encryption setup
KeyPair keyPair = generateRSAKeyPair(); // your key generation logic
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RsaRawEncryptor rsaEncryptor = new RsaRawEncryptor(publicKey, privateKey);
String encrypted = rsaEncryptor.encrypt("Secret Message");
String decrypted = rsaEncryptor.decrypt(encrypted);
// AES with specific algorithm
AesBytesEncryptor aesEncryptor = new AesBytesEncryptor("password", "salt",
AesBytesEncryptor.CipherAlgorithm.GCM);
byte[] encryptedData = aesEncryptor.encrypt("data".getBytes());
byte[] decryptedData = aesEncryptor.decrypt(encryptedData);Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-security--spring-security-crypto