Package org.aoju.bus.crypto.asymmetric
Class Crypto
- All Implemented Interfaces:
Serializable,Decryptor,Encryptor
非对称加密算法
1、签名:使用私钥加密,公钥解密。 用于让所有公钥所有者验证私钥所有者的身份并且用来防止私钥所有者发布的内容被篡改,但是不用来保证内容不被他人获得。 2、加密:用公钥加密,私钥解密。 用于向公钥所有者发布信息,这个信息可能被他人篡改,但是无法被他人获得。
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CiphersCipher负责完成加密或解密工作protected int解密的块大小protected int加密的块大小Fields inherited from class org.aoju.bus.crypto.asymmetric.Asymmetric
algorithm, lock, privateKey, publicKey -
Constructor Summary
ConstructorsConstructorDescription构造,创建新的私钥公钥对构造构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密Crypto(String algorithm, PrivateKey privateKey, PublicKey publicKey) 构造Crypto(org.aoju.bus.core.lang.Algorithm algorithm) 构造,创建新的私钥公钥对Crypto(org.aoju.bus.core.lang.Algorithm algorithm, byte[] privateKey, byte[] publicKey) 构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密Crypto(org.aoju.bus.core.lang.Algorithm algorithm, PrivateKey privateKey, PublicKey publicKey) 构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密 -
Method Summary
Modifier and TypeMethodDescriptionbyte[]解密byte[]加密获取AlgorithmParameterSpec在某些算法中,需要特别的参数,例如在ECIES中,此处为IESParameterSpec获得加密或解密器int获取解密块大小int获取加密块大小init(String algorithm, PrivateKey privateKey, PublicKey publicKey) 初始化 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密(签名)或者解密(校验)protected void初始化Cipher,默认尝试加载BC库voidsetAlgorithmParameterSpec(AlgorithmParameterSpec algorithmParameterSpec) 设置AlgorithmParameterSpec在某些算法中,需要特别的参数,例如在ECIES中,此处为IESParameterSpecvoidsetDecryptBlockSize(int decryptBlockSize) 设置解密块大小voidsetEncryptBlockSize(int encryptBlockSize) 设置加密块大小setRandom(SecureRandom random) 设置随机数生成器,可自定义随机数种子Methods inherited from class org.aoju.bus.crypto.asymmetric.Asymmetric
getKeyByType, getPrivateKey, getPrivateKeyBase64, getPublicKey, getPublicKeyBase64, initKeys, setKey, setPrivateKey, setPublicKeyMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.aoju.bus.crypto.asymmetric.Decryptor
decrypt, decrypt, decryptFromBcd, decryptFromBcd, decryptStr, decryptStr, decryptStrFromBcd, decryptStrFromBcdMethods inherited from interface org.aoju.bus.crypto.asymmetric.Encryptor
encrypt, encrypt, encrypt, encrypt, encryptBase64, encryptBase64, encryptBase64, encryptBase64, encryptBcd, encryptBcd, encryptHex, encryptHex, encryptHex, encryptHex
-
Field Details
-
ciphers
Cipher负责完成加密或解密工作 -
encryptBlockSize
protected int encryptBlockSize加密的块大小 -
decryptBlockSize
protected int decryptBlockSize解密的块大小
-
-
Constructor Details
-
Crypto
public Crypto(org.aoju.bus.core.lang.Algorithm algorithm) 构造,创建新的私钥公钥对- Parameters:
algorithm-Algorithm
-
Crypto
构造,创建新的私钥公钥对- Parameters:
algorithm- 算法
-
Crypto
public Crypto(org.aoju.bus.core.lang.Algorithm algorithm, String privateKeyStr, String publicKeyStr) 构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密- Parameters:
algorithm-AlgorithmprivateKeyStr- 私钥Hex或Base64表示publicKeyStr- 公钥Hex或Base64表示
-
Crypto
public Crypto(org.aoju.bus.core.lang.Algorithm algorithm, byte[] privateKey, byte[] publicKey) 构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密- Parameters:
algorithm-AlgorithmprivateKey- 私钥publicKey- 公钥
-
Crypto
public Crypto(org.aoju.bus.core.lang.Algorithm algorithm, PrivateKey privateKey, PublicKey publicKey) 构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密- Parameters:
algorithm-AlgorithmprivateKey- 私钥publicKey- 公钥
-
Crypto
构造 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密- Parameters:
algorithm- 非对称加密算法privateKeyBase64- 私钥Base64publicKeyBase64- 公钥Base64
-
Crypto
构造私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
- Parameters:
algorithm- 算法privateKey- 私钥publicKey- 公钥
-
Crypto
构造私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
- Parameters:
algorithm- 算法privateKey- 私钥publicKey- 公钥
-
-
Method Details
-
getEncryptBlockSize
public int getEncryptBlockSize()获取加密块大小- Returns:
- 加密块大小
-
setEncryptBlockSize
public void setEncryptBlockSize(int encryptBlockSize) 设置加密块大小- Parameters:
encryptBlockSize- 加密块大小
-
getDecryptBlockSize
public int getDecryptBlockSize()获取解密块大小- Returns:
- 解密块大小
-
setDecryptBlockSize
public void setDecryptBlockSize(int decryptBlockSize) 设置解密块大小- Parameters:
decryptBlockSize- 解密块大小
-
getAlgorithmParameterSpec
获取AlgorithmParameterSpec在某些算法中,需要特别的参数,例如在ECIES中,此处为IESParameterSpec- Returns:
AlgorithmParameterSpec
-
setAlgorithmParameterSpec
设置AlgorithmParameterSpec在某些算法中,需要特别的参数,例如在ECIES中,此处为IESParameterSpec- Parameters:
algorithmParameterSpec-AlgorithmParameterSpec
-
setRandom
设置随机数生成器,可自定义随机数种子- Parameters:
random- 随机数生成器,可自定义随机数种子- Returns:
- this
-
init
Description copied from class:Asymmetric初始化 私钥和公钥同时为空时生成一对新的私钥和公钥 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密(签名)或者解密(校验)- Overrides:
initin classAsymmetric<Crypto>- Parameters:
algorithm- 算法privateKey- 私钥publicKey- 公钥- Returns:
- this
-
encrypt
Description copied from interface:Encryptor加密- Parameters:
data- 被加密的byteskeyType- 私钥或公钥KeyType- Returns:
- 加密后的bytes
-
decrypt
Description copied from interface:Decryptor解密- Parameters:
data- 被解密的byteskeyType- 私钥或公钥KeyType- Returns:
- 解密后的bytes
-
getCipher
获得加密或解密器- Returns:
- 加密或解密
-
initCipher
protected void initCipher()初始化Cipher,默认尝试加载BC库
-