怎么在Java中利用AES256進(jìn)行加密解密操作

怎么在Java 中利用AES256進(jìn)行加密解密操作?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、做網(wǎng)站、尼勒克網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、尼勒克網(wǎng)絡(luò)營銷、尼勒克企業(yè)策劃、尼勒克品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供尼勒克建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:muchs.cn

什么是 AES 256?

  • 高級(jí)加密標(biāo)準(zhǔn) (英語:Advanced Encryption Standard,縮寫:AES ),在密碼學(xué)中又稱Rijndael加密法,是美國聯(lián)邦政府采用的一種區(qū)塊加密標(biāo)準(zhǔn)。這個(gè)標(biāo)準(zhǔn)用來替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用。

  • AES是一種對(duì)稱加密算法。它旨在易于在硬件和軟件以及受限環(huán)境中實(shí)施,并提供針對(duì)各種攻擊技術(shù)的良好防御。AES是能夠使用大小為128、192和256位的密鑰處理128位塊的塊密碼。每個(gè)密碼分別使用128位,192位和256位的加密密鑰對(duì)128位塊中的數(shù)據(jù)進(jìn)行加密和解密。它使用相同的密鑰進(jìn)行加密和解密,因此發(fā)送方和接收方都必須知道并使用相同的秘密密鑰。

在下面的加密和解密示例中,我在UTF-8字符集中使用了base64編碼。用于顯示程序的輸出。也可以以字節(jié)數(shù)組格式存儲(chǔ)和驗(yàn)證數(shù)據(jù)。

AES 256加密 

Java程序中,用于使用AES 256位對(duì)密碼(或任何信息)進(jìn)行加密。

private static String secretKey = "boooooooooom!!!!";
private static String salt = "ssshhhhhhhhhhh!!!!";
 
public static String encrypt(String strToEncrypt, String secret)
{
  try
  {
    byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    IvParameterSpec ivspec = new IvParameterSpec(iv);
     
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
    KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
     
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
    return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
  }
  catch (Exception e)
  {
    System.out.println("Error while encrypting: " + e.toString());
  }
  return null;
}

AES 256解密

Java程序,用于使用AES 256位解密密碼(或任何信息)。 

private static String secretKey = "boooooooooom!!!!";
private static String salt = "ssshhhhhhhhhhh!!!!";
 
public static String decrypt(String strToDecrypt, String secret) {
  try
  {
    byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    IvParameterSpec ivspec = new IvParameterSpec(iv);
     
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
    KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
     
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
    return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
  }
  catch (Exception e) {
    System.out.println("Error while decrypting: " + e.toString());
  }
  return null;
}

測試AES256加密和解密方法

用一個(gè)簡單的字符串測試我們的AES256加密和解密方法

public static void main(String[] args)
{
  String originalString = "www.csdn.net";
   
  String encryptedString = AES.encrypt(originalString, secretKey) ;
  String decryptedString = AES.decrypt(encryptedString, secretKey) ;
   
  System.out.println(originalString);
  System.out.println(encryptedString);
  System.out.println(decryptedString);
}

輸出結(jié)果

www.csdn.net
biXhp3Ha1fgxVEp48zHrvVoXMStmxPuAPHo3TVz5lHU=
www.csdn.net

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

分享名稱:怎么在Java中利用AES256進(jìn)行加密解密操作
當(dāng)前路徑:http://muchs.cn/article22/ghegcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站建設(shè)、商城網(wǎng)站、動(dòng)態(tài)網(wǎng)站、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)