Java decrypts AES-128-ECB encryption using AES encryption

Source: Internet
Author: User
Tags base64 decrypt



Http://www.cnblogs.com/chen-lhx/p/5817161.html



*************************************************


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
/ **
 *
 * @author Administrator
 *
 * /
public class AES {

    // encryption
    public static String Encrypt (String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print ("Key is null");
            return null;
        }
        // determine whether the Key is 16 bits
        if (sKey.length ()! = 16) {
            System.out.print ("Key length is not 16 bits");
            return null;
        }
        byte [] raw = sKey.getBytes ("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES");
        Cipher cipher = Cipher.getInstance ("AES / ECB / PKCS5Padding"); // "Algorithm / pattern / complement method"
        cipher.init (Cipher.ENCRYPT_MODE, skeySpec);
        byte [] encrypted = cipher.doFinal (sSrc.getBytes ("utf-8"));

        return new Base64 (). encodeToString (encrypted); // Base64 is used here as the transcoding function, and it can also play the role of secondary encryption.
    }

    // decrypt
    public static String Decrypt (String sSrc, String sKey) throws Exception {
        try {
            // determine if the Key is correct
            if (sKey == null) {
                System.out.print ("Key is null");
                return null;
            }
            // determine whether the Key is 16 bits
            if (sKey.length ()! = 16) {
                System.out.print ("Key length is not 16 bits");
                return null;
            }
            byte [] raw = sKey.getBytes ("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES");
            Cipher cipher = Cipher.getInstance ("AES / ECB / PKCS5Padding");
            cipher.init (Cipher.DECRYPT_MODE, skeySpec);
            byte [] encrypted1 = new Base64 (). decode (sSrc);
            try {
                byte [] original = cipher.doFinal (encrypted1);
                String originalString = new String (original, "utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println (e.toString ());
                return null;
            }
        } catch (Exception ex) {
            System.out.println (ex.toString ());
            return null;
        }
    }

    public static void main (String [] args) throws Exception {
        / *
         * AES-128-ECB encryption mode is used here, and the key needs to be 16 bits.
         * /
        String cKey = "1234567890123456";
        // string to be encrypted          System.out.println ("The encrypted string is:" + enString);

        // decrypt
        String DeString = AES.Decrypt (enString, cKey);
        System.out.println ("The decrypted string is:" + DeString);
    }
}

// Source code snippet from cloud code http://yuncode.net 


Android


package cd.server.data;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import android.annotation.SuppressLint;
import android.util.Base64;

// http://blog.csdn.net/hbcui1984/article/details/5201247
// AES encryption and decryption tool class
public class AES128
{
    private static final String UTF_8 = "UTF-8";

    / **
     * Encryption
     * @param content Content to be encrypted
     * @param password encrypted password
     * @return
     * /
    @SuppressLint ("TrulyRandom")
    public static String encrypt (String key, String src)
    {
        try
        {
            if (key == null || key.length () <= 0 || src == null || src.length () <= 0) return null;
            
            SecretKeySpec skey = new SecretKeySpec (key.getBytes (), "AES");
            Cipher cipher = Cipher.getInstance ("AES / ECB / PKCS5Padding");
            byte [] byteContent = src.getBytes (UTF_8);
            cipher.init (Cipher.ENCRYPT_MODE, skey); // initialization
            byte [] result = cipher.doFinal (byteContent);
            return byte2Base64 (result); // encryption
        }
        catch (Exception ext)
        {
            ext.printStackTrace ();
        }
        return null;
    }
    private static String byte2Base64 (byte [] encode)
    {
        try
        {
            return Base64.encodeToString (encode, Base64.NO_WRAP);

        }
        catch (Exception ext)
        {
            ext.printStackTrace ();
        }

        return null;
    }
} 





Java decrypts AES-128-ECB encryption using AES encryption


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.