A complete example of AES encryption algorithm implemented by Java _java

Source: Internet
Author: User
Tags base64

This article describes the AES encryption algorithm implemented by Java. Share to everyone for your reference, specific as follows:

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, 2013, 5:18:34 PM <br> * Package :-<b>com.veebow.util</b> <br> * project:-<b>Veebow</b> * <p> */Public CL
  Ass 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 messaged
    Igest 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: About encryption and decryption interested friends can also refer to the site online tools:

Password Security online detection:
Http://tools.jb51.net/password/my_password_safe

High Strength Password Generator:
Http://tools.jb51.net/password/CreateStrongPassword

MD5 Online Encryption Tool:
Http://tools.jb51.net/password/CreateMD5Password

Thunderbolt, Express, Cyclone URL encryption/decryption tool:
Http://tools.jb51.net/password/urlrethunder

Online hashing/hashing algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt

I hope this article will help you with Java programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.