|
|
@@ -5,9 +5,11 @@ import android.util.Log;
|
|
|
|
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
import java.security.InvalidKeyException;
|
|
|
import java.security.Key;
|
|
|
+import java.security.MessageDigest;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.security.NoSuchProviderException;
|
|
|
import java.security.Security;
|
|
|
@@ -81,50 +83,110 @@ public class AESUtil {
|
|
|
}
|
|
|
private final static String HEX = "0123456789ABCDEF";
|
|
|
|
|
|
- public static String encrypt(String key,String content)
|
|
|
+ public static String encrypt128(String keystring,String content)
|
|
|
{
|
|
|
- byte[] result=encrypt(content.getBytes(),key.getBytes());
|
|
|
- Log.d(TAG, "encrypt: "+toHex(result));
|
|
|
- return Base64.encodeToString(result,Base64.DEFAULT);
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ Key key= null;
|
|
|
+ key = createKey128(keystring);
|
|
|
+ byte[] result=encrypt(content.getBytes(),key);
|
|
|
+ Log.d(TAG, "encrypt: "+toHex(result));
|
|
|
+ return Base64.encodeToString(result,Base64.DEFAULT);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- public static String decrypt(String key,String content)
|
|
|
+ public static String decrypt128(String keystring,String content)
|
|
|
{
|
|
|
- byte[] buffer =Base64.decode(content, Base64.DEFAULT);
|
|
|
|
|
|
- byte[] result = decrypt(buffer,key.getBytes());
|
|
|
|
|
|
- return new String(result);
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ byte[] buffer =Base64.decode(content, Base64.DEFAULT);
|
|
|
+
|
|
|
+
|
|
|
+ Key key= null;
|
|
|
+ key = createKey128(keystring);
|
|
|
+
|
|
|
+ byte[] result = decrypt(buffer,key);
|
|
|
+
|
|
|
+ return new String(result);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
+ public static String encrypt256(String keystring,String content)
|
|
|
+ {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ Key key= null;
|
|
|
+ key = createKey256(keystring);
|
|
|
+ byte[] result=encrypt(content.getBytes(),key);
|
|
|
+ Log.d(TAG, "encrypt: "+toHex(result));
|
|
|
+ return Base64.encodeToString(result,Base64.DEFAULT);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String decrypt256(String keystring,String content)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ byte[] buffer =Base64.decode(content, Base64.DEFAULT);
|
|
|
+
|
|
|
+
|
|
|
+ Key key= null;
|
|
|
+ key = createKey256(keystring);
|
|
|
+
|
|
|
+ byte[] result = decrypt(buffer,key);
|
|
|
+
|
|
|
+ return new String(result);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
/**
|
|
|
* 加密方法
|
|
|
*
|
|
|
* @param content
|
|
|
* 要加密的字符串
|
|
|
- * @param keyBytes
|
|
|
+ * @param key
|
|
|
* 加密密钥
|
|
|
* @return
|
|
|
*/
|
|
|
- private static byte[] encrypt(byte[] content, byte[] keyBytes) {
|
|
|
+ private static byte[] encrypt(byte[] content, Key key) {
|
|
|
byte[] encryptedText = null;
|
|
|
// init(keyBytes);
|
|
|
|
|
|
- Key key;
|
|
|
+
|
|
|
Cipher cipher;
|
|
|
- // 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
|
|
|
- int base = 16;
|
|
|
- if (keyBytes.length % base != 0) {
|
|
|
- int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
|
|
|
- byte[] temp = new byte[groups * base];
|
|
|
- Arrays.fill(temp, (byte) 0);
|
|
|
- System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
|
|
|
- keyBytes = temp;
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
// 初始化
|
|
|
Security.addProvider(new BouncyCastleProvider());
|
|
|
- // 转化成JAVA的密钥格式
|
|
|
- key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
|
|
|
try {
|
|
|
// 初始化cipher
|
|
|
cipher = Cipher.getInstance(algorithmStr, "BC");
|
|
|
@@ -167,29 +229,29 @@ public class AESUtil {
|
|
|
*
|
|
|
* @param encryptedData
|
|
|
* 要解密的字符串
|
|
|
- * @param keyBytes
|
|
|
+ * @param key
|
|
|
* 解密密钥
|
|
|
* @return
|
|
|
*/
|
|
|
- private static byte[] decrypt(byte[] encryptedData, byte[] keyBytes) {
|
|
|
+ private static byte[] decrypt(byte[] encryptedData, Key key) {
|
|
|
byte[] encryptedText = null;
|
|
|
// init(keyBytes);
|
|
|
|
|
|
- Key key;
|
|
|
+// Key key;
|
|
|
Cipher cipher;
|
|
|
// 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
|
|
|
int base = 16;
|
|
|
- if (keyBytes.length % base != 0) {
|
|
|
- int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
|
|
|
- byte[] temp = new byte[groups * base];
|
|
|
- Arrays.fill(temp, (byte) 0);
|
|
|
- System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
|
|
|
- keyBytes = temp;
|
|
|
- }
|
|
|
+// if (keyBytes.length % base != 0) {
|
|
|
+// int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
|
|
|
+// byte[] temp = new byte[groups * base];
|
|
|
+// Arrays.fill(temp, (byte) 0);
|
|
|
+// System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
|
|
|
+// keyBytes = temp;
|
|
|
+// }
|
|
|
// 初始化
|
|
|
Security.addProvider(new BouncyCastleProvider());
|
|
|
// 转化成JAVA的密钥格式
|
|
|
- key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
|
|
|
+// key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
|
|
|
try {
|
|
|
// 初始化cipher
|
|
|
cipher = Cipher.getInstance(algorithmStr, "BC");
|
|
|
@@ -225,4 +287,41 @@ public class AESUtil {
|
|
|
return encryptedText;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ private static Key createKey128(String keystring) throws UnsupportedEncodingException, NoSuchAlgorithmException {
|
|
|
+
|
|
|
+
|
|
|
+ byte[] keyBytes=keystring.getBytes();
|
|
|
+ Key key;
|
|
|
+ // 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
|
|
|
+ int base = 16;
|
|
|
+ if (keyBytes.length % base != 0) {
|
|
|
+ int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
|
|
|
+ byte[] temp = new byte[groups * base];
|
|
|
+ Arrays.fill(temp, (byte) 0);
|
|
|
+ System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
|
|
|
+ keyBytes = temp;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转化成JAVA的密钥格式
|
|
|
+ key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
|
|
|
+ return key;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static Key createKey256(String key) throws UnsupportedEncodingException, NoSuchAlgorithmException {
|
|
|
+ MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
|
|
+ byte[] keyBytes = digest.digest(key.getBytes("UTF-8"));
|
|
|
+
|
|
|
+ int base = 16;
|
|
|
+ if (keyBytes.length % base != 0) {
|
|
|
+ Log.d(TAG, "createKey256: "+"key is too short, expanded");
|
|
|
+ int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
|
|
|
+ byte[] temp = new byte[groups * base];
|
|
|
+ Arrays.fill(temp, (byte) 0);
|
|
|
+ System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
|
|
|
+ keyBytes = temp;
|
|
|
+ }
|
|
|
+ return new SecretKeySpec(keyBytes, KEY_ALGORITHM);
|
|
|
+ }
|
|
|
}
|