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 </