java怎么实现AES的五种加密模式

   2025-02-13 2230
核心提示:在Java中,可以使用Java Cryptography Architecture (JCA)来实现AES的五种加密模式。以下是一些示例代码:电子密码本模式 (Elect

在Java中,可以使用Java Cryptography Architecture (JCA)来实现AES的五种加密模式。以下是一些示例代码:

电子密码本模式 (Electronic Codebook Mode, ECB)
import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import java.util.Base64;public class AESExample {    public static void main(String[] args) throws Exception {        String plaintext = "Hello, World!";        String key = "0123456789abcdef"; // 16字节的AES密钥        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");        cipher.init(Cipher.ENCRYPT_MODE, secretKey);        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);        System.out.println("Encrypted Text: " + encryptedText);    }}
密码分组链接模式 (Cipher Block Chaining Mode, CBC)
import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import java.util.Base64;public class AESExample {    public static void main(String[] args) throws Exception {        String plaintext = "Hello, World!";        String key = "0123456789abcdef"; // 16字节的AES密钥        String iv = "fedcba9876543210"; // 16字节的初始化向量        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");        IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);        System.out.println("Encrypted Text: " + encryptedText);    }}
密文反馈模式 (Cipher Feedback Mode, CFB)
import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import java.util.Base64;public class AESExample {    public static void main(String[] args) throws Exception {        String plaintext = "Hello, World!";        String key = "0123456789abcdef"; // 16字节的AES密钥        String iv = "fedcba9876543210"; // 16字节的初始化向量        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");        IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());        Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);        System.out.println("Encrypted Text: " + encryptedText);    }}
输出反馈模式 (Output Feedback Mode, OFB)
import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import java.util.Base64;public class AESExample {    public static void main(String[] args) throws Exception {        String plaintext = "Hello, World!";        String key = "0123456789abcdef"; // 16字节的AES密钥        String iv = "fedcba9876543210"; // 16字节的初始化向量        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");        IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());        Cipher cipher = Cipher.getInstance("AES/OFB/PKCS5Padding");        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);        System.out.println("Encrypted Text: " + encryptedText);    }}
计数器模式 (Counter Mode, CTR)
import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import java.util.Base64;public class AESExample {    public static void main(String[] args) throws Exception {        String plaintext = "Hello, World!";        String key = "0123456789abcdef"; // 16字节的AES密钥        String iv = "fedcba9876543210"; // 16字节的初始化向量        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言