Java AES Encryption uses the Java built-in object class to complete the AES encryption of strings __java

Source: Internet
Author: User
Tags base64 decrypt

Advanced Encryption Standard (English: Advanced encryption Standard, abbreviation: AES), also known as the Rijndael encryption method in cryptography, is a block encryption standard used by the U.S. federal government. This standard, which replaces the original DES, has been analyzed and widely used worldwide. After five years of selection process, the Advanced Encryption Standard was issued by the National Institute of Standards and Technology (NIST) on November 26, 2001 in FIPS PUB 197 and became an effective standard on May 26, 2002. In the 2006, the Advanced Encryption Standard has become one of the most popular algorithms in symmetric key cryptography.


Digression: The struggle for spears and shields is always there, and recent studies have shown that AES is not unbreakable, although the AES wall has not been cross, but has been opened a hole, but it does not necessarily mean that AES is not safe, because to really break AES is still quite complex, that is, AES is still safe.


The following encryption code is based on the Java built-in encryption object to complete AES encryption, the use of static key, you can easily change the key to dynamic input, the code is very simple to use:


* * AES Encryption * Incoming text string, get ciphertext * incoming ciphertext, decrypt and get the original text.

* Key static, a little adjustment, you can be key as a parameter real-time encryption * * */package cn.sharcom.library;
Import Javax.crypto.Cipher;
Import Javax.crypto.spec.IvParameterSpec;
Import Javax.crypto.spec.SecretKeySpec;

Import org.apache.commons.codec.binary.Base64;
  public class Encrypttools {//128 bit key private String key = "Te53dah32r9vmhep";

  Bytes Vector private String initvector = "Zem84jfyejdhmkg0"; /** * Encrypt the original text * @param key * @param initvector * @param strsource * @return/private String ENCR
    Ypt (string key, String initvector, String strsource) {string strresult = null;
      try {Ivparameterspec IV = new Ivparameterspec (initvector.getbytes ("UTF-8"));

      Secretkeyspec Skeyspec = new Secretkeyspec (key.getbytes ("UTF-8"), "AES");
      Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");

      Cipher.init (Cipher.encrypt_mode, Skeyspec, iv); Byte[] encrypted = Cipher.dofinal (Strsource.getbytes ());
      strresult = base64.encodebase64string (encrypted);
      catch (Exception e) {e.printstacktrace ();
    strresult = null;
  return strresult;  /** * Decryption of ciphertext * * @param key * @param initvector * @param encrypted * @return/private String Decrypt (string key, String initvector, string encrypted) {try {Ivparameterspec IV = new IVPARAMETERSPEC
      (Initvector.getbytes ("UTF-8"));

      Secretkeyspec Skeyspec = new Secretkeyspec (key.getbytes ("UTF-8"), "AES");
      Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");

      Cipher.init (Cipher.decrypt_mode, Skeyspec, iv);

      byte[] Original = cipher.dofinal (Base64.decodebase64 (encrypted));
    return new String (original);
    catch (Exception ex) {ex.printstacktrace ();
  return null; /** * AES Encryption <br> * Incoming text that requires encryption * * @param strsource * @return/Public String Encrypt (strin G StrText) {returnEncrypt (key, Initvector, StrText);  /** * AES Decryption <br> * Input ciphertext * * @param strciphertext * @return/public string decrypt (string
  Strciphertext) {Return decrypt (key, Initvector, Strciphertext);
 }
}


Test code:

  @Test public
  void Testencrypt ()
  {
    //fail (' not yet implemented ');

    Create a Cryptographic object
    encrypttools encrypt = new Encrypttools ();

    Sets the original
    String str = "Hello World" that needs to be encrypted;
    System.out.println (str);

    Encrypt and output ciphertext
    String strencrypt = Encrypt.encrypt (str);
    System.out.println ();

    Decrypt the text and output the original
    String strmessage = Encrypt.decrypt (strencrypt);
    System.out.println (strmessage);

  }


Output, and the ciphertext you see is not the same as here, because the Key we use is not the same.

Hello world
fho50bjyle1jnfb8+i7xkg==
Hello World


Q Group Discussion 236201801

.



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.