Skip to content

Instantly share code, notes, and snippets.

@AoEiuV020
Last active July 4, 2017 09:25
Show Gist options
  • Save AoEiuV020/4d079f15097b0069eb511bb641c85fec to your computer and use it in GitHub Desktop.
Save AoEiuV020/4d079f15097b0069eb511bb641c85fec to your computer and use it in GitHub Desktop.
java aes util,
import com.ddddd.util.LoggerUtil;
import org.slf4j.Logger;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import okio.ByteString;
public class AESUtil {
private static final Logger Log = LoggerUtil.getLogger(AESUtil.class);
private final static String dfEncode = "UTF-8";
private final static String KEY_GENERATION_ALG = "PBKDF2WithHmacSHA1";//PBEWITHSHAANDTWOFISH-CBC
private final static int HASH_ITERATIONS = 10000;
private final static int KEY_LENGTH = 128;//256
private final static byte[] salt = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF}; // must save this for next time we want the key
private final static byte[] iv = {0xA, 1, 0xB, 5, 4, 0xF, 7, 9, 0x17, 3, 1, 6, 8, 0xC, 0xD, 91};
private final static String CIPHERMODEPADDING = "AES/CBC/PKCS5Padding";
private static String base64(byte[] s) {
return new okio.Buffer().write(s).readByteString().base64();
}
private static byte[] decodeBase64(String s) {
return ByteString.decodeBase64(s).toByteArray();
}
public static String encrypt(String value, char[] humanPassphrase, String encode) {
String enstr = null;
try {
Cipher c = initCipher(Cipher.ENCRYPT_MODE, humanPassphrase);
byte[] enval = c.doFinal(value.getBytes(encode));
enstr = base64(enval);
} catch (Exception e) {
Log.debug("encrypt occur error e.{}", e);
throw new RuntimeException("加密异常:" + e.getMessage());
}
return enstr;
}
public static String decrypt(String value, char[] humanPassphrase, String encode) {
String enstr = null;
try {
byte[] devals = decodeBase64(value);
Cipher c = initCipher(Cipher.DECRYPT_MODE, humanPassphrase);
byte[] deval = c.doFinal(devals);
enstr = new String(deval, encode);
} catch (Exception e) {
Log.debug("decrypt occur error e.{}", e);
throw new RuntimeException("解密异常:" + e.getMessage());
}
return enstr;
}
private static Cipher initCipher(int opmode, char[] humanPassphrase) {
try {
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(KEY_GENERATION_ALG);
PBEKeySpec myKeyspec = new PBEKeySpec(humanPassphrase, salt, HASH_ITERATIONS, KEY_LENGTH);
SecretKey sk = keyfactory.generateSecret(myKeyspec);
byte[] skAsByteArray = sk.getEncoded();
SecretKeySpec skforAES = new SecretKeySpec(skAsByteArray, "AES");
IvParameterSpec IV = new IvParameterSpec(iv);
Cipher c = Cipher.getInstance(CIPHERMODEPADDING);
c.init(opmode, skforAES, IV);
return c;
} catch (NoSuchAlgorithmException nsae) {
Log.debug("AESdemo no key factory support for PBEWITHSHAANDTWOFISH-CBC NoSuchAlgorithmException e.{}", nsae);
} catch (InvalidKeySpecException ikse) {
Log.debug("AESdemo invalid key spec for PBEWITHSHAANDTWOFISH-CBC InvalidKeySpecException e.{}", ikse);
} catch (NoSuchPaddingException e) {
Log.debug("decrypt occur NoSuchPaddingException e.{}", e);
} catch (InvalidKeyException e) {
Log.debug("decrypt occur InvalidKeyException e.{}", e);
} catch (InvalidAlgorithmParameterException e) {
Log.debug("decrypt occur InvalidAlgorithmParameterException e.{}", e);
}
return null;
}
private byte[] addPadding(byte[] plain) {
byte plainpad[] = null;
int shortage = 16 - (plain.length % 16);
// if already an exact multiple of 16, need to add another block of 16
// bytes
if (shortage == 0)
shortage = 16;
// reallocate array bigger to be exact multiple, adding shortage bits.
plainpad = new byte[plain.length + shortage];
for (int i = 0; i < plain.length; i++) {
plainpad[i] = plain[i];
}
for (int i = plain.length; i < plain.length + shortage; i++) {
plainpad[i] = (byte) shortage;
}
return plainpad;
}
private byte[] dropPadding(byte[] plainpad) {
byte plain[] = null;
int drop = plainpad[plainpad.length - 1]; // last byte gives number of
plain = new byte[plainpad.length - drop];
for (int i = 0; i < plain.length; i++) {
plain[i] = plainpad[i];
plainpad[i] = 0; // don't keep a copy of the decrypt
}
return plain;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment