Import java.io.UnsupportedEncodingException;
Import java.security.InvalidKeyException;
Import java.security.NoSuchAlgorithmException;
Import Java.security.SecureRandom;
Import javax.crypto.BadPaddingException;
Import Javax.crypto.Cipher;
Import javax.crypto.IllegalBlockSizeException;
Import Javax.crypto.KeyGenerator;
Import javax.crypto.NoSuchPaddingException;
Import Javax.crypto.SecretKey;
Import Javax.crypto.spec.SecretKeySpec;
public class Aesutil {
/**
* Encryption
*
* @param content that needs to be encrypted
* @param password encryption password
* @return
*/
public static byte[] Encrypt (string content, string password) {
try {
Keygenerator KGen = keygenerator.getinstance ("AES");
Kgen.init (+, New SecureRandom (Password.getbytes ()));
SecureRandom securerandom = securerandom.getinstance ("sha1prng");
Securerandom.setseed (Password.getbytes ());
Kgen.init (128,securerandom);
Secretkey Secretkey = Kgen.generatekey ();
byte[] Encodeformat = secretkey.getencoded ();
Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES");
Cipher Cipher = cipher.getinstance ("AES");//Create Cipher
byte[] bytecontent = content.getbytes ("Utf-8");
Cipher.init (Cipher.encrypt_mode, key);//initialization
Byte[] result = cipher.dofinal (bytecontent);
return result; Encryption
} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
} catch (Nosuchpaddingexception e) {
E.printstacktrace ();
} catch (InvalidKeyException e) {
E.printstacktrace ();
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
} catch (Illegalblocksizeexception e) {
E.printstacktrace ();
} catch (Badpaddingexception e) {
E.printstacktrace ();
}
return null;
}
/** decryption
* @param content to decrypt
* @param password decryption key
* @return
*/
public static byte[] Decrypt (byte[] content, String password) {
try {
Keygenerator KGen = keygenerator.getinstance ("AES");
Kgen.init (+, New SecureRandom (Password.getbytes ()));
SecureRandom securerandom = securerandom.getinstance ("sha1prng");
Securerandom.setseed (Password.getbytes ());
Kgen.init (128,securerandom);
Secretkey Secretkey = Kgen.generatekey ();
byte[] Encodeformat = secretkey.getencoded ();
Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES");
Cipher Cipher = cipher.getinstance ("AES");//Create Cipher
Cipher.init (Cipher.decrypt_mode, key);//initialization
Byte[] result = cipher.dofinal (content);
return result; Encryption
} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
} catch (Nosuchpaddingexception e) {
E.printstacktrace ();
} catch (InvalidKeyException e) {
E.printstacktrace ();
} catch (Illegalblocksizeexception e) {
E.printstacktrace ();
} catch (Badpaddingexception e) {
E.printstacktrace ();
}
return null;
}
/** converting binary into 16 binary
* @param buf
* @return
*/
public static String Parsebyte2hexstr (byte buf[]) {
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < buf.length; i++) {
String hex = integer.tohexstring (Buf[i] & 0xFF);
if (hex.length () = = 1) {
Hex = ' 0 ' + hex;
}
Sb.append (Hex.touppercase ());
}
return sb.tostring ();
}
/** converting 16 binary into binary
* @param hexstr
* @return
*/
public static byte[] Parsehexstr2byte (String hexstr) {
if (Hexstr.length () < 1)
return null;
Byte[] result = new Byte[hexstr.length ()/2];
for (int i = 0;i< hexstr.length ()/2; i++) {
int high = Integer.parseint (Hexstr.substring (i*2, i*2+1), 16);
int low = Integer.parseint (Hexstr.substring (i*2+1, i*2+2), 16);
Result[i] = (byte) (high * + low);
}
return result;
}
}
public class Aesdemo {
/** There are two limitations to this type of encryption
* The key must be 16-bit
* The length of the content to be encrypted must be a multiple of 16, and if it is not a multiple of 16, an exception will occur:
*/
public static void Main (string[] args) {
String content = "ZELIA1ZBFLWM7L3O7SSTEF5764K3VQM2";
String password = "ii9028";
Encryption
System.out.println ("Before encryption:" + content);
byte[] Encryptresult = aesutil.encrypt (content, password);
String encryptresultstr = Aesutil.parsebyte2hexstr (Encryptresult);
SYSTEM.OUT.PRINTLN ("After encryption:" + ENCRYPTRESULTSTR);
Decrypt
byte[] Decryptfrom = Aesutil.parsehexstr2byte (ENCRYPTRESULTSTR);
byte[] Decryptresult = Aesutil.decrypt (Decryptfrom,password);
System.out.println ("decrypted:" + new String (Decryptresult));
}
}
Decryption on Windows normal, encryption is normal on Linux, the exception occurs when decrypting: Javax.crypto.BadPaddingException:Given final block not properly padded