/**
* @Title: Encrypaes.java
* @Package Com.weidinghuo.payment.util
* @Description: TODO (describe what the file does in a sentence)
* @author Liyongdong
* @date August 17, 2016 2:45:49
*/
Package com.weidinghuo.payment.util;
Import java.io.UnsupportedEncodingException;
Import java.security.NoSuchAlgorithmException;
Import Java.security.SecureRandom;
Import Javax.crypto.Cipher;
Import Javax.crypto.KeyGenerator;
Import javax.crypto.NoSuchPaddingException;
Import Javax.crypto.SecretKey;
Import Javax.crypto.spec.SecretKeySpec;
Import org.apache.shiro.codec.Base64;
Import Com.weidinghuo.base.log.BaseLog;
/**
* @ClassName: Encrypaes
* @Description: TODO (symmetric encryption)
* @author Liyongdong
* @date August 17, 2016 2:46:09
*/
public class Encrypaes extends Baselog {
/**
* Encryption
*
* @param content
* Content that needs to be encrypted
Widows and Linux generate ciphertext compatibility mode
* @param password
* Encrypt password
* @return
* @throws nosuchalgorithmexception
* @throws nosuchpaddingexception
*/
public static byte[] Encrypt (string content, string password) throws Exception {
Keygenerator KGen = keygenerator.getinstance ("AES");
SecureRandom securerandom = securerandom.getinstance ("sha1prng");
Securerandom.setseed (Password.getbytes ());
Kgen.init (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
}
/**
* Decryption
*
* @param content
* Content to be decrypted
* @param password
* Decryption key
* @return
*/
public static byte[] Decrypt (byte[] content, String password) throws exception{
Keygenerator KGen = keygenerator.getinstance ("AES");
SecureRandom securerandom = securerandom.getinstance ("sha1prng");
Securerandom.setseed (Password.getbytes ());
Kgen.init (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; Decrypt
}
/**
* Binary conversion to 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 ();
}
/**
* Converts 16 binary to 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;
}
/**
* @Title: Encryptcode
* @Description: TODO (reversible AES encryption)
* @param content
* @param password
* @return Parameter description
* String return type
* @throws
*/
public static string Encryptcode (Object content, string password) throws Exception {
byte[] encode = Encrypt (content.tostring (), password);
return new String (Base64.encode (encode));
}
/**
* @Title: Decryptcode
* @Description: TODO (Decrypt)
* @param content
* Decryption Text
* @param password
* Key
* @return String
* @throws unsupportedencodingexception
*/
public static string Decryptcode (Object content, string password) throws Exception {
Byte[] decode = Base64.decode (content.tostring ());
byte[] Decryptresult = Decrypt (decode, password);
return new String (Decryptresult, "UTF-8");
}
}
Widows and Linux Java encryption considerations