1、AES-128加密算法:
package com.arithmetic.encryption;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
//使用Java的javax.crypto包提供的AES算法实现加密和解密功能。
//AES_ALGORITHM表示使用CBC模式和PKCS5Padding填充,AES_KEY和AES_IV分别为密钥和初始向量,需要使用16字节长度的byte数组。
//在encrypt方法中,首先使用Cipher.getInstance方法获取实现指定算法的Cipher对象,并使用密钥和初始向量进行初始化。
//调用cipher.doFinal方法对明文进行加密,并使用Base64进行编码,返回加密后的密文。
//在decrypt方法中,同样使用Cipher.getInstance方法获取Cipher对象,并使用相同的密钥和初始向量初始化。
//调用cipher.doFinal方法对Base64解码后的密文进行解密,并将解密后的字节数组转换为字符串返回。
//在main方法中,测试加密和解密的功能
//定义一个明文字符串,然后调用encrypt方法对明文进行加密,将得到的密文打印输出。
//调用decrypt方法对密文进行解密,并将解密后的明文打印输出。
//示例代码中的密钥和初始向量是硬编码的,实际应用中,应该根据安全要求生成随机的密钥和初始向量,并妥善保管。
public class AES128EncryptionDemo {
private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String AES_KEY = "0123456789abcdef"; // 16字节的密钥
private static final String AES_IV = "0123456789abcdef"; // 16字节的初始向量
public static String encrypt(String plaintext) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
SecretKeySpec keySpec = new SecretKeySpec(AES_KEY.getBytes(StandardCharsets.UTF_8), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(AES_IV.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String ciphertext) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
SecretKeySpec keySpec = new SecretKeySpec(AES_KEY.getBytes(StandardCharsets.UTF_8), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(AES_IV.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
public static void main(String[] args) {
try {
String plaintext = "Hello, world!";
String ciphertext = encrypt(plaintext);
System.out.println("Ciphertext: " + ciphertext);
String decryptedText = decrypt(ciphertext);
System.out.println("Decrypted text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、AES-256加密算法:
package com.arithmetic.encryption;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
//AES-256算法要求JCE(Java Cryptography Extension)无限制权限策略文件,否则会报错。
//如果使用Java的默认加密库,需要下载适用于特定JDK版本的JCE无限制权限策略文件,并将其覆盖到<JDK_HOME>/jre/lib/security/目录下。
//使用Java的javax.crypto包提供的AES算法实现加密和解密功能。
//AES_ALGORITHM表示使用CBC模式和PKCS5Padding填充,AES_KEY和AES_IV分别为密钥和初始向量,需要使用32字节长度的byte数组。
//在encrypt方法中,首先使用Cipher.getInstance方法获取实现指定算法的Cipher对象,并使用密钥和初始向量进行初始化。
//调用cipher.doFinal方法对明文进行加密,并使用Base64进行编码,返回加密后的密文。
//在decrypt方法中,同样使用Cipher.getInstance方法获取Cipher对象,并使用相同的密钥和初始向量初始化。
//调用cipher.doFinal方法对Base64解码后的密文进行解密,并将解密后的字节数组转换为字符串返回。
//在main方法中,测试加密和解密的功能
//首先,定义一个明文字符串,然后调用encrypt方法对明文进行加密,将得到的密文打印输出。
//接着,调用decrypt方法对密文进行解密,并将解密后的明文打印输出。
//请注意,示例代码中的密钥和初始向量是硬编码的,实际应用中,应该根据安全要求生成随机的密钥和初始向量,并妥善保管。
public class AES256EncryptionDemo {
private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String AES_KEY = "0123456789abcdef0123456789abcdef"; // 32字节的密钥
private static final String AES_IV = "0123456789abcdef"; // 16字节的初始向量
public static String encrypt(String plaintext) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
SecretKeySpec keySpec = new SecretKeySpec(AES_KEY.getBytes(StandardCharsets.UTF_8), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(AES_IV.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String ciphertext) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
SecretKeySpec keySpec = new SecretKeySpec(AES_KEY.getBytes(StandardCharsets.UTF_8), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(AES_IV.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
public static void main(String[] args) {
try {
String plaintext = "Hello, world!";
String ciphertext = encrypt(plaintext);
System.out.println("Ciphertext: " + ciphertext);
String decryptedText = decrypt(ciphertext);
System.out.println("Decrypted text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3、Blowfish加密算法:
package com.arithmetic.encryption;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
//使用Java的加密库来实现Blowfish加密算法。
//在BlowfishEncryption类中,BLOWFISH_KEY是一个16字节的密钥,用于创建SecretKeySpec对象。
//使用Cipher类创建一个Blowfish加密/解密对象,并使用密钥初始化它。
//encrypt方法将明文转换为字节数组,通过调用doFinal方法进行加密,并使用Base64编码将结果转换为字符串。
//decrypt方法将密文解码为字节数组,通过调用doFinal解密,并将结果转换为字符串。
//Blowfish是一种对称加密算法,密钥长度可变,但建议密钥为8字节的倍数,最长可达到56字节,此处使用了16字节的密钥。
public class BlowfishEncryptionDemo {
private static final String BLOWFISH_ALGORITHM = "Blowfish";
private static final String BLOWFISH_KEY = "0123456789abcdef"; // 16字节的密钥
public static String encrypt(String plaintext) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(BLOWFISH_KEY.getBytes(StandardCharsets.UTF_8), BLOWFISH_ALGORITHM);
Cipher cipher = Cipher.getInstance(BLOWFISH_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String ciphertext) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(BLOWFISH_KEY.getBytes(StandardCharsets.UTF_8), BLOWFISH_ALGORITHM);
Cipher cipher = Cipher.getInstance(BLOWFISH_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
public static void main(String[] args) {
try {
String plaintext = "Hello, world!";
String ciphertext = encrypt(plaintext);
System.out.println("Ciphertext: " + ciphertext);
String decryptedText = decrypt(ciphertext);
System.out.println("Decrypted text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、DSA加密算法:
package com.arithmetic.encryption;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.util.Base64;
//使用Java的加密库实现DSA加密算法。
//在DSAEncryption类中,generateKeyPair方法用于生成DSA公钥和私钥对。
//sign方法使用DSA算法对数据进行签名,并使用私钥初始化签名对象。
//verify方法验证签名是否有效,并使用公钥初始化验签对象。
//在main方法中,生成密钥对,然后使用私钥对明文进行签名,最后使用公钥对签名进行验证。
//生成的密钥对通常需要进行安全存储。
public class DSAEncryptionDemo {
private static final String DSA_ALGORITHM = "DSA";
//使用SHA256withDSA作为DSA算法的摘要算法。
public static String sign(String data, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance("SHA256withDSA");
signature.initSign(privateKey);
signature.update(data.getBytes(StandardCharsets.UTF_8));
byte[] signBytes = signature.sign();
return Base64.getEncoder().encodeToString(signBytes);
}
public static boolean verify(String data, String signature, PublicKey publicKey) throws Exception {
Signature verifier = Signature.getInstance(DSA_ALGORITHM);
verifier.initVerify(publicKey);
verifier.update(data.getBytes(StandardCharsets.UTF_8));
byte[] signatureBytes = Base64.getDecoder().decode(signature);
return verifier.verify(signatureBytes);
}
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(DSA_ALGORITHM);
keyPairGen.initialize(2048);
return keyPairGen.generateKeyPair();
}
public static void main(String[] args) {
try {
String plaintext = "Hello, world!";
KeyPair keyPair = generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String signature = sign(plaintext, privateKey);
System.out.println("Signature: " + signature);
boolean verified = verify(plaintext, signature, publicKey);
System.out.println("Verified: " + verified);
} catch (Exception e) {
e.printStackTrace();
}
}
}
5、RSA加密算法:
package com.arithmetic.encryption;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.util.Base64;
import javax.crypto.Cipher;
//使用Java的加密库实现RSA加密算法。
//在RSAEncryption类中,generateKeyPair方法用于生成RSA公钥和私钥对。
//encrypt方法将明文转换为字节数组,通过调用doFinal方法进行加密,并使用Base64编码将结果转换为字符串。
//decrypt方法将密文解码为字节数组,通过调用doFinal解密,并将结果转换为字符串。
//示例代码还实现了数字签名部分。
//sign方法使用SHA1withRSA算法对数据进行签名,并使用私钥初始化签名对象。
//verify方法使用SHA1withRSA算法验证签名是否有效,并使用公钥初始化验签对象。
//示例代码中,直接使用生成的密钥对进行加密、解密和签名验证操作。
public class RSAEncryptionDemo {
private static final String RSA_ALGORITHM = "RSA";
public static String encrypt(String plaintext, PublicKey publicKey) throws Exception {
byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8);
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(plaintextBytes);
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String ciphertext, PrivateKey privateKey) throws Exception {
byte[] ciphertextBytes = Base64.getDecoder().decode(ciphertext);
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(ciphertextBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(RSA_ALGORITHM);
keyPairGen.initialize(2048);
return keyPairGen.generateKeyPair();
}
public static String sign(String data, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initSign(privateKey);
signature.update(data.getBytes(StandardCharsets.UTF_8));
byte[] signBytes = signature.sign();
return Base64.getEncoder().encodeToString(signBytes);
}
public static boolean verify(String data, String signature, PublicKey publicKey) throws Exception {
Signature verifier = Signature.getInstance("SHA1withRSA");
verifier.initVerify(publicKey);
verifier.update(data.getBytes(StandardCharsets.UTF_8));
byte[] signatureBytes = Base64.getDecoder().decode(signature);
return verifier.verify(signatureBytes);
}
public static void main(String[] args) {
try {
String plaintext = "Hello, world!";
KeyPair keyPair = generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String ciphertext = encrypt(plaintext, publicKey);
System.out.println("Ciphertext: " + ciphertext);
String decryptedText = decrypt(ciphertext, privateKey);
System.out.println("Decrypted text: " + decryptedText);
String signature = sign(plaintext, privateKey);
System.out.println("Signature: " + signature);
boolean verified = verify(plaintext, signature, publicKey);
System.out.println("Verified: " + verified);
} catch (Exception e) {
e.printStackTrace();
}
}
}