Aes+base64 to achieve dual encryption _ encryption

Source: Internet
Author: User
Tags base64 decrypt

I've never written a document before, and I've decided to start sorting the code from now on. It's easy to look through it later.


Because now do the project data need to encrypt, so that they see in the database is ciphertext, so first through the information on the Internet to write a cryptographic tool class.

Package com.iwork.platform.util;

Import Java.security.SecureRandom;
Import Javax.crypto.Cipher;
Import Javax.crypto.KeyGenerator;

Import Javax.crypto.spec.SecretKeySpec;
Import Sun.misc.BASE64Decoder;

Import Sun.misc.BASE64Encoder; 
	/** * Encryption and Decryption tool * * @author YKC * */public class Encryptanddecrypt {//Encryption length private final static int length = 128;
	Encoding method private Final static String ENCODE = "UTF-8";
	Secret key private Final static String Defaultkey = "sdit606.";

	Prefix private final static String Defaultprefix = "Sditencrypt";
		public static void Main (string[] args) {String mes = "test data";
		String e = Encrypt (MES);
		SYSTEM.OUT.PRINTLN ("Encrypted:" + E);
	System.out.println ("After decryption:" + Decrypt (e)); /** * AES Encryption and then use BASE64 encryption * Add prefix * 1. Identify normal data so that it is not decrypted * 2. Increased security * @param content * @return * @throws Excepti
		On */public static string encrypt (string content) {String value = ' "; try {if (!isempty (content)) {value = Defaultprefix + Base64encOde (content) (aesencrypttobytes);
			The catch (Exception e) {System.out.println ("Encryptanddecrypt (cryptographic error)");
		E.printstacktrace ();
	return value; /** * AES decrypted and then use BASE64 decryption * Add prefix * 1. Identify normal data so that it does not decrypt * 2. Increased security * @param encryptstr * @return * @throws exce
		Ption */public static string decrypt (string encryptstr) {String value = ' ";
			try {int length = Defaultprefix.length ();
				if (Encryptstr.length () > Length) {String val = encryptstr.substring (0, length);
				if (Val.equals (Defaultprefix)) {value = Aesdecryptbybytes (base64decode (length));
				else {value = Encryptstr;
			} else {value = Encryptstr;
			The catch (Exception e) {System.out.println ("Encryptanddecrypt (decryption error)");
		E.printstacktrace ();
	return value; /** * AES Encryption * @param content * @return * @throws Exception/public static byte[] Aesencrypttobytes (S Tring content) throws Exception {KeYgenerator KGen = keygenerator.getinstance ("AES");
		Kgen.init (LENGTH, New SecureRandom (Defaultkey.getbytes ()));
		Cipher Cipher = cipher.getinstance ("AES");
		Secretkeyspec sks = new Secretkeyspec (Kgen.generatekey (). getencoded (), "AES");
		Cipher.init (Cipher.encrypt_mode, SKS);
	Return Cipher.dofinal (Content.getbytes (ENCODE)); /** * AES Decryption * * @param encryptbytes * @return * @throws Exception */public static String Aesdecryptbyby
		TES (byte[] encryptbytes) throws Exception {Keygenerator KGen = keygenerator.getinstance ("AES");
		Kgen.init (LENGTH, New SecureRandom (Defaultkey.getbytes ()));
		Cipher Cipher = cipher.getinstance ("AES");
		Secretkeyspec sks = new Secretkeyspec (Kgen.generatekey (). getencoded (), "AES");
		Cipher.init (Cipher.decrypt_mode, SKS);
		byte[] decryptbytes = cipher.dofinal (encryptbytes);
	return new String (decryptbytes); /** * BASE64 Encryption * * @param content * @return * @throws Exception/public static String Base64Encode by Te[] BYtes) {return new Base64encoder (). Encode (bytes); /** * BASE64 Decryption * @param content * @return * @throws Exception/public static byte[] Base64decode (St
	Ring Base64code) throws Exception {return IsEmpty (Base64code) null:new Base64decoder (). Decodebuffer (Base64code); public static Boolean IsEmpty (String str) {return NULL = = STR | |
	"". Equals (Str.trim ()); }
}
The test results are as follows
After encryption: sditencrypttmidf05lpkksrhpx2nvkha==
After decryption: test data


In general this is the way, followed by a study of how to put the decryption method into the project. The initial thought is put in the model's get set.

PS: prefix is because there is already a lot of data in the project, plus the prefix can determine whether it is raw data, but also add a bit of security (although there is no eggs to use.) )

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.