在Java函数中如何进行加密和解密操作?
发布时间:2023-07-03 09:33:24
在Java函数中进行加密和解密操作可以使用Java加密扩展(Java Cryptography Extension,JCE)提供的加密库。JCE是Java平台的标准扩展,为开发者提供了各种加密和解密算法,包括对称加密、非对称加密和消息摘要等。下面将介绍一些常用的加密和解密操作。
1. 对称加密和解密操作:
在对称加密中,使用相同的密钥进行加密和解密。常见的对称加密算法有AES、DES和Triple DES等。
加密:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
public class SymmetricEncryption {
public static byte[] encrypt(String plaintext, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plaintext.getBytes());
}
public static String decrypt(byte[] ciphertext, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] plaintextBytes = cipher.doFinal(ciphertext);
return new String(plaintextBytes);
}
public static void main(String[] args) throws Exception {
String plaintext = "Hello, World!";
SecureRandom random = new SecureRandom();
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128, random);
SecretKey key = keyGen.generateKey();
byte[] ciphertext = encrypt(plaintext, key);
System.out.println("Ciphertext: " + new String(ciphertext));
String decryptedText = decrypt(ciphertext, key);
System.out.println("Plaintext: " + decryptedText);
}
}
2. 非对称加密和解密操作:
在非对称加密中,使用一对相关的密钥,即公钥和私钥,公钥用于加密,私钥用于解密。常见的非对称加密算法有RSA和DSA等。
加密:
import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
public class AsymmetricEncryption {
public static byte[] encrypt(String plaintext, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(plaintext.getBytes());
}
public static String decrypt(byte[] ciphertext, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] plaintextBytes = cipher.doFinal(ciphertext);
return new String(plaintextBytes);
}
public static void main(String[] args) throws Exception {
String plaintext = "Hello, World!";
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keyGen.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
byte[] ciphertext = encrypt(plaintext, publicKey);
System.out.println("Ciphertext: " + new String(ciphertext));
String decryptedText = decrypt(ciphertext, privateKey);
System.out.println("Plaintext: " + decryptedText);
}
}
3. 消息摘要操作:
消息摘要是将任意长度的数据经过一系列处理后生成固定长度的摘要,通常用于验证消息的完整性。常见的消息摘要算法有MD5和SHA等。
import java.security.MessageDigest;
public class MessageDigestExample {
public static String digest(String plaintext, String algorithm) throws Exception {
MessageDigest digest = MessageDigest.getInstance(algorithm);
byte[] plaintextBytes = plaintext.getBytes();
byte[] digestBytes = digest.digest(plaintextBytes);
StringBuilder hexString = new StringBuilder();
for (byte digestByte : digestBytes) {
hexString.append(Integer.toHexString(0xFF & digestByte));
}
return hexString.toString();
}
public static void main(String[] args) throws Exception {
String plaintext = "Hello, World!";
String algorithm = "MD5";
String digest = digest(plaintext, algorithm);
System.out.println("Digest: " + digest);
}
}
以上是在Java函数中进行加密和解密操作的示例,使用JCE提供的加密库可以实现常用的加密算法。开发者可以根据需求选择适合的加密和解密算法,并根据具体应用场景选择合适的密钥长度和算法模式。
