在Java中,与加密相关的函数和类主要集中在javax.crypto
和java.security
两个包中,涵盖对称加密、非对称加密、消息摘要、密钥生成等方面。下面列出一些常用的类和函数:
1. 对称加密(Symmetric Encryption)
对称加密使用单一密钥进行加密和解密。
-
Cipher类(
javax.crypto.Cipher
)Cipher.getInstance(String transformation)
:获取加密/解密算法的实例。例如:Cipher.getInstance("AES/CBC/PKCS5Padding")
。Cipher.init(int mode, Key key)
:初始化加密或解密模式,mode
为Cipher.ENCRYPT_MODE
(加密)或Cipher.DECRYPT_MODE
(解密),需要提供密钥。Cipher.update(byte[] input)
:更新数据块,执行加密/解密。Cipher.doFinal(byte[] input)
:完成数据加密或解密。Cipher.wrap(Key key)
:将密钥封装为加密形式。Cipher.unwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType)
:解封密钥。
-
SecretKey类(
javax.crypto.SecretKey
)- 用于表示对称密钥。
-
KeyGenerator类(
javax.crypto.KeyGenerator
)KeyGenerator.getInstance(String algorithm)
:获取指定加密算法的密钥生成器实例,例如KeyGenerator.getInstance("AES")
。KeyGenerator.init(int keysize)
:初始化密钥生成器,指定密钥大小。KeyGenerator.generateKey()
:生成对称密钥。
2. 非对称加密(Asymmetric Encryption)
非对称加密使用公钥加密、私钥解密。
-
KeyPairGenerator类(
java.security.KeyPairGenerator
)KeyPairGenerator.getInstance(String algorithm)
:获取指定算法的公钥/私钥对生成器,例如:KeyPairGenerator.getInstance("RSA")
。KeyPairGenerator.initialize(int keysize)
:初始化生成器,指定密钥大小。KeyPairGenerator.generateKeyPair()
:生成公钥/私钥对。
-
KeyPair类(
java.security.KeyPair
)- 用于保存生成的公钥和私钥。
-
PublicKey/PrivateKey类(
java.security.PublicKey
和java.security.PrivateKey
)- 分别表示公钥和私钥。
-
Cipher类(同样用于非对称加密)
Cipher.init(int mode, Key key)
:用公钥或私钥进行加密/解密初始化。Cipher.doFinal(byte[] input)
:完成加密/解密。
3. 消息摘要(Message Digest)
消息摘要算法用于生成固定长度的哈希值,常用于数据完整性校验。
- MessageDigest类(
java.security.MessageDigest
)MessageDigest.getInstance(String algorithm)
:获取指定摘要算法的实例,例如:MessageDigest.getInstance("SHA-256")
。MessageDigest.update(byte[] input)
:更新摘要值。MessageDigest.digest()
:计算并返回最终的摘要。
4. 数字签名(Digital Signatures)
用于数据的签名和验证。
- Signature类(
java.security.Signature
)Signature.getInstance(String algorithm)
:获取指定签名算法的实例,例如:Signature.getInstance("SHA256withRSA")
。Signature.initSign(PrivateKey privateKey)
:初始化签名,使用私钥。Signature.initVerify(PublicKey publicKey)
:初始化签名验证,使用公钥。Signature.update(byte[] data)
:更新要签名或验证的数据。Signature.sign()
:生成数字签名。Signature.verify(byte[] signature)
:验证数字签名。
5. 密钥生成与管理
-
KeyFactory类(
java.security.KeyFactory
)KeyFactory.getInstance(String algorithm)
:获取指定算法的密钥工厂,用于将密钥从一种表示转换为另一种表示,例如:从字节数组到密钥对象。
-
SecretKeyFactory类(
javax.crypto.SecretKeyFactory
)SecretKeyFactory.getInstance(String algorithm)
:用于将KeySpec转换为SecretKey。SecretKeyFactory.generateSecret(KeySpec keySpec)
:从密钥规范生成密钥。
-
KeyAgreement类(
javax.crypto.KeyAgreement
)KeyAgreement.getInstance(String algorithm)
:获取密钥协商算法实例(例如DH
用于Diffie-Hellman算法)。KeyAgreement.init(Key key)
:初始化密钥协商操作。KeyAgreement.doPhase(Key key, boolean lastPhase)
:执行密钥协商的阶段。KeyAgreement.generateSecret()
:生成共享密钥。
6. 随机数生成
- SecureRandom类(
java.security.SecureRandom
)SecureRandom.getInstance(String algorithm)
:获取加密级别的随机数生成器实例。SecureRandom.nextBytes(byte[] bytes)
:生成随机字节数组。SecureRandom.setSeed(byte[] seed)
:设置随机种子。
7. 加密文件或数据流
- CipherInputStream类(
javax.crypto.CipherInputStream
)- 用于将输入流加密。
- CipherOutputStream类(
javax.crypto.CipherOutputStream
)- 用于将输出流加密。
8. 编码与解码
- Base64类(
java.util.Base64
)Base64.getEncoder().encodeToString(byte[] bytes)
:将字节数组编码为Base64字符串。Base64.getDecoder().decode(String base64String)
:将Base64字符串解码为字节数组。
这些是Java中与加密和解密相关的常用类和方法,涵盖了对称加密、非对称加密、消息摘要、数字签名、密钥管理等。