0
# Cryptographic Operations
1
2
Comprehensive cryptographic utilities through `SecureUtil`, `SignUtil`, and `SmUtil` classes, providing encryption, decryption, hashing, and digital signature operations.
3
4
## Import
5
6
```java
7
import cn.hutool.crypto.SecureUtil;
8
import cn.hutool.crypto.SignUtil;
9
import cn.hutool.crypto.SmUtil;
10
import cn.hutool.crypto.symmetric.SymmetricCrypto;
11
import cn.hutool.crypto.asymmetric.AsymmetricCrypto;
12
```
13
14
## Hash Operations
15
16
### Message Digest
17
18
```java { .api }
19
// MD5 hashing
20
public static String md5(String data);
21
public static String md5(File file);
22
public static String md5(InputStream data);
23
24
// SHA hashing
25
public static String sha1(String data);
26
public static String sha256(String data);
27
public static String sha512(String data);
28
29
// Generic digest
30
public static String digestHex(String algorithm, String data);
31
public static byte[] digest(String algorithm, byte[] data);
32
```
33
34
### HMAC Operations
35
36
```java { .api }
37
// HMAC with various algorithms
38
public static String hmacMd5(String data, String key);
39
public static String hmacSha1(String data, String key);
40
public static String hmacSha256(String data, String key);
41
42
// Generic HMAC
43
public static String hmac(String algorithm, String data, String key);
44
public static byte[] hmac(String algorithm, byte[] data, byte[] key);
45
```
46
47
**Usage Examples:**
48
49
```java
50
// Basic hashing
51
String password = "myPassword123";
52
String md5Hash = SecureUtil.md5(password);
53
String sha256Hash = SecureUtil.sha256(password);
54
55
// File hashing for integrity verification
56
String fileHash = SecureUtil.sha256(new File("document.pdf"));
57
58
// HMAC for message authentication
59
String message = "Important data";
60
String secretKey = "mySecretKey";
61
String hmac = SecureUtil.hmacSha256(message, secretKey);
62
```
63
64
## Symmetric Encryption
65
66
### AES Encryption
67
68
```java { .api }
69
// Create AES crypto instances
70
public static SymmetricCrypto aes(byte[] key);
71
public static SymmetricCrypto aes(String key);
72
public static SymmetricCrypto aes(); // Generate random key
73
74
// DES encryption
75
public static SymmetricCrypto des(byte[] key);
76
public static SymmetricCrypto des(String key);
77
public static SymmetricCrypto des(); // Generate random key
78
79
// Generic symmetric encryption
80
public static SymmetricCrypto symmetric(String algorithm, byte[] key);
81
```
82
83
### SymmetricCrypto Class
84
85
```java { .api }
86
public class SymmetricCrypto {
87
// Encryption
88
public byte[] encrypt(byte[] data);
89
public String encryptHex(String data);
90
public String encryptBase64(String data);
91
92
// Decryption
93
public byte[] decrypt(byte[] data);
94
public String decryptStr(byte[] data);
95
public String decryptStr(String data);
96
public String decryptStrFromHex(String data);
97
public String decryptStrFromBase64(String data);
98
99
// Key management
100
public byte[] getSecretKey();
101
public String getSecretKeyStr();
102
}
103
```
104
105
**Usage Examples:**
106
107
```java
108
// AES encryption with auto-generated key
109
SymmetricCrypto aes = SecureUtil.aes();
110
String originalText = "Confidential data";
111
112
// Encrypt to different formats
113
String encryptedHex = aes.encryptHex(originalText);
114
String encryptedBase64 = aes.encryptBase64(originalText);
115
116
// Decrypt back to original
117
String decrypted = aes.decryptStrFromHex(encryptedHex);
118
assert originalText.equals(decrypted);
119
120
// AES with custom key
121
byte[] key = SecureUtil.generateKey("AES", 256).getEncoded();
122
SymmetricCrypto customAes = SecureUtil.aes(key);
123
String encrypted = customAes.encryptBase64("Secret message");
124
```
125
126
## Asymmetric Encryption
127
128
### RSA Encryption
129
130
```java { .api }
131
// Create RSA crypto instances
132
public static AsymmetricCrypto rsa();
133
public static AsymmetricCrypto rsa(String privateKeyStr, String publicKeyStr);
134
public static AsymmetricCrypto rsa(byte[] privateKey, byte[] publicKey);
135
136
// Key pair generation
137
public static KeyPair generateKeyPair(String algorithm);
138
public static KeyPair generateKeyPair(String algorithm, int keySize);
139
```
140
141
### AsymmetricCrypto Class
142
143
```java { .api }
144
public class AsymmetricCrypto {
145
// Encryption (typically with public key)
146
public byte[] encrypt(byte[] data, KeyType keyType);
147
public String encryptHex(String data, KeyType keyType);
148
public String encryptBase64(String data, KeyType keyType);
149
150
// Decryption (typically with private key)
151
public byte[] decrypt(byte[] data, KeyType keyType);
152
public String decryptStr(byte[] data, KeyType keyType);
153
public String decryptStrFromHex(String data, KeyType keyType);
154
public String decryptStrFromBase64(String data, KeyType keyType);
155
156
// Key management
157
public PrivateKey getPrivateKey();
158
public PublicKey getPublicKey();
159
public String getPrivateKeyBase64();
160
public String getPublicKeyBase64();
161
}
162
```
163
164
**Usage Examples:**
165
166
```java
167
// Generate RSA key pair
168
AsymmetricCrypto rsa = SecureUtil.rsa();
169
170
// Get keys for sharing
171
String publicKeyStr = rsa.getPublicKeyBase64();
172
String privateKeyStr = rsa.getPrivateKeyBase64();
173
174
// Encrypt with public key, decrypt with private key
175
String message = "Confidential message";
176
String encrypted = rsa.encryptBase64(message, KeyType.PublicKey);
177
String decrypted = rsa.decryptStrFromBase64(encrypted, KeyType.PrivateKey);
178
179
// Load existing key pair
180
AsymmetricCrypto existingRsa = SecureUtil.rsa(privateKeyStr, publicKeyStr);
181
```
182
183
## Digital Signatures
184
185
### Signature Creation and Verification
186
187
```java { .api }
188
// Create signatures
189
public static String sign(String data, PrivateKey privateKey, String algorithm);
190
public static byte[] sign(byte[] data, PrivateKey privateKey, String algorithm);
191
192
// Verify signatures
193
public static boolean verify(String data, String signature, PublicKey publicKey, String algorithm);
194
public static boolean verify(byte[] data, byte[] signature, PublicKey publicKey, String algorithm);
195
196
// Common signature algorithms
197
public static String signSHA256withRSA(String data, PrivateKey privateKey);
198
public static boolean verifySHA256withRSA(String data, String signature, PublicKey publicKey);
199
```
200
201
**Usage Examples:**
202
203
```java
204
// Generate key pair for signing
205
KeyPair keyPair = SecureUtil.generateKeyPair("RSA", 2048);
206
PrivateKey privateKey = keyPair.getPrivate();
207
PublicKey publicKey = keyPair.getPublic();
208
209
// Create digital signature
210
String document = "Important contract terms...";
211
String signature = SignUtil.signSHA256withRSA(document, privateKey);
212
213
// Verify signature
214
boolean isValid = SignUtil.verifySHA256withRSA(document, signature, publicKey);
215
System.out.println("Signature valid: " + isValid);
216
```
217
218
## Chinese Cryptographic Standards (SM)
219
220
### SM2, SM3, SM4 Algorithms
221
222
```java { .api }
223
// SM2 (Chinese elliptic curve public key cryptography)
224
public static SM2 sm2();
225
public static SM2 sm2(byte[] privateKey, byte[] publicKey);
226
227
// SM3 (Chinese hash algorithm)
228
public static String sm3(String data);
229
public static String sm3(File file);
230
231
// SM4 (Chinese symmetric encryption)
232
public static SM4 sm4();
233
public static SM4 sm4(byte[] key);
234
```
235
236
**Usage Examples:**
237
238
```java
239
// SM3 hashing (Chinese national standard)
240
String data = "测试数据";
241
String sm3Hash = SmUtil.sm3(data);
242
243
// SM4 symmetric encryption
244
SM4 sm4 = SmUtil.sm4();
245
String plaintext = "机密信息";
246
String encrypted = sm4.encryptBase64(plaintext);
247
String decrypted = sm4.decryptStrFromBase64(encrypted);
248
249
// SM2 public key cryptography
250
SM2 sm2 = SmUtil.sm2();
251
String message = "重要消息";
252
String encryptedMsg = sm2.encryptBase64(message, KeyType.PublicKey);
253
String decryptedMsg = sm2.decryptStrFromBase64(encryptedMsg, KeyType.PrivateKey);
254
```
255
256
## Key Management
257
258
### Key Generation
259
260
```java { .api }
261
// Generate symmetric keys
262
public static SecretKey generateKey(String algorithm);
263
public static SecretKey generateKey(String algorithm, int keySize);
264
265
// Generate asymmetric keys
266
public static KeyPair generateKeyPair(String algorithm);
267
public static KeyPair generateKeyPair(String algorithm, int keySize);
268
269
// Random data generation
270
public static byte[] generateKey(int keySize);
271
public static String generateToken();
272
```
273
274
### Key Conversion
275
276
```java { .api }
277
// Convert keys to strings
278
public static String encodeKey(Key key);
279
public static byte[] decodeKey(String keyStr);
280
281
// PEM format operations
282
public static PrivateKey readPrivateKey(String pemContent);
283
public static PublicKey readPublicKey(String pemContent);
284
```
285
286
## Utilities and Constants
287
288
### Algorithm Constants
289
290
```java { .api }
291
// Symmetric algorithms
292
public static final String AES = "AES";
293
public static final String DES = "DES";
294
public static final String DESede = "DESede";
295
296
// Asymmetric algorithms
297
public static final String RSA = "RSA";
298
public static final String DSA = "DSA";
299
public static final String ECDSA = "ECDSA";
300
301
// Hash algorithms
302
public static final String MD5 = "MD5";
303
public static final String SHA1 = "SHA-1";
304
public static final String SHA256 = "SHA-256";
305
public static final String SHA512 = "SHA-512";
306
307
// Signature algorithms
308
public static final String SHA256withRSA = "SHA256withRSA";
309
public static final String SHA256withDSA = "SHA256withDSA";
310
public static final String SHA256withECDSA = "SHA256withECDSA";
311
```
312
313
### Encoding Utilities
314
315
```java { .api }
316
// Base64 encoding/decoding
317
public static String encodeBase64(byte[] data);
318
public static byte[] decodeBase64(String base64);
319
320
// Hex encoding/decoding
321
public static String encodeHex(byte[] data);
322
public static byte[] decodeHex(String hex);
323
```
324
325
**Complete Usage Example:**
326
327
```java
328
public class CryptoExample {
329
public void demonstrateCrypto() {
330
// 1. Hash for integrity
331
String document = "Important document content";
332
String documentHash = SecureUtil.sha256(document);
333
334
// 2. Symmetric encryption for bulk data
335
SymmetricCrypto aes = SecureUtil.aes();
336
String encryptedDocument = aes.encryptBase64(document);
337
338
// 3. Asymmetric encryption for key exchange
339
AsymmetricCrypto rsa = SecureUtil.rsa();
340
String aesKeyBase64 = Base64.encode(aes.getSecretKey());
341
String encryptedKey = rsa.encryptBase64(aesKeyBase64, KeyType.PublicKey);
342
343
// 4. Digital signature for authenticity
344
KeyPair signingKeys = SecureUtil.generateKeyPair("RSA", 2048);
345
String signature = SignUtil.signSHA256withRSA(document, signingKeys.getPrivate());
346
347
// 5. Verification
348
boolean signatureValid = SignUtil.verifySHA256withRSA(
349
document, signature, signingKeys.getPublic());
350
351
System.out.println("Document hash: " + documentHash);
352
System.out.println("Signature valid: " + signatureValid);
353
}
354
}
355
```
356
357
All cryptographic operations follow industry standards and provide secure defaults. The library handles padding, initialization vectors, and other cryptographic parameters automatically while allowing customization when needed.