国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現的AES加密算法完整實例

2019-11-26 14:03:13
字體:
來源:轉載
供稿:網友

本文實例講述了java實現的AES加密算法。分享給大家供大家參考,具體如下:

import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import android.util.Base64;/** * @author vipin.cb , vipin.cb@experionglobal.com <br> *     Sep 27, 2013, 5:18:34 PM <br> *     Package:- <b>com.veebow.util</b> <br> *     Project:- <b>Veebow</b> *     <p> */public class AESCrypt {  private final Cipher cipher;  private final SecretKeySpec key;  private AlgorithmParameterSpec spec;  public static final String SEED_16_CHARACTER = "U1MjU1M0FDOUZ.Qz";  public AESCrypt() throws Exception {    // hash password with SHA-256 and crop the output to 128-bit for key    MessageDigest digest = MessageDigest.getInstance("SHA-256");    digest.update(SEED_16_CHARACTER.getBytes("UTF-8"));    byte[] keyBytes = new byte[32];    System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);    cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");    key = new SecretKeySpec(keyBytes, "AES");    spec = getIV();  }  public AlgorithmParameterSpec getIV() {    byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };    IvParameterSpec ivParameterSpec;    ivParameterSpec = new IvParameterSpec(iv);    return ivParameterSpec;  }  public String encrypt(String plainText) throws Exception {    cipher.init(Cipher.ENCRYPT_MODE, key, spec);    byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));    String encryptedText = new String(Base64.encode(encrypted,        Base64.DEFAULT), "UTF-8");    return encryptedText;  }  public String decrypt(String cryptedText) throws Exception {    cipher.init(Cipher.DECRYPT_MODE, key, spec);    byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);    byte[] decrypted = cipher.doFinal(bytes);    String decryptedText = new String(decrypted, "UTF-8");    return decryptedText;  }}

PS:關于加密解密感興趣的朋友還可以參考本站在線工具:

密碼安全性在線檢測:
http://tools.VeVB.COm/password/my_password_safe

高強度密碼生成器:
http://tools.VeVB.COm/password/CreateStrongPassword

MD5在線加密工具:
http://tools.VeVB.COm/password/CreateMD5Password

迅雷、快車、旋風URL加密/解密工具:
http://tools.VeVB.COm/password/urlrethunder

在線散列/哈希算法加密工具:
http://tools.VeVB.COm/password/hash_encrypt

希望本文所述對大家java程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陵水| 赤峰市| 金昌市| 腾冲县| 军事| 兴隆县| 西昌市| 沂水县| 加查县| 且末县| 车险| 开平市| 祁连县| 洛川县| 仙游县| 云霄县| 武功县| 石棉县| 毕节市| 普洱| 石嘴山市| 张家界市| 河东区| 龙岩市| 通山县| 双流县| 六安市| 阿拉善左旗| 江山市| 汝阳县| 肥城市| 石河子市| 金昌市| 丰县| 楚雄市| 司法| 固安县| 桐城市| 三明市| 五华县| 通许县|