소스 검색

增加aes 256位加密

Ray Zhang 7 년 전
부모
커밋
e581c3709f

+ 132 - 33
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/Utils/AESUtil.java

@@ -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);
+    }
 }

+ 30 - 9
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/MainActivity.java

@@ -22,7 +22,6 @@ import com.usai.redant.apexdrivers.Home.HomeFragment;
 import com.usai.redant.apexdrivers.Login.LoginFragment;
 import com.usai.redant.apexdrivers.Network.Network;
 import com.usai.redant.apexdrivers.Upload.TaskActivity;
-import com.usai.redant.apexdrivers.Upload.UploadListActivity;
 import com.usai.redant.rautils.Utils.RAUtil;
 import com.usai.redant.rautils.Utils.dbgUtil;
 
@@ -34,9 +33,37 @@ public class MainActivity extends AppCompatActivity implements LoginFragment.Log
 
     void Test()
     {
+        dbgUtil.fileLog(this,"lib import successful");
+
+
+        RAUtil.LibTest();
+
+
+        RAUtil.LibTest1();
+
+
+        if(true)
+            return;
+
+//        String en1=AESUtil.encrypt256("usai1234usai1234usai1234usai1234","ABCDE$3&))@!_*FGHIJ^%KLMNOPQRSTUVWXYZ--ABCDE$3&))@!_*FGHIJ^%KLMNOPQRSTUVWXYZABCDE$3&))@!_*FGHIJ^%KLMNOPQRSTUVWXYZABCDE$3&))@!_*FGHIJ^%KLMNOPQRSTUVWXYZ");
+//        String de1=AESUtil.decrypt256("usai1234usai1234usai1234usai1234",en1);
+//        Log.d("AES256", "EN: "+en1+"   DE: "+de1);
+
+//        String en2=AESUtil.encrypt256("usai1234","ABCDE$3&))@!_*FGHIJ^%KLMNOPQRSTUVWXYZ--ABCDE$3&))@!_*FGHIJ^%KLMNOPQRSTUVWXYZ");
+//        String de2=AESUtil.decrypt256("usai1234",en2);
+//        Log.d("AES256", "EN: "+en2+"   DE: "+de2);
+//
+//
+//        String en3=AESUtil.encrypt256("usai1234","ABCDE$3&))@!_*FGHIJ^%KLMNOPQRSTUVWXYZ--ABCDE$3&))@!_*FGHIJ^%KLMNOPQRSTUVWXYZABCDE$3&))@!_*FGHIJ^%KLMNOPQRSTUVWXYZABCDE$3&))@!_*FGHIJ^%KLMNOPQRSTUVWXYZ");
+//        String de3=AESUtil.decrypt256("usai1234",en3);
+//        Log.d("AES256", "EN: "+en3+"   DE: "+de3);
 
-//        if(true)
-//            return;
+
+
+
+
+        if(true)
+            return;
         ServiceConnection serviceConnection= new ServiceConnection(){
 
             @Override
@@ -62,14 +89,8 @@ public class MainActivity extends AppCompatActivity implements LoginFragment.Log
         bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
 
 
-        dbgUtil.fileLog(this,"lib import successful");
 
 
-        RAUtil.LibTest();
-
-
-        RAUtil.LibTest1();
-
 
 
         int FLAG_LOCATION_NONE = 0 ;