常用的 Token
加密算法包括对称加密算法和非对称加密算法。对称加密算法使用相同的密钥来进行加密和解密,加密速度快,但是密钥传输和管理较为复杂。非对称加密算法使用一对公钥和私钥来进行加密和解密,加密速度慢,但是密钥传输和管理较为简单,且具有更好的安全性。

下面分别介绍常用的对称加密算法和非对称加密算法,并提供相应的 Java 代码示例。

<>对称加密算法

<>AES

AES(Advanced Encryption Standard)是一种高级加密标准,是一种对称加密算法。AES 可以使用不同的密钥长度,包括 128
位、192 位和 256 位。AES 加密速度较快,安全性较高,已得到广泛应用。下面是使用 AES 进行加密和解密的 Java 代码示例:
import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import
java.util.Base64; public class AESUtils { private static final String
TRANSFORMATION = "AES/ECB/PKCS5Padding"; private static final String ALGORITHM
= "AES"; public static String encrypt(String plainText, String key) throws
Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(),
ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encryptedBytes =
cipher.doFinal(plainText.getBytes()); return
Base64.getEncoder().encodeToString(encryptedBytes); } public static String
decrypt(String cipherText, String key) throws Exception { SecretKeySpec
secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); Cipher cipher =
Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE,
secretKeySpec); byte[] cipherBytes = Base64.getDecoder().decode(cipherText);
byte[] decryptedBytes = cipher.doFinal(cipherBytes); return new
String(decryptedBytes); } }
在上面的示例代码中,encrypt 方法使用 AES 算法对明文进行加密,decrypt 方法使用 AES 算法对密文进行解密。其中,
TRANSFORMATION 和 ALGORITHM 分别指定了加密算法和算法模式。在加密和解密的过程中,需要使用相同的密钥来进行加密和解密。

<>DES

DES(Data Encryption Standard)是一种经典的对称加密算法,使用 56
位密钥进行加密和解密。由于密钥长度较短,安全性较低,已逐渐被其它对称加密算法所取代。下面是使用 DES 进行加密和解密的 Java 代码示例:
import javax.crypto.Cipher; import javax.crypto.spec.DESKeySpec; import
javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec;
import java.util.Base64; public class DESUtils { private static final String
TRANSFORMATION = "DES/CBC/PKCS5Padding"; private static final String ALGORITHM
= "DES"; private static final String IV = "12345678"; public static String
encrypt(String plainText, String key) throws Exception { DESKeySpec desKeySpec
= new DESKeySpec(key.getBytes()); SecretKeySpec secretKeySpec = new
SecretKeySpec(desKeySpec.getKey(), ALGORITHM); Cipher cipher =
Cipher.getInstance(TRANSFORMATION); IvParameterSpec ivParameterSpec = new
IvParameterSpec(IV.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec,
ivParameterSpec); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes); } public static
String decrypt(String cipherText, String key) throws Exception { DESKeySpec
desKeySpec = new DESKeySpec(key.getBytes()); SecretKeySpec secretKeySpec = new
SecretKeySpec(desKeySpec.getKey(), ALGORITHM); Cipher cipher =
Cipher.getInstance(TRANSFORMATION); IvParameterSpec ivParameterSpec = new
IvParameterSpec(IV.getBytes()); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec,
ivParameterSpec); byte[] cipherBytes = Base64.getDecoder().decode(cipherText);
byte[] decryptedBytes = cipher.doFinal(cipherBytes); return new
String(decryptedBytes); } }
在上面的示例代码中,encrypt 方法使用 DES 算法对明文进行加密,decrypt 方法使用 DES 算法对密文进行解密。其中,
TRANSFORMATION 和 ALGORITHM 分别指定了加密算法和算法模式,IV
则指定了加密算法的初始向量。在加密和解密的过程中,需要使用相同的密钥来进行加密和解密。

<>非对称加密算法

<>RSA

RSA(Rivest–Shamir–Adleman)是一种非对称加密算法,使用公钥加密,私钥解密。RSA
的安全性基于质因数分解问题,即将一个大的合数分解成其质数因子的问题。RSA 需要生成一对公钥和私钥,其中公钥可以公开,私钥需要保密。下面是使用 RSA
进行加密和解密的 Java 代码示例:
import java.nio.charset.StandardCharsets; import java.security.KeyPair; import
java.security.KeyPairGenerator; import java.security.PrivateKey; import
java.security.PublicKey; import java.security.spec.PKCS8EncodedKeySpec; import
java.security.spec.X509EncodedKeySpec; import java.util.Base64; import
javax.crypto.Cipher; public class RSAUtils { private static final String
ALGORITHM = "RSA"; public static KeyPair generateKeyPair(int keySize) throws
Exception { KeyPairGenerator keyPairGenerator =
KeyPairGenerator.getInstance(ALGORITHM); keyPairGenerator.initialize(keySize);
return keyPairGenerator.generateKeyPair(); } public static String
encrypt(String plainText, PublicKey publicKey) throws Exception { Cipher cipher
= Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes =
cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); return
Base64.getEncoder().encodeToString(encryptedBytes); } public static String
decrypt(StringcipherText, PrivateKey privateKey) throws Exception { Cipher
cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE,
privateKey); byte[] cipherBytes = Base64.getDecoder().decode(cipherText);
byte[] decryptedBytes = cipher.doFinal(cipherBytes); return new
String(decryptedBytes, StandardCharsets.UTF_8); } public static PublicKey
getPublicKey(byte[] keyBytes) throws Exception { X509EncodedKeySpec keySpec =
new X509EncodedKeySpec(keyBytes); return
KeyFactory.getInstance(ALGORITHM).generatePublic(keySpec); } public static
PrivateKey getPrivateKey(byte[] keyBytes) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); return
KeyFactory.getInstance(ALGORITHM).generatePrivate(keySpec); } }
在上面的示例代码中,generateKeyPair 方法用于生成一对公钥和私钥,encrypt 方法使用公钥对明文进行加密,decrypt
方法使用私钥对密文进行解密。在加密和解密的过程中,需要使用相同的公钥和私钥。在获取公钥和私钥的过程中,需要指定相应的密钥格式。

<>ECC

ECC(Elliptic Curve Cryptography)是一种基于椭圆曲线数学问题的非对称加密算法。ECC 相对于 RSA
等传统的非对称加密算法来说,具有更高的加密强度和更短的密钥长度,因此在一些资源受限或对安全性要求较高的场景中得到了广泛应用。下面是使用 ECC
进行加密和解密的 Java 代码示例:
import java.security.KeyPair; import java.security.KeyPairGenerator; import
java.security.PrivateKey; import java.security.PublicKey; import
java.security.Signature; import java.util.Base64; public class ECCUtils {
private static final String ALGORITHM = "EC"; private static final String
SIGNATURE_ALGORITHM = "SHA256withECDSA"; public static KeyPair
generateKeyPair() throws Exception { KeyPairGenerator keyPairGenerator =
KeyPairGenerator.getInstance(ALGORITHM); return
keyPairGenerator.generateKeyPair(); } public static String sign(String
plainText, PrivateKey privateKey) throws Exception { Signature signature =
Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(privateKey);
signature.update(plainText.getBytes()); byte[] signedBytes = signature.sign();
return Base64.getEncoder().encodeToString(signedBytes); } public static boolean
verify(String plainText, String signatureText, PublicKey publicKey) throws
Exception { Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(publicKey); signature.update(plainText.getBytes()); byte[]
signatureBytes = Base64.getDecoder().decode(signatureText); return
signature.verify(signatureBytes); } }
在上面的示例代码中,generateKeyPair 方法用于生成一对公钥和私钥,sign 方法使用私钥对明文进行签名,verify
方法使用公钥对签名进行验证。在签名和验证的过程中,需要使用相同的公钥和私钥。

<>小结

以上是常用的 Token 加密算法的 Java 代码示例,包括对称加密算法 AES 和 DES,以及非对称加密算法 RSA 和
ECC。在实际使用中,需要根据具体的场景和需求选择合适的加密算法,并进行适当的参数配置和密钥管理,以保障数据的安全性和可靠性。

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信