日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java code lib aes 加解密

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

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;} }

?

轉載于:https://www.cnblogs.com/lvjianwei/p/8432679.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。