"Java" AES encryption and decryption | And the use of Base64 _ encryption and decryption

Source: Internet
Author: User
Tags base64 decrypt null null

AES encryption and decryption algorithm, using BASE64 to do transcoding and auxiliary encryption: package Com.wintv.common;

Import Javax.crypto.Cipher;
Import Javax.crypto.spec.IvParameterSpec;
Import Javax.crypto.spec.SecretKeySpec;

Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;

/** *****************************************************************************
* AES Plus decryption algorithm
*
* @author arix04
*
*/

public class AES {

Encryption
public static string Encrypt (String ssrc, String SKey) throws Exception {
if (SKey = = null) {
System.out.print ("key is null null");
return null;
}
Determine if key is 16 bits
if (Skey.length ()!= 16) {
System.out.print ("Key length is not 16 bits");
return null;
}
byte [] raw = skey.getbytes ();
Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES");
Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding"); "Algorithm/Mode/complement Method"
Ivparameterspec IV = new Ivparameterspec ("0102030405060708". GetBytes ()); Using CBC mode, a vector IV is required to increase the strength of the encryption algorithm
Cipher.init (Cipher.encrypt_mode, Skeyspec, iv);
byte [] encrypted = Cipher.dofinal (Ssrc.getbytes ());

return new Base64encoder (). Encode (encrypted); Here use BASE64 to do transcoding function, at the same time can play the role of 2 times encryption.
}

Decrypt
public static string Decrypt (String ssrc, String SKey) throws Exception {
try {
Determine if key is correct
if (SKey = = null) {
System.out.print ("key is null null");
return null;
}
Determine if key is 16 bits
if (Skey.length ()!= 16) {
System.out.print ("Key length is not 16 bits");
return null;
}
byte [] raw = Skey.getbytes ("ASCII");
Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES");
Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");
Ivparameterspec IV = new Ivparameterspec ("0102030405060708"
. GetBytes ());
Cipher.init (Cipher.decrypt_mode, Skeyspec, iv);
byte [] encrypted1 = new Base64decoder (). Decodebuffer (SSRC); First use Base64 to decrypt
try {
byte [] original = Cipher.dofinal (encrypted1);
String originalstring = new String (original);
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 {
/*
* Encryption with the key can be composed of 26 letters and numbers, it is best not to use reserved characters, although not wrong, as to how to decide, the individual depends on the situation
* Use AES-128-CBC encryption mode here, key needs to be 16 bits.
*/
String ckey = "1234567890123456";
Strings that need to be encrypted
String CSRC </

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.