Example of AES256 encryption and decryption implemented by Java
- 2020-06-12 08:59:54
- OfStack
This paper describes the Java AES256 encryption and decryption function. To share for your reference, specific as follows:
Code 1.
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=" non-success sweet";
// Print the original
System.out.println(" The original: "+str);
// The key
byte[] key;
try {
// Generating random keys
key = AES256Encryption.initkey();
// The print key
System.out.print(" Keys: ");
for(int i = 0;i
System.out.printf("%x", key[i]);
}
System.out.print("n");
// encryption
byte[] data=AES256Encryption.encrypt(str.getBytes(), key);
// Print the ciphertext
System.out.print(" After the encryption: ");
for(int i = 0;i
System.out.printf("%x", data[i]);
}
System.out.print("n");
// unlock
data=AES256Encryption.decrypt(data, key);
// Print the original
System.out.println(" After decryption: "+new String(data));
} catch (Exception e) {
e.printStackTrace();
}
2. Pay attention to
1. It is necessary to introduce ES10en-jdk15-133. jar into the project
Download link for this site.
2. Replace es17EN_policy.jar and ES20en_export_ES22en.jar under jrelibsecurity
Download link for this site.
1) If the program USES system jdk, then replace the jar package under jrelibsecurity in the SYSTEM environment variable jdk.
2) If the program is running in MyEclipse, find jdk used by MyEclipse (method: enter window- in MyEclipse) > Preferences- > java option contains 1 Installed JREs option. Click on the right side to see a list of JDK version and path you are using). Replace the jar package under jrelibsecurity in the jdk.
Can resolve: java. security. InvalidKeyException: Illegal key or default parameters exception
3. If the key needs to be stored in the database, the key needs to be encoded with base64, which converts the key (byte array) to the key (String type) through base64 encoding; When the key is read from the database, it is decoded using base64, converting the key (type String) to the key (byte array). See Java for base64 Code for details.
PS: If you are interested in encryption and decryption, please refer to our online tools:
MD5 Online encryption Tools:
http://tools.ofstack.com/password/CreateMD5Password
URL Encryption/decryption tools:
http://tools.ofstack.com/password/urlrethunder
Online hashing/hashing algorithm encryption tools:
http://tools.ofstack.com/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/ ES95en-512 / ES96en-3 /RIPEMD-160 Encryption tools:
http://tools.ofstack.com/password/hash_md5_sha
Online sha1 sha224 / sha256 sha384 / sha512 encryption tools:
http://tools.ofstack.com/password/sha_encode
I hope this article has been helpful in java programming.