English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the implementation of AES in Java256Encryption and decryption functions. Shared for everyone's reference, as follows:
I. Code
package com.handler; import java.io.UnsupportedEncodingException; import java.security.Key; import java.security.Security; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class AES256Encryption{}} public static final String KEY_ALGORITHM="AES"; public static final String CIPHER_ALGORITHM="AES/ECB/PKCS7Padding"; public static byte[] initkey() throws Exception{ //Instantiate the key generator Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyGenerator kg=KeyGenerator.getInstance(KEY_ALGORITHM, "BC"); kg.init(256); kg.init(128); SecretKey secretKey=kg.generateKey(); return secretKey.getEncoded(); } public static byte[] initRootKey() throws Exception{ return new byte[] { 0x08, 0x08, 0x04, 0x0b, 0x02, 0x0f, 0x0b, 0x0c, 0x01, 0x03, 0x09, 0x07, 0x0c, 0x03, 0x07, 0x0a, 0x04, 0x0f, 0x06, 0x0f, 0x0e, 0x09, 0x05, 0x01, 0x0a, 0x0a, 0x01, 0x09, 0x06, 0x07, 0x09, 0x0d }; } public static Key toKey(byte[] key) throws Exception{ SecretKey secretKey=new SecretKeySpec(key,KEY_ALGORITHM); return secretKey; } public static byte[] encrypt(byte[] data,byte[] key) throws Exception{ Key k=toKey(key); Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM, "BC"); cipher.init(Cipher.ENCRYPT_MODE, k); return cipher.doFinal(data); } public static byte[] decrypt(byte[] data,byte[] key) throws Exception{ Key k = toKey(key); Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM, "BC"); cipher.init(Cipher.DECRYPT_MODE, k); return cipher.doFinal(data); } public static void main(String[] args) throws UnsupportedEncodingException{ String str="芸sweet"; //Print original text System.out.println("Original text: "+str); //Key byte[] key; try { //Generate a random key key = AES256Encryption.initkey(); //Print key System.out.print("Key: "); for(int i = 0;i System.out.printf("%x", key[i]); } System.out.print("n"); //Encrypt byte[] data=AES256Encryption.encrypt(str.getBytes(), key); //Print ciphertext System.out.print("Encrypted: "); for(int i = 0;i System.out.printf("%x", data[i]); } System.out.print("n"); //Decrypt ciphertext data=AES256Encryption.decrypt(data, key); //Print original text System.out.println("Decrypted: "+new String(data)); } catch (Exception e) { e.printStackTrace(); }
Second. Note
1.bcprov needs to be imported into the project.-jdk15-133.jar
Download link of this site.
2.Replace 'local_policy.jar' and 'US_export_policy.jar' under 'jrelibsecurity'.
1If the program uses the system JDK, replace the jar package under 'jrelibsecurity' in the system environment variable's JDK.
2If the program is running in MyEclipse, find the JDK used by MyEclipse (method: enter 'window' in MyEclipse)->Preferences->There is an option called Installed JREs in the java options, click on the right to see a list, which contains the JDK version and path you are currently using), replace the jar package under jrelibsecurity in the jdk.
Can solve: java.security.InvalidKeyException: Illegal key size or default parameters exception
3. If the key needs to be stored in the database, the key needs to be encoded in base64Encoding, which means converting the key (byte array) through base64Encoding, which means converting the key (byte array) into a key (String type); when reading the key from the database, use base64Decoding, which means converting the key (String type) into a key (byte array). See 'Java Implementation of base64Encoding>
PS: Those who are interested in encryption and decryption can also refer to the online tools on this site:
MD5Online Encryption Tool:
http://tools.jb51.net/password/CreateMD5Password
Xunlei, Kuaiche, Xuanfeng URL Encryption/Decryption Tool:
http://tools.jb51.net/password/urlrethunder
Online Hashing/Hash Algorithm Encryption Tool:
http://tools.jb51.net/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 Encryption Tool:
http://tools.jb51.net/password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512Encryption Tool:
http://tools.jb51.net/password/sha_encode
I hope the description in this article will be helpful to everyone in java program design.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)