对称加密、消息摘要的JDK实现
发布人:shili8
发布时间:2025-01-04 17:10
阅读次数:0
**Java 中对称加密和消息摘要的实现**
在 Java 中,提供了多种方式来实现对称加密和消息摘要。下面我们将使用 JDK 提供的 API 来实现这些功能。
### 对称加密对称加密是一种常见的加密算法,它使用相同的密钥进行加密和解密。在 Java 中,我们可以使用 `javax.crypto` 包来实现对称加密。
####1. AES 加密AES(Advanced Encryption Standard)是目前最流行的对称加密算法之一。它支持多种块大小,包括128、192 和256 位。
javaimport javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Base64;
public class AESExample {
public static void main(String[] args) throws Exception {
// 密钥(16位)
String key = "1234567890abcdef";
// 需要加密的数据 String data = "Hello, World!";
// 加密 Cipher cipher = Cipher.getInstance("AES");
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
System.out.println("加密后数据:" + Base64.getEncoder().encodeToString(encryptedData));
// 解密 cipher.init(Cipher.DECRYPT_MODE, aesKey);
byte[] decryptedData = cipher.doFinal(encryptedData);
System.out.println("解密后数据:" + new String(decryptedData));
}
}
####2. DES 加密DES(Data Encryption Standard)是早期的一种对称加密算法。它使用56 位密钥进行加密和解密。
javaimport javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Base64;
public class DESExample {
public static void main(String[] args) throws Exception {
// 密钥(8位)
String key = "12345678";
// 需要加密的数据 String data = "Hello, World!";
// 加密 Cipher cipher = Cipher.getInstance("DES");
Key desKey = new SecretKeySpec(key.getBytes(), "DES");
cipher.init(Cipher.ENCRYPT_MODE, desKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
System.out.println("加密后数据:" + Base64.getEncoder().encodeToString(encryptedData));
// 解密 cipher.init(Cipher.DECRYPT_MODE, desKey);
byte[] decryptedData = cipher.doFinal(encryptedData);
System.out.println("解密后数据:" + new String(decryptedData));
}
}
### 消息摘要消息摘要是一种将原始数据转换为固定长度的哈希值的算法。它常用于验证数据的完整性和真实性。在 Java 中,我们可以使用 `javax.crypto` 包来实现消息摘要。
####1. MD5 摘要MD5(Message-Digest Algorithm5)是一种常见的消息摘要算法。它产生一个128 位的哈希值。
javaimport javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.nio.charset.StandardCharsets;
public class MD5Example {
public static void main(String[] args) throws Exception {
// 需要摘要的数据 String data = "Hello, World!";
// 摘要 Mac mac = Mac.getInstance("MD5");
Key md5Key = new SecretKeySpec(data.getBytes(StandardCharsets.UTF_8), "HmacMD5");
byte[] digest = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
System.out.println("摘要:" + bytesToHex(digest));
}
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}
####2. SHA-256 摘要SHA-256(Secure Hash Algorithm256)是一种安全的消息摘要算法。它产生一个256 位的哈希值。
javaimport javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.nio.charset.StandardCharsets;
public class SHA256Example {
public static void main(String[] args) throws Exception {
// 需要摘要的数据 String data = "Hello, World!";
// 摘要 Mac mac = Mac.getInstance("SHA-256");
Key sha256Key = new SecretKeySpec(data.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
byte[] digest = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
System.out.println("摘要:" + bytesToHex(digest));
}
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}
以上是 Java 中对称加密和消息摘要的实现示例。这些代码可以帮助您理解如何使用 JDK 提供的 API 来进行加密和摘要操作。

