日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java code lib aes 加解密

發(fā)布時間:2025/10/17 java 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java code lib aes 加解密 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Java aes 加解密

/*** Created by LvJianwei on 2018/2/8.*/import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.DatatypeConverter; import java.security.NoSuchAlgorithmException; import java.util.Arrays;/*** @program: reflection* @description: AES* @author: LvJianwei* @create: 2018-02-08 15:07**/ public class AESDemo {public static void main(String[] args) {String key="i will always love you";byte[] keyBytes=initSecretKey();String plainText="have a nice day";System.out.println("plainText:"+plainText);byte[] encryptedBytes=encrypt(DEFAULT_CIPHER_ALGORITHM,keyBytes,KEY_ALGORITHM,plainText);System.out.println("encryptedText:"+Arrays.toString(encryptedBytes));byte[] decryptedBytes=decrypt(DEFAULT_CIPHER_ALGORITHM,keyBytes,KEY_ALGORITHM,encryptedBytes);System.out.println("decryptedText:"+new String(decryptedBytes));}/*** algorithm*/private static final String KEY_ALGORITHM = "AES";private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";/*** generate key* @return byte[] key* @throws Exception*/public static byte[] initSecretKey() {//返回生成指定算法的秘密密鑰的 KeyGenerator 對象KeyGenerator kg = null;try {kg = KeyGenerator.getInstance(KEY_ALGORITHM);} catch (NoSuchAlgorithmException e) {e.printStackTrace();return new byte[0];}//初始化此密鑰生成器,使其具有確定的密鑰大小//AES 要求密鑰長度為 128kg.init(128);//生成一個密鑰SecretKey secretKey = kg.generateKey();return secretKey.getEncoded();}/*** AESEncrypt* @param cipherAlgorithm transformation* @param keyBytes key byte array* @param keyAlgorithm SecretKeySpec's algorithm* @param plainText text to be encrypted* @return*/public static byte[] encrypt(String cipherAlgorithm, byte[] keyBytes, String keyAlgorithm, String plainText) {try {Cipher cipher = Cipher.getInstance(cipherAlgorithm);String keyStr= DatatypeConverter.printBase64Binary(keyBytes);SecretKeySpec keySpec = new SecretKeySpec(keyBytes, keyAlgorithm);cipher.init(Cipher.ENCRYPT_MODE, keySpec);byte[] encyptedBytes = cipher.doFinal(plainText.getBytes());return encyptedBytes;} catch (Exception e) {e.printStackTrace();}return null;}/*** decrypt* @param cipherAlgorithm transformation* @param keyBytes key* @param keyAlgorithm SecretKeySpec's algorithm* @param encyptedBytes encypted byte array* @return*/public static byte[] decrypt(String cipherAlgorithm, byte[] keyBytes, String keyAlgorithm, byte[] encyptedBytes) {try {Cipher cipher = Cipher.getInstance(cipherAlgorithm);String keyStr= DatatypeConverter.printBase64Binary(keyBytes);SecretKeySpec keySpec = new SecretKeySpec(keyBytes, keyAlgorithm);cipher.init(Cipher.DECRYPT_MODE, keySpec);byte[] decryptedBytes = cipher.doFinal(encyptedBytes);return decryptedBytes;} catch (Exception e) {e.printStackTrace();}return null;} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/lvjianwei/p/8432679.html

總結(jié)

以上是生活随笔為你收集整理的Java code lib aes 加解密的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。