CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-cn-hutool--hutool-all

A comprehensive Java utility library providing static method wrappers for common operations to reduce API learning costs and improve development efficiency

Pending
Overview
Eval results
Files

cryptographic-operations.mddocs/

Cryptographic Operations

Comprehensive cryptographic utilities through SecureUtil, SignUtil, and SmUtil classes, providing encryption, decryption, hashing, and digital signature operations.

Import

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SignUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.crypto.asymmetric.AsymmetricCrypto;

Hash Operations

Message Digest

// MD5 hashing
public static String md5(String data);
public static String md5(File file);
public static String md5(InputStream data);

// SHA hashing
public static String sha1(String data);
public static String sha256(String data);
public static String sha512(String data);

// Generic digest
public static String digestHex(String algorithm, String data);
public static byte[] digest(String algorithm, byte[] data);

HMAC Operations

// HMAC with various algorithms
public static String hmacMd5(String data, String key);
public static String hmacSha1(String data, String key);
public static String hmacSha256(String data, String key);

// Generic HMAC
public static String hmac(String algorithm, String data, String key);
public static byte[] hmac(String algorithm, byte[] data, byte[] key);

Usage Examples:

// Basic hashing
String password = "myPassword123";
String md5Hash = SecureUtil.md5(password);
String sha256Hash = SecureUtil.sha256(password);

// File hashing for integrity verification
String fileHash = SecureUtil.sha256(new File("document.pdf"));

// HMAC for message authentication
String message = "Important data";
String secretKey = "mySecretKey";
String hmac = SecureUtil.hmacSha256(message, secretKey);

Symmetric Encryption

AES Encryption

// Create AES crypto instances
public static SymmetricCrypto aes(byte[] key);
public static SymmetricCrypto aes(String key);
public static SymmetricCrypto aes();  // Generate random key

// DES encryption  
public static SymmetricCrypto des(byte[] key);
public static SymmetricCrypto des(String key);
public static SymmetricCrypto des();  // Generate random key

// Generic symmetric encryption
public static SymmetricCrypto symmetric(String algorithm, byte[] key);

SymmetricCrypto Class

public class SymmetricCrypto {
    // Encryption
    public byte[] encrypt(byte[] data);
    public String encryptHex(String data);
    public String encryptBase64(String data);
    
    // Decryption
    public byte[] decrypt(byte[] data);
    public String decryptStr(byte[] data);
    public String decryptStr(String data);
    public String decryptStrFromHex(String data);
    public String decryptStrFromBase64(String data);
    
    // Key management
    public byte[] getSecretKey();
    public String getSecretKeyStr();
}

Usage Examples:

// AES encryption with auto-generated key
SymmetricCrypto aes = SecureUtil.aes();
String originalText = "Confidential data";

// Encrypt to different formats
String encryptedHex = aes.encryptHex(originalText);
String encryptedBase64 = aes.encryptBase64(originalText);

// Decrypt back to original
String decrypted = aes.decryptStrFromHex(encryptedHex);
assert originalText.equals(decrypted);

// AES with custom key
byte[] key = SecureUtil.generateKey("AES", 256).getEncoded();
SymmetricCrypto customAes = SecureUtil.aes(key);
String encrypted = customAes.encryptBase64("Secret message");

Asymmetric Encryption

RSA Encryption

// Create RSA crypto instances
public static AsymmetricCrypto rsa();
public static AsymmetricCrypto rsa(String privateKeyStr, String publicKeyStr);
public static AsymmetricCrypto rsa(byte[] privateKey, byte[] publicKey);

// Key pair generation
public static KeyPair generateKeyPair(String algorithm);
public static KeyPair generateKeyPair(String algorithm, int keySize);

AsymmetricCrypto Class

public class AsymmetricCrypto {
    // Encryption (typically with public key)
    public byte[] encrypt(byte[] data, KeyType keyType);
    public String encryptHex(String data, KeyType keyType);
    public String encryptBase64(String data, KeyType keyType);
    
    // Decryption (typically with private key)
    public byte[] decrypt(byte[] data, KeyType keyType);
    public String decryptStr(byte[] data, KeyType keyType);
    public String decryptStrFromHex(String data, KeyType keyType);
    public String decryptStrFromBase64(String data, KeyType keyType);
    
    // Key management
    public PrivateKey getPrivateKey();
    public PublicKey getPublicKey();
    public String getPrivateKeyBase64();
    public String getPublicKeyBase64();
}

Usage Examples:

// Generate RSA key pair
AsymmetricCrypto rsa = SecureUtil.rsa();

// Get keys for sharing
String publicKeyStr = rsa.getPublicKeyBase64();
String privateKeyStr = rsa.getPrivateKeyBase64();

// Encrypt with public key, decrypt with private key
String message = "Confidential message";
String encrypted = rsa.encryptBase64(message, KeyType.PublicKey);
String decrypted = rsa.decryptStrFromBase64(encrypted, KeyType.PrivateKey);

// Load existing key pair
AsymmetricCrypto existingRsa = SecureUtil.rsa(privateKeyStr, publicKeyStr);

Digital Signatures

Signature Creation and Verification

// Create signatures
public static String sign(String data, PrivateKey privateKey, String algorithm);
public static byte[] sign(byte[] data, PrivateKey privateKey, String algorithm);

// Verify signatures
public static boolean verify(String data, String signature, PublicKey publicKey, String algorithm);
public static boolean verify(byte[] data, byte[] signature, PublicKey publicKey, String algorithm);

// Common signature algorithms
public static String signSHA256withRSA(String data, PrivateKey privateKey);
public static boolean verifySHA256withRSA(String data, String signature, PublicKey publicKey);

Usage Examples:

// Generate key pair for signing
KeyPair keyPair = SecureUtil.generateKeyPair("RSA", 2048);
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();

// Create digital signature
String document = "Important contract terms...";
String signature = SignUtil.signSHA256withRSA(document, privateKey);

// Verify signature
boolean isValid = SignUtil.verifySHA256withRSA(document, signature, publicKey);
System.out.println("Signature valid: " + isValid);

Chinese Cryptographic Standards (SM)

SM2, SM3, SM4 Algorithms

// SM2 (Chinese elliptic curve public key cryptography)
public static SM2 sm2();
public static SM2 sm2(byte[] privateKey, byte[] publicKey);

// SM3 (Chinese hash algorithm)
public static String sm3(String data);
public static String sm3(File file);

// SM4 (Chinese symmetric encryption)
public static SM4 sm4();
public static SM4 sm4(byte[] key);

Usage Examples:

// SM3 hashing (Chinese national standard)
String data = "测试数据";
String sm3Hash = SmUtil.sm3(data);

// SM4 symmetric encryption
SM4 sm4 = SmUtil.sm4();
String plaintext = "机密信息";
String encrypted = sm4.encryptBase64(plaintext);
String decrypted = sm4.decryptStrFromBase64(encrypted);

// SM2 public key cryptography
SM2 sm2 = SmUtil.sm2();
String message = "重要消息";
String encryptedMsg = sm2.encryptBase64(message, KeyType.PublicKey);
String decryptedMsg = sm2.decryptStrFromBase64(encryptedMsg, KeyType.PrivateKey);

Key Management

Key Generation

// Generate symmetric keys
public static SecretKey generateKey(String algorithm);
public static SecretKey generateKey(String algorithm, int keySize);

// Generate asymmetric keys  
public static KeyPair generateKeyPair(String algorithm);
public static KeyPair generateKeyPair(String algorithm, int keySize);

// Random data generation
public static byte[] generateKey(int keySize);
public static String generateToken();

Key Conversion

// Convert keys to strings
public static String encodeKey(Key key);
public static byte[] decodeKey(String keyStr);

// PEM format operations
public static PrivateKey readPrivateKey(String pemContent);
public static PublicKey readPublicKey(String pemContent);

Utilities and Constants

Algorithm Constants

// Symmetric algorithms
public static final String AES = "AES";
public static final String DES = "DES";
public static final String DESede = "DESede";

// Asymmetric algorithms  
public static final String RSA = "RSA";
public static final String DSA = "DSA";
public static final String ECDSA = "ECDSA";

// Hash algorithms
public static final String MD5 = "MD5";
public static final String SHA1 = "SHA-1";
public static final String SHA256 = "SHA-256";
public static final String SHA512 = "SHA-512";

// Signature algorithms
public static final String SHA256withRSA = "SHA256withRSA";
public static final String SHA256withDSA = "SHA256withDSA";
public static final String SHA256withECDSA = "SHA256withECDSA";

Encoding Utilities

// Base64 encoding/decoding
public static String encodeBase64(byte[] data);
public static byte[] decodeBase64(String base64);

// Hex encoding/decoding  
public static String encodeHex(byte[] data);
public static byte[] decodeHex(String hex);

Complete Usage Example:

public class CryptoExample {
    public void demonstrateCrypto() {
        // 1. Hash for integrity
        String document = "Important document content";
        String documentHash = SecureUtil.sha256(document);
        
        // 2. Symmetric encryption for bulk data
        SymmetricCrypto aes = SecureUtil.aes();
        String encryptedDocument = aes.encryptBase64(document);
        
        // 3. Asymmetric encryption for key exchange
        AsymmetricCrypto rsa = SecureUtil.rsa();
        String aesKeyBase64 = Base64.encode(aes.getSecretKey());
        String encryptedKey = rsa.encryptBase64(aesKeyBase64, KeyType.PublicKey);
        
        // 4. Digital signature for authenticity
        KeyPair signingKeys = SecureUtil.generateKeyPair("RSA", 2048);
        String signature = SignUtil.signSHA256withRSA(document, signingKeys.getPrivate());
        
        // 5. Verification
        boolean signatureValid = SignUtil.verifySHA256withRSA(
            document, signature, signingKeys.getPublic());
        
        System.out.println("Document hash: " + documentHash);
        System.out.println("Signature valid: " + signatureValid);
    }
}

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.

Install with Tessl CLI

npx tessl i tessl/maven-cn-hutool--hutool-all

docs

additional-utilities.md

bean-object-manipulation.md

collection-utilities.md

core-string-operations.md

cryptographic-operations.md

database-access.md

date-time-handling.md

file-io-operations.md

http-client-operations.md

index.md

json-processing.md

tile.json