在java中用aes256進行加密,可是發現java裏面不能使用PKCS7Padding,而java中自帶的是PKCS5Padding填充,那解決辦法是,經過BouncyCastle組件來讓java裏面支持PKCS7Padding填充。html
説辣麼多不如上代碼:java
public class AESUtil {
/**
* Encodes a String in AES-256 with a given key
*
* @param context
* @param password
* @param text
* @return String Base64 and AES encoded String
*/
public static String encode(String keyString, String stringToEncode) throws NullPointerException {
if (keyString.length() == 0 || keyString == null) {
throw new NullPointerException("Please give Password");
}
if (stringToEncode.length() == 0 || stringToEncode == null) {
throw new NullPointerException("Please give text");
}
try {
SecretKeySpec skeySpec = getKey(keyString);
byte[] clearText = stringToEncode.getBytes("UTF8");
// IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
final byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x00);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
/**
* 這個地方調用BouncyCastleProvider
*讓java支持PKCS7Padding
*/
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
// Cipher is not thread safe
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
String encrypedValue = Base64.encodeToString(cipher.doFinal(clearText), Base64.DEFAULT);
// Log.d("jacek", "Encrypted: " + stringToEncode + " -> " + encrypedValue);
return encrypedValue;
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return "";
}
/**
* Decodes a String using AES-256 and Base64
*
* @param context
* @param password
* @param text
* @return desoded String
*/
public static String decode(String password, String text) throws NullPointerException {
if (password.length() == 0 || password == null) {
throw new NullPointerException("Please give Password");
}
if (text.length() == 0 || text == null) {
throw new NullPointerException("Please give text");
}
try {
SecretKey key = getKey(password);
// IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
final byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x00);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
byte[] encrypedPwdBytes = Base64.decode(text, Base64.DEFAULT);
// cipher is not thread safe
/**
* 這個地方調用BouncyCastleProvider
*讓java支持PKCS7Padding
*/
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));
String decrypedValue = new String(decrypedValueBytes);
//Log.d(LOG_TAG, "Decrypted: " + text + " -> " + decrypedValue);
return decrypedValue;
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return "";
}
/**
* Generates a SecretKeySpec for given password
*
* @param password
* @return SecretKeySpec
* @throws UnsupportedEncodingException
*/
private static SecretKeySpec getKey(String password) throws UnsupportedEncodingException {
// You can change it to 128 if you wish
int keyLength = 256;
byte[] keyBytes = new byte[keyLength / 8];
// explicitly fill with zeros
Arrays.fill(keyBytes, (byte) 0x0);
// if password is shorter then key length, it will be zero-padded
// to key length
byte[] passwordBytes = password.getBytes("UTF-8");
int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length;
System.arraycopy(passwordBytes, 0, keyBytes, 0, length);
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
return key;
}
public static void main(String args[])
{
long startTime = System.currentTimeMillis();
String encodeStr = AESUtil.encode("1234", "你要去哪兒test123");
System.out.println("encoder:"+encodeStr);
String decodeStr = AESUtil.decode("1234",encodeStr);
System.out.println("decoder:"+decodeStr);
}
}android
BouncyCastle組件相關jar包bcprov-jdk15on-152.jar的下載地址:http://www.bouncycastle.org
oracle
默認 Java 中僅支持 128 位密鑰,當使用 256 位密鑰的時候,會報告密鑰長度錯誤 你須要下載一個支持更長密鑰的包。這個包叫作ide
,能夠從這裏下載,下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html。下好了zip文件之後,把裏面的兩個jar包(local_policy.jar,US_export_policy.jar)替換掉jdk的security文件夾中相應的jar包就OK啦。加密
可是我確實遇到一個比較奇怪的問題,我在android上面調用PKCS7Padding 是沒有問題的,可是在java寫的服務端就有問題了。不知道爲何,我用的jdk都是1.8.0的jdk。還請各位若是有遇到相同狀況的能夠解答一下。spa