首頁 > Java > java教程 > 為什麼 Java AES/CBC 解密後初始位元組不正確,如何修復?

為什麼 Java AES/CBC 解密後初始位元組不正確,如何修復?

DDD
發布: 2024-11-29 08:00:17
原創
486 人瀏覽過

Why are the initial bytes incorrect after Java AES/CBC decryption, and how can I fix it?

Java AES/CBC 解密後初始位元組不正確

問題:

嘗試

問題:

嘗試

問題:

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESEncryptionExample {

  public static void encryptDecryptString() {
    try {
      String key = "mySecretKey";
      String value = "This is a test message";
      String initVector = "initializationVector"; // 16-byte (128-bit) initialization vector

      IvParameterSpec iv = new IvParameterSpec(initVector.getBytes());
      SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");

      // Create encrypt cipher
      Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
      byte[] encryptedBytes = encryptCipher.doFinal(value.getBytes());

      // Create decrypt cipher
      Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      decryptCipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
      byte[] decryptedBytes = decryptCipher.doFinal(encryptedBytes);

      System.out.println("Original: " + value);
      System.out.println("Decrypted: " + new String(decryptedBytes));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    encryptDecryptString();
  }
}
登入後複製
嘗試時在Java中解密AES/CBC加密的字串,解密結果的初始位元組是錯誤的。

Result: `£eB6O�geS��i are you? Have a nice day.
登入後複製
範例:

以下程式碼示範了這個問題:

執行此程式碼時,解密的輸出可能會出現類似的情況至:

解決方案:
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import java.util.Base64;

public class CipherAESBase64 {

  public static void encryptDecryptString() {
    try {
      String key = "mySecretKey";
      String value = "This is a test message";
      String initVector = "initializationVector"; // 16-byte (128-bit) initialization vector

      IvParameterSpec iv = new IvParameterSpec(initVector.getBytes());
      SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");

      // Create encrypt cipher
      Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
      byte[] encryptedBytes = encryptCipher.doFinal(value.getBytes());

      // Encode the encrypted bytes into a Base64 string
      String encryptedString = Base64.getEncoder().encodeToString(encryptedBytes);

      // Create decrypt cipher
      Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      decryptCipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

      // Decode the encrypted Base64 string into bytes
      byte[] decryptedBytes = Base64.getDecoder().decode(encryptedString);

      // Decrypt the decoded bytes
      byte[] decryptedBytes = decryptCipher.doFinal(decryptedBytes);

      System.out.println("Original: " + value);
      System.out.println("Decrypted: " + new String(decryptedBytes));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    encryptDecryptString();
  }
}
登入後複製
由於處理加密/解密資料時缺少Base64 編碼/解碼,導致解密字串中的初始位元組不正確。若要解決此問題,應在傳輸加密位元組之前對加密位元組進行 Base64 編碼,並在解密之前對接收到的加密位元組進行 Base64 解碼。 更新範例:

以上是為什麼 Java AES/CBC 解密後初始位元組不正確,如何修復?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板