Explorar el Código

v1.0.1开发:修改RSA加解密方法

#Suyghur hace 3 años
padre
commit
32239b0853

+ 1 - 1
build.gradle

@@ -2,7 +2,7 @@
 buildscript {
 
     ext {
-        USE_ANDROIDX_VOLLEY = true
+        USE_ANDROIDX_VOLLEY = false
         // 混淆开关
         MINIFY_ENABLE = true
         // ndk版本

+ 12 - 0
demo/src/main/java/com/yyxx/support/demo/DemoActivity.kt

@@ -9,6 +9,7 @@ import android.widget.*
 import cn.yyxx.support.AppUtils
 import cn.yyxx.support.DensityUtils
 import cn.yyxx.support.device.DeviceInfoUtils
+import cn.yyxx.support.encryption.rsa.RsaUtils
 import cn.yyxx.support.hawkeye.LogUtils
 import cn.yyxx.support.hawkeye.OwnDebugUtils
 import cn.yyxx.support.msa.MsaDeviceIdsHandler
@@ -19,6 +20,7 @@ import cn.yyxx.support.volley.source.toolbox.ImageRequest
 import com.tencent.mmkv.MMKV
 import kotlin.system.exitProcess
 
+
 /**
  * @author #Suyghur.
  * Created on 2021/04/23
@@ -146,6 +148,16 @@ class DemoActivity : Activity(), View.OnClickListener {
                         }
                     }
                 }
+                7 -> {
+                    val rsaKey =
+                        "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3eXf1JxdFtx6c8AJTdlSverL8WqRE11yFB6Q+GbQeEVXjSCgQN48qePat7mXbH4LAtjaSEqXHruP4hJO8777wYtEKNKIN2VZgWQElrllAuAtaHyA+UGKwulOKmkR8k1Oxmfd46fnQBwzy+Giab4lqQRQAObCT0QtUrlrsU1U+zwIDAQAB"
+
+//            String unamePwd = SDKDrive.getInstance().RsaEncrypt(userName + "|" + pwd);
+
+//            String unamePwd = SDKDrive.getInstance().RsaEncrypt(userName + "|" + pwd);
+                    val unamePwd = RsaUtils.encryptByPublicKey("test1231|a123456", rsaKey)
+                    LogUtils.d(unamePwd)
+                }
             }
         }
     }

+ 1 - 1
library_support/build.gradle

@@ -47,7 +47,7 @@ dependencies {
         implementation 'androidx.core:core:1.5.0'
         api files('../libs/yyxx_support_volleyx_1.0.0.jar')
     } else {
-        implementation files('../libs/android-support-v4.jar')
+        compileOnly files('../libs/android-support-v4.jar')
         api files('../libs/yyxx_support_volley_1.0.0.jar')
     }
 }

+ 5 - 1
library_support/src/main/java/cn/yyxx/support/encryption/aes/AesDecrypt.java

@@ -21,11 +21,15 @@ public class AesDecrypt extends AesCommon {
     private final Cipher aesCipher;
 
     public AesDecrypt(byte[] aesKey) {
+        this(aesKey, generateIV(aesKey));
+    }
+
+    public AesDecrypt(byte[] aesKey, byte[] iv) {
         if (aesKey == null || !(aesKey.length == 16 || aesKey.length == 32)) {
             throw new RuntimeException("aes key should be 16 bytes(128bits) or 32 bytes(256bits)");
         }
         this.aesKey = aesKey;
-        this.iv = generateIV(this.aesKey);
+        this.iv = iv;
         try {
             this.aesCipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
         } catch (NoSuchAlgorithmException e) {

+ 5 - 1
library_support/src/main/java/cn/yyxx/support/encryption/aes/AesEncrypt.java

@@ -22,11 +22,15 @@ public class AesEncrypt extends AesCommon {
     private final Cipher aesCipher;
 
     public AesEncrypt(byte[] aesKey) {
+        this(aesKey, generateIV(aesKey));
+    }
+
+    public AesEncrypt(byte[] aesKey, byte[] iv) {
         if (aesKey == null || !(aesKey.length == 16 || aesKey.length == 32)) {
             throw new RuntimeException("aes key should be 16 bytes(128bits) or 32 bytes(256bits)");
         }
         this.aesKey = aesKey;
-        this.iv = generateIV(this.aesKey);
+        this.iv = iv;
         try {
             this.aesCipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
         } catch (NoSuchAlgorithmException e) {

+ 40 - 0
library_support/src/main/java/cn/yyxx/support/encryption/aes/AesUtils.java

@@ -1,7 +1,10 @@
 package cn.yyxx.support.encryption.aes;
 
 
+import java.util.Locale;
+
 import cn.yyxx.support.encryption.Base64Utils;
+import cn.yyxx.support.encryption.HexUtils;
 
 /**
  * @author #Suyghur.
@@ -26,6 +29,21 @@ public class AesUtils {
         return Base64Utils.encode(encryptedContent);
     }
 
+    public static String encrypt2hex(String aesKey, String iv, String content) {
+        try {
+            byte[] keyBytes = aesKey.getBytes("UTF-8");
+            byte[] ivBytes = iv.getBytes("UTF-8");
+            byte[] contentBytes = content.getBytes("UTF-8");
+
+            AesEncrypt aesEncrypt = new AesEncrypt(keyBytes, ivBytes);
+            byte[] encryptedContent = aesEncrypt.encrypt(contentBytes, 0, contentBytes.length);
+            return HexUtils.bytes2HexString(encryptedContent).toLowerCase(Locale.getDefault());
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return "";
+    }
+
     /**
      * 解密
      *
@@ -41,4 +59,26 @@ public class AesUtils {
         byte[] decryptedContent = aesDecrypt.decrypt(contentBytes, 0, contentBytes.length);
         return new String(decryptedContent);
     }
+
+    /**
+     * 解密
+     *
+     * @param aesKey
+     * @param content
+     * @return
+     */
+    public static String decrypt2hex(String aesKey, String iv, String content) {
+        try {
+            byte[] keyBytes = aesKey.getBytes("UTF-8");
+            byte[] ivBytes = iv.getBytes("UTF-8");
+            byte[] contentBytes = HexUtils.hexString2Bytes(content);
+            //AES decrypt
+            AesDecrypt aesDecrypt = new AesDecrypt(keyBytes, ivBytes);
+            byte[] decryptedContent = aesDecrypt.decrypt(contentBytes, 0, contentBytes.length);
+            return new String(decryptedContent);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return "";
+    }
 }

+ 5 - 5
library_support/src/main/java/cn/yyxx/support/encryption/rsa/RsaUtils.java

@@ -461,10 +461,10 @@ public class RsaUtils {
     }
 
     public static String encryptByPublicKey(String raw) {
-        return encryptByPublicKey(RSA_PUBLIC_1024_X509_PEM, raw);
+        return encryptByPublicKey(raw, RSA_PUBLIC_1024_X509_PEM);
     }
 
-    public static String encryptByPublicKey(String key, String raw) {
+    public static String encryptByPublicKey(String raw, String key) {
         String enc = "";
         byte[] keyBytesPublic = Base64Utils.decode(key);
         try {
@@ -479,10 +479,10 @@ public class RsaUtils {
 
 
     public static String decryptByPublicKey(String enc) {
-        return decryptByPublicKey(RSA_PUBLIC_1024_X509_PEM, enc);
+        return decryptByPublicKey(enc, RSA_PUBLIC_1024_X509_PEM);
     }
 
-    public static String decryptByPublicKey(String key, String enc) {
+    public static String decryptByPublicKey(String enc, String key) {
         String raw = "";
         byte[] keyBytesPublic = Base64Utils.decode(key);
         try {
@@ -516,7 +516,7 @@ public class RsaUtils {
         byte[] keyBytesPrivate = Base64Utils.decode(priKey);
         try {
             byte[] bytes = decryptByPrivateKey(Base64Utils.decode(enc), keyBytesPrivate);
-            raw = new String(bytes, "utf-8");
+            raw = new String(bytes, "UTF-8");
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 2 - 25
library_support/src/main/java/cn/yyxx/support/ui/scaleprogress/ScaleLoadingView.java

@@ -4,7 +4,6 @@ import android.animation.Animator;
 import android.animation.ValueAnimator;
 import android.content.Context;
 import android.graphics.Canvas;
-import android.graphics.Color;
 import android.graphics.LinearGradient;
 import android.graphics.Paint;
 import android.graphics.RectF;
@@ -22,14 +21,13 @@ import java.util.List;
 public class ScaleLoadingView extends View {
 
     private float mDefaultSize = 0f;
-    private int mBarColor = Color.GREEN;
     private int startColor = 0;
     private int endColor = 0;
     private Paint mPaint;
     private List<DataBean> mDataBeanList = null;
     private int mBarCount = 5;
     private float mBarDivideSize = 0f;
-    private long mPerBarTime = 300L;
+    private final long mPerBarTime = 300L;
 
     public ScaleLoadingView(Context context, int viewSize, int divideSize, int count, int startColor, int endColor) {
         super(context);
@@ -41,21 +39,9 @@ public class ScaleLoadingView extends View {
         initView(context);
     }
 
-//    public ScaleLoadingView(Context context, @Nullable AttributeSet attrs) {
-//        this(context, attrs, 0);
-//    }
-//
-//    public ScaleLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
-//        super(context, attrs, defStyleAttr);
-//        initView(context);
-//    }
-
     private void initView(Context context) {
-//        mDefaultSize = DensityUtils.dip2px(context, 60f);
-//        mBarDivideSize = DensityUtils.dip2px(context, 5f);
         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
         mPaint.setStyle(Paint.Style.FILL);
-//        mPaint.setColor(mBarColor);
         LinearGradient linearGradient = new LinearGradient(0, 0, 180, 0, startColor, endColor, Shader.TileMode.MIRROR);
         mPaint.setShader(linearGradient);
         if (mDataBeanList == null) {
@@ -120,17 +106,8 @@ public class ScaleLoadingView extends View {
     @Override
     protected void onDetachedFromWindow() {
         super.onDetachedFromWindow();
-//        LogUtils.d(" --- onDetachedFromWindow ---");
         if (mDataBeanList != null) {
-//            LogUtils.d(" --- onDetachedFromWindow size : " + mDataBeanList.size() + " ---");
-//            for (int i = 0; i < mDataBeanList.size(); i++) {
-//                LogUtils.d("cancel valueAnimator " + i);
-//                if (mDataBeanList.get(i).valueAnimator != null) {
-//                    mDataBeanList.get(i).valueAnimator.cancel();
-//                    mDataBeanList.get(i).valueAnimator = null;
-//                }
-//
-//            }
+
             for (DataBean bean : mDataBeanList) {
                 if (bean.valueAnimator != null) {
                     bean.valueAnimator.cancel();

+ 1 - 1
library_volleyx/src/main/java/cn/yyxx/support/volley/source/Cache.java

@@ -91,7 +91,7 @@ public interface Cache {
          *
          * <p>Note that if the server returns two headers with the same (case-insensitive) name,
          * this map will only contain the one of them. {@link #allResponseHeaders} may contain all
-         * headers if the {@link .Cache} implementation supports it.
+         * headers if the {@link Cache} implementation supports it.
          */
         public Map<String, String> responseHeaders = Collections.emptyMap();
 

BIN
libs/yyxx_support_volley_1.0.0.jar


+ 1 - 0
settings.gradle

@@ -3,3 +3,4 @@ include ':library_support'
 include ':library_volleyx'
 include ':library_volley'
 rootProject.name = 'YYXXSupportSdk'
+