Java实现ECB、CBC工作模式加密数据。
时间: 2024-05-10 11:19:41 浏览: 13
ECB和CBC是常见的对称加密工作模式,Java提供了多种加密算法和工作模式的实现,以下是ECB和CBC工作模式加密数据的示例代码:
ECB工作模式加密数据:
```java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class ECBEncryption {
public static void main(String[] args) throws Exception {
String plainText = "Hello, world!";
System.out.println("Plain text: " + plainText);
// 生成AES密钥
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
// 加密
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedText = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
// 输出加密结果
String encryptedStr = Base64.getEncoder().encodeToString(encryptedText);
System.out.println("Encrypted text: " + encryptedStr);
}
}
```
CBC工作模式加密数据:
```java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;
public class CBCEncryption {
public static void main(String[] args) throws Exception {
String plainText = "Hello, world!";
System.out.println("Plain text: " + plainText);
// 生成AES密钥
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
// 生成随机IV
byte[] iv = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
// 加密
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
byte[] encryptedText = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
// 输出加密结果
String encryptedStr = Base64.getEncoder().encodeToString(encryptedText);
System.out.println("Encrypted text: " + encryptedStr);
}
}
```
需要注意的是,在CBC工作模式下,需要生成随机IV来增加加密安全性。另外,这里使用了Base64编码来将二进制加密结果输出为字符串。在实际应用中,可以根据需求进行适当调整。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)