117.info
人生若只如初见

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

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

  1. 电子密码本模式 (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);
    }
}
  1. 密码分组链接模式 (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);
    }
}
  1. 密文反馈模式 (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);
    }
}
  1. 输出反馈模式 (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);
    }
}
  1. 计数器模式 (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(), "

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe69aAzsLAA5RDF0.html

推荐文章

  • java中异常类会影响性能吗

    在Java中,异常类本身不会对性能产生显著影响。异常处理机制是为了在程序运行时处理错误或异常情况,而不是为了优化性能。然而,在使用异常时,需要注意以下几点...

  • java中异常类如何避免

    在Java中,避免异常的最好方法是编写健壮的代码并遵循一些最佳实践。以下是一些建议: 预期和处理异常:使用try-catch语句来捕获和处理可能发生的异常。确保处理...

  • java中异常类有哪些常见类型

    Java中的异常类主要分为两大类:受检异常(Checked Exceptions)和非受检异常(Unchecked Exceptions)。以下是具体的异常类型:
    受检异常(Checked Except...

  • java中异常类怎么捕获

    在Java中,我们使用try-catch语句来捕获异常。当程序执行过程中遇到异常时,Java运行时系统会抛出一个异常对象。你可以使用try块来包含可能引发异常的代码,然后...

  • python字符串去重及排序的方法是什么

    Python中字符串去重及排序的方法有以下几种: 使用set()函数进行去重:将字符串转换为集合,集合具有去重的特性。然后再将集合转换为字符串。示例代码如下: s =...

  • Android中bindservice的三个参数怎么用

    在Android中使用bindService()方法绑定服务时,有三个参数需要传递: Intent对象:用于指定要绑定的服务。可以通过Intent构造函数或者Intent.createIntent()方法...

  • hibernate框架的特点有哪些

    Hibernate框架的特点包括: 对数据库的操作进行了封装,使得开发人员可以通过面向对象的方式进行数据库操作,而不需要直接编写SQL语句。
    提供了事务管理机制...

  • mysql怎么授权数据库给用户

    要授权数据库给用户,可以使用MySQL的GRANT语句。
    以下是授权数据库给用户的一般语法:
    GRANT 权限 ON 数据库名.表名 TO ‘用户名’@‘主机’;
    其...