概要
Shiro专注于密码学的两个核心要素:使用公钥或私钥加密数据的密码,以及对密码等数据进行不可逆加密的哈希。
Jce加密数据Demo
1package com.wjz.demo.crypto; 2 3import java.security.Key; 4import java.security.SecureRandom; 5 6import javax.crypto.Cipher; 7import javax.crypto.KeyGenerator; 8import javax.crypto.SecretKey; 9import javax.crypto.spec.IvParameterSpec; 10import javax.crypto.spec.SecretKeySpec; 11 12public class JceDemo { 13 14 private static final String ALGORITHM_NAME = "AES"; 15 private static final String TRANSFORMATION_STRING_DELIMITER = "/"; 16 private static final String RANDOM_NUM_GENERATOR_ALGORITHM_NAME = "SHA1PRNG"; 17 private static final int DEFAULT_KEY_SIZE = 128; 18 private static final int BITS_PER_BYTE = 8; 19 private static byte[] encryptionCipherKey; 20 private static byte[] decryptionCipherKey; 21 private static String modeName = "CBC"; 22 private static String paddingSchemeName = "PKCS5Padding"; 23 private static String transformationString = ALGORITHM_NAME + TRANSFORMATION_STRING_DELIMITER + modeName 24 + TRANSFORMATION_STRING_DELIMITER + paddingSchemeName; 25 26 public static void main(String[] args) throws Exception { 27 KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_NAME); 28 keyGenerator.init(DEFAULT_KEY_SIZE); 29 SecretKey key = keyGenerator.generateKey(); 30 encryptionCipherKey = key.getEncoded(); 31 decryptionCipherKey = key.getEncoded(); 32 33 int sizeInBytes = DEFAULT_KEY_SIZE / BITS_PER_BYTE; 34 byte[] ivBytes = new byte[sizeInBytes]; 35 SecureRandom random = SecureRandom.getInstance(RANDOM_NUM_GENERATOR_ALGORITHM_NAME); 36 random.nextBytes(ivBytes); 37 38 Cipher cipher = Cipher.getInstance(transformationString); 39 Key jdkKey = new SecretKeySpec(encryptionCipherKey, ALGORITHM_NAME); 40 IvParameterSpec spec = new IvParameterSpec(ivBytes); 41 cipher.init(Cipher.ENCRYPT_MODE, jdkKey, spec, random); 42 43 byte[] encrypted = cipher.doFinal("Shiro安全加密".getBytes()); 44 45 byte[] output = new byte[ivBytes.length + encrypted.length]; 46 System.arraycopy(ivBytes, 0, output, 0, ivBytes.length); 47 System.arraycopy(encrypted, 0, output, ivBytes.length, encrypted.length); 48 49 byte[] input = de(output); 50 51 System.out.println(new String(input)); 52 } 53 54 public static byte[] de(byte[] output) throws Exception { 55 int sizeInBytes = DEFAULT_KEY_SIZE / BITS_PER_BYTE; 56 byte[] ivBytes = new byte[sizeInBytes]; 57 System.arraycopy(output, 0, ivBytes, 0, sizeInBytes); 58 int encryptedSize = output.length - sizeInBytes; 59 byte[] encrypted = new byte[encryptedSize]; 60 System.arraycopy(output, sizeInBytes, encrypted, 0, encryptedSize); 61 62 Cipher cipher = Cipher.getInstance(transformationString); 63 Key jdkKey = new SecretKeySpec(decryptionCipherKey, ALGORITHM_NAME); 64 IvParameterSpec spec = new IvParameterSpec(ivBytes); 65 cipher.init(Cipher.DECRYPT_MODE, jdkKey, spec); 66 67 byte[] decrypted = cipher.doFinal(encrypted); 68 69 return decrypted; 70 } 71 72}
AesCipherService

DefaultBlockCipherService

定义加密模式(默认CBC),填充方案(默认PKCS5),块个数(默认0)
AbstractSymmetricCipherService

生成密钥
JcaCipherService

加密、解密
CipherService

Hash

其主要行为是获得Hash加密所需要的元素,算法名称、佐料、加密次数

SimpleHash

其功能是根据加密所需的元素进行加密(使用java.security.*的API),将加密后的byte数组toHex或toBase64
Md5Hash

其主要作用是注入加密所需的元素