本文實例講述了java使用Hex編碼解碼實現Aes加密解密功能。分享給大家供大家參考,具體如下:
這裡的Aes加密解密方法使用Hex進行了編碼解碼
package com.baidu.wallet.bdwallet.utils; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; public class Test { private static final String AES="AES"; private static final String UTF8="UTF-8"; /** * AES加密 * @param content * @param pkey * @return * @throws DecoderException */ private static byte[] encrypt(String content, String pkey) throws DecoderException { try { String private_key=pkey; byte[] encodeFormat=null; try { //秘钥 Hex解码为什么秘钥要进行解码,因为秘钥是某个秘钥明文进行了Hex编码后的值,所以在使用的时候要进行解码 encodeFormat = Hex.decodeHex(private_key.toCharArray()); } catch (DecoderException e) { e.printStackTrace(); } SecretKeySpec key = new SecretKeySpec(encodeFormat, AES); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // 加密内容进行编码 byte[] byteContent = content.getBytes(UTF8); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, key); // 正式执行加密操作 byte[] result = cipher.doFinal(byteContent); return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; } /** * AES解密 * @param contents * @param password * @return * @throws DecoderException */ private static byte[] decrypt(String contents, String password) throws DecoderException { try { //密文使用Hex解码 byte[]content = Hex.decodeHex(contents.toCharArray()); //秘钥 Hex解码为什么秘钥要进行解码,因为秘钥是某个秘钥明文进行了Hex编码后的值,所以在使用的时候要进行解码 byte[] encodeFormat = Hex.decodeHex(password.toCharArray()); SecretKeySpec key = new SecretKeySpec(encodeFormat, AES); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(AES); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, key); // 正式执行解密操作 byte[] result = cipher.doFinal(content); return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; } /** * Aes加密 * @param context 明文 * @param private_key 秘钥 * @return * @throws DecoderException */ public static String encryption(String context,String private_key) throws DecoderException{ //加密后的明文也就变成了密文 byte[] encryptResult = encrypt(context, private_key); //密码文Hex编码 String encryptResultStr = Hex.encodeHexString(encryptResult); return encryptResultStr; } /** * Aes解密 * @param context 密文 * @param private_key 秘钥 * @return * @throws DecoderException * @throws UnsupportedEncodingException */ public static String decryption(String context,String private_key) throws DecoderException, UnsupportedEncodingException{ //这里的密文解密前先进行了Hex解码 byte[] decryptResult = decrypt(context, private_key); String result = new String(decryptResult, UTF8); return result; } public static void main(String[] args) throws UnsupportedEncodingException, DecoderException { //加密内容 String content = "123456787654321"; //AES加密解密秘钥 String password = "这个值一般都是给定的,双发都知道"; // 加密 System.out.println("加密前:" + content); // 调用加密方法 String encryptResultStr = encryption(content, password); System.out.println("加密后:" + encryptResultStr); // 调用解密方法 String result = decryption(encryptResultStr, password); // 解密内容进行解码 System.out.println("解密后:" + result); } }
這個方法在正式的專案中已經在使用木有問題,注意這裡的AES加密解密你要要對哦…
上面使用的就是org.apache.commons.codec.binary.Hex這個類別的方法,在maven中配置如下:
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.4</version> </dependency>
注意:這裡要使用1.4以及以上版本,應為1.4以下的沒有Hex.encodeHexString(byte[])這個方法!
更多java使用Hex編碼解碼實現Aes加密解密功能範例相關文章請關注PHP中文網!

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

Java代碼在不同平台上運行時會有性能差異。 1)JVM的實現和優化策略不同,如OracleJDK和OpenJDK。 2)操作系統的特性,如內存管理和線程調度,也會影響性能。 3)可以通過選擇合適的JVM、調整JVM參數和代碼優化來提升性能。

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑戰WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具