public class SymmetricCrypto extends Object implements SymmetricEncryptor, SymmetricDecryptor, Serializable
Constructor and Description |
---|
SymmetricCrypto(String algorithm)
构造,使用随机密钥
|
SymmetricCrypto(String algorithm,
byte[] key)
构造
|
SymmetricCrypto(String algorithm,
SecretKey key)
构造
|
SymmetricCrypto(String algorithm,
SecretKey key,
AlgorithmParameterSpec paramsSpec)
构造
|
SymmetricCrypto(SymmetricAlgorithm algorithm)
构造,使用随机密钥
|
SymmetricCrypto(SymmetricAlgorithm algorithm,
byte[] key)
构造
|
SymmetricCrypto(SymmetricAlgorithm algorithm,
SecretKey key)
构造
|
Modifier and Type | Method and Description |
---|---|
byte[] |
decrypt(byte[] bytes)
解密
|
void |
decrypt(InputStream data,
OutputStream out,
boolean isClose)
解密,针对大数据量,结束后不关闭流
|
byte[] |
encrypt(byte[] data)
加密
|
void |
encrypt(InputStream data,
OutputStream out,
boolean isClose)
加密,针对大数据量,可选结束后是否关闭流
|
Cipher |
getCipher()
获得加密或解密器
|
SecretKey |
getSecretKey()
获得对称密钥
|
SymmetricCrypto |
init(String algorithm,
SecretKey key)
初始化
|
SymmetricCrypto |
setIv(byte[] iv)
设置偏移向量
|
SymmetricCrypto |
setIv(IvParameterSpec iv)
设置偏移向量
|
SymmetricCrypto |
setMode(CipherMode mode)
初始化模式并清空数据
|
SymmetricCrypto |
setParams(AlgorithmParameterSpec params)
设置
AlgorithmParameterSpec ,通常用于加盐或偏移向量 |
SymmetricCrypto |
setRandom(SecureRandom random)
设置随机数生成器,可自定义随机数种子
|
byte[] |
update(byte[] data)
更新数据,分组加密中间结果可以当作随机数
第一次更新数据前需要调用 setMode(CipherMode) 初始化加密或解密模式,然后每次更新数据都是累加模式 |
String |
updateHex(byte[] data)
更新数据,分组加密中间结果可以当作随机数
第一次更新数据前需要调用 setMode(CipherMode) 初始化加密或解密模式,然后每次更新数据都是累加模式 |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
encrypt, encrypt, encrypt, encrypt, encryptBase64, encryptBase64, encryptBase64, encryptBase64, encryptBase64, encryptHex, encryptHex, encryptHex, encryptHex, encryptHex
decrypt, decrypt, decryptStr, decryptStr, decryptStr, decryptStr, decryptStr, decryptStr
public SymmetricCrypto(SymmetricAlgorithm algorithm)
algorithm
- SymmetricAlgorithm
public SymmetricCrypto(String algorithm)
algorithm
- 算法,可以是"algorithm/mode/padding"或者"algorithm"public SymmetricCrypto(SymmetricAlgorithm algorithm, byte[] key)
algorithm
- 算法 SymmetricAlgorithm
key
- 自定义KEYpublic SymmetricCrypto(SymmetricAlgorithm algorithm, SecretKey key)
algorithm
- 算法 SymmetricAlgorithm
key
- 自定义KEYpublic SymmetricCrypto(String algorithm, byte[] key)
algorithm
- 算法key
- 密钥public SymmetricCrypto(String algorithm, SecretKey key)
algorithm
- 算法key
- 密钥public SymmetricCrypto(String algorithm, SecretKey key, AlgorithmParameterSpec paramsSpec)
algorithm
- 算法key
- 密钥paramsSpec
- 算法参数,例如加盐等public SymmetricCrypto init(String algorithm, SecretKey key)
algorithm
- 算法key
- 密钥,如果为null
自动生成一个keypublic SecretKey getSecretKey()
public Cipher getCipher()
public SymmetricCrypto setIv(byte[] iv)
iv
- 偏移向量,加盐public SymmetricCrypto setIv(IvParameterSpec iv)
iv
- IvParameterSpec
偏移向量public SymmetricCrypto setParams(AlgorithmParameterSpec params)
AlgorithmParameterSpec
,通常用于加盐或偏移向量params
- AlgorithmParameterSpec
public SymmetricCrypto setRandom(SecureRandom random)
random
- 随机数生成器,可自定义随机数种子public SymmetricCrypto setMode(CipherMode mode)
mode
- 模式枚举public byte[] update(byte[] data)
setMode(CipherMode)
初始化加密或解密模式,然后每次更新数据都是累加模式data
- 被加密的bytespublic String updateHex(byte[] data)
setMode(CipherMode)
初始化加密或解密模式,然后每次更新数据都是累加模式data
- 被加密的bytespublic byte[] encrypt(byte[] data)
SymmetricEncryptor
encrypt
in interface SymmetricEncryptor
data
- 被加密的bytespublic void encrypt(InputStream data, OutputStream out, boolean isClose) throws IORuntimeException
SymmetricEncryptor
encrypt
in interface SymmetricEncryptor
data
- 被加密的字符串out
- 输出流,可以是文件或网络位置isClose
- 是否关闭流IORuntimeException
- IO异常public byte[] decrypt(byte[] bytes)
SymmetricDecryptor
decrypt
in interface SymmetricDecryptor
bytes
- 被解密的bytespublic void decrypt(InputStream data, OutputStream out, boolean isClose) throws IORuntimeException
SymmetricDecryptor
decrypt
in interface SymmetricDecryptor
data
- 加密的字符串out
- 输出流,可以是文件或网络位置isClose
- 是否关闭流,包括输入和输出流IORuntimeException
- IO异常Copyright © 2024. All rights reserved.