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中文網其他相關文章!

類加載器通過統一的類文件格式、動態加載、雙親委派模型和平台無關的字節碼,確保Java程序在不同平台上的一致性和兼容性,實現平台獨立性。

Java編譯器生成的代碼是平台無關的,但最終執行的代碼是平台特定的。 1.Java源代碼編譯成平台無關的字節碼。 2.JVM將字節碼轉換為特定平台的機器碼,確保跨平台運行但性能可能不同。

多線程在現代編程中重要,因為它能提高程序的響應性和資源利用率,並處理複雜的並發任務。 JVM通過線程映射、調度機制和同步鎖機制,在不同操作系統上確保多線程的一致性和高效性。

Java的平台獨立性是指編寫的代碼可以在任何安裝了JVM的平台上運行,無需修改。 1)Java源代碼編譯成字節碼,2)字節碼由JVM解釋執行,3)JVM提供內存管理和垃圾回收功能,確保程序在不同操作系統上運行。

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

云计算显著提升了Java的平台独立性。1)Java代码编译为字节码,由JVM在不同操作系统上执行,确保跨平台运行。2)使用Docker和Kubernetes部署Java应用,提高可移植性和可扩展性。

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

容器化技術如Docker增強而非替代Java的平台獨立性。 1)確保跨環境的一致性,2)管理依賴性,包括特定JVM版本,3)簡化部署過程,使Java應用更具適應性和易管理性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能