亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

java怎么實現AES的五種加密模式

小億
91
2023-12-29 16:21:56
欄目: 編程語言

在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(), "

0
巨野县| 临湘市| 吉木萨尔县| 库伦旗| 阿拉善右旗| 闽侯县| 莱西市| 越西县| 滨州市| 白朗县| 汪清县| 万宁市| 乐都县| 宜兰县| 五常市| 安福县| 资中县| 南皮县| 南安市| 大宁县| 兴和县| 将乐县| 平顶山市| 仁布县| 浪卡子县| 富宁县| 保康县| 临沭县| 西乌珠穆沁旗| 平和县| 阿鲁科尔沁旗| 罗源县| 大港区| 兴仁县| 大埔县| 鄂伦春自治旗| 乌审旗| 马龙县| 林西县| 江孜县| 松桃|