0
# Spring Security Crypto
1
2
Spring Security Crypto provides a comprehensive suite of cryptographic utilities for Spring Security applications, including secure password encoding algorithms, key generation utilities, encryption/decryption services, and various encoding/decoding functions. The library is designed with security best practices and provides industry-standard cryptographic implementations.
3
4
## Package Information
5
6
- **Package Name**: spring-security-crypto
7
- **Package Type**: maven
8
- **Language**: Java
9
- **Installation**:
10
```xml
11
<dependency>
12
<groupId>org.springframework.security</groupId>
13
<artifactId>spring-security-crypto</artifactId>
14
<version>6.5.1</version>
15
</dependency>
16
```
17
18
## Core Imports
19
20
```java
21
// Password encoding
22
import org.springframework.security.crypto.password.PasswordEncoder;
23
import org.springframework.security.crypto.password.BCryptPasswordEncoder;
24
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
25
26
// Encryption
27
import org.springframework.security.crypto.encrypt.BytesEncryptor;
28
import org.springframework.security.crypto.encrypt.TextEncryptor;
29
import org.springframework.security.crypto.encrypt.Encryptors;
30
31
// Key generation
32
import org.springframework.security.crypto.keygen.BytesKeyGenerator;
33
import org.springframework.security.crypto.keygen.StringKeyGenerator;
34
import org.springframework.security.crypto.keygen.KeyGenerators;
35
36
// Codec utilities
37
import org.springframework.security.crypto.codec.Base64;
38
import org.springframework.security.crypto.codec.Hex;
39
import org.springframework.security.crypto.codec.Utf8;
40
import org.springframework.security.crypto.util.EncodingUtils;
41
```
42
43
## Basic Usage
44
45
```java
46
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
47
import org.springframework.security.crypto.password.PasswordEncoder;
48
import org.springframework.security.crypto.encrypt.Encryptors;
49
import org.springframework.security.crypto.encrypt.TextEncryptor;
50
import org.springframework.security.crypto.keygen.KeyGenerators;
51
import org.springframework.security.crypto.keygen.StringKeyGenerator;
52
53
// Password encoding
54
PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
55
String encodedPassword = passwordEncoder.encode("mySecretPassword");
56
boolean matches = passwordEncoder.matches("mySecretPassword", encodedPassword);
57
58
// Text encryption
59
TextEncryptor textEncryptor = Encryptors.text("myPassword", "mySalt");
60
String encrypted = textEncryptor.encrypt("Hello World");
61
String decrypted = textEncryptor.decrypt(encrypted);
62
63
// Key generation
64
StringKeyGenerator keyGenerator = KeyGenerators.string();
65
String generatedKey = keyGenerator.generateKey();
66
```
67
68
## Architecture
69
70
Spring Security Crypto is organized around several key components:
71
72
- **Password Encoding**: Secure password hashing with multiple algorithm support (BCrypt, Argon2, SCrypt, PBKDF2)
73
- **Encryption Services**: Symmetric encryption for both text and binary data using AES and RSA
74
- **Key Generation**: Secure random key generation for cryptographic operations
75
- **Codec Utilities**: Base64, Hex, and UTF-8 encoding/decoding functions
76
- **Factory Pattern**: Convenient factory classes for creating pre-configured instances
77
- **Delegation Pattern**: Flexible encoder selection based on algorithm identifiers
78
79
## Capabilities
80
81
### Password Encoding
82
83
Secure password hashing with support for multiple industry-standard algorithms including BCrypt, Argon2, SCrypt, and PBKDF2.
84
85
```java { .api }
86
interface PasswordEncoder {
87
String encode(CharSequence rawPassword);
88
boolean matches(CharSequence rawPassword, String encodedPassword);
89
default boolean upgradeEncoding(String encodedPassword);
90
}
91
```
92
93
[Password Encoding](./password-encoding.md)
94
95
### Encryption and Decryption
96
97
Symmetric encryption services for both text and binary data using AES and RSA algorithms with various cipher modes.
98
99
```java { .api }
100
interface BytesEncryptor {
101
byte[] encrypt(byte[] byteArray);
102
byte[] decrypt(byte[] encryptedByteArray);
103
}
104
105
interface TextEncryptor {
106
String encrypt(String text);
107
String decrypt(String encryptedText);
108
}
109
```
110
111
[Encryption and Decryption](./encryption.md)
112
113
### Key Generation
114
115
Secure random key generation utilities for creating cryptographic keys and salts.
116
117
```java { .api }
118
interface BytesKeyGenerator {
119
int getKeyLength();
120
byte[] generateKey();
121
}
122
123
interface StringKeyGenerator {
124
String generateKey();
125
}
126
```
127
128
[Key Generation](./key-generation.md)
129
130
### Codec Utilities
131
132
Encoding and decoding utilities for Base64, Hexadecimal, and UTF-8 conversions.
133
134
```java { .api }
135
class Base64 {
136
static byte[] encode(byte[] src);
137
static byte[] decode(byte[] src);
138
static boolean isBase64(byte[] bytes);
139
}
140
141
class Hex {
142
static char[] encode(byte[] bytes);
143
static byte[] decode(CharSequence hexString);
144
}
145
146
class Utf8 {
147
static byte[] encode(CharSequence string);
148
static String decode(byte[] bytes);
149
}
150
151
class EncodingUtils {
152
static byte[] concatenate(byte[]... arrays);
153
static byte[] subArray(byte[] array, int beginIndex, int endIndex);
154
}
155
```
156
157
[Codec Utilities](./codecs.md)