程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java完成的AES加密算法完全實例

java完成的AES加密算法完全實例

編輯:關於JAVA

java完成的AES加密算法完全實例。本站提示廣大學習愛好者:(java完成的AES加密算法完全實例)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成的AES加密算法完全實例正文


本文實例講述了java完成的AES加密算法。分享給年夜家供年夜家參考,詳細以下:

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;
/**
 * @author vipin.cb , [email protected] <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.jb51.net/password/my_password_safe

高強度暗碼生成器:
http://tools.jb51.net/password/CreateStrongPassword

MD5在線加密對象:
http://tools.jb51.net/password/CreateMD5Password

迅雷、慢車、旋風URL加密/解密對象:
http://tools.jb51.net/password/urlrethunder

在線散列/哈希算法加密對象:
http://tools.jb51.net/password/hash_encrypt

願望本文所述對年夜家java法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved