From 5a42ed55d82d59d0adc50be3690da0c2ea6354d2 Mon Sep 17 00:00:00 2001 From: Allen Date: Tue, 11 Apr 2017 10:57:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E5=BC=B1=E5=BC=95?= =?UTF-8?q?=E7=94=A8handler=203des=E5=8A=A0=E5=AF=86=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +- .../java/com/allen/apputils/SecretUtils.java | 112 ++++++++++++++++++ .../com/allen/apputils/WeakRefHander.java | 92 ++++++++++++++ 3 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 apputils/src/main/java/com/allen/apputils/SecretUtils.java create mode 100644 apputils/src/main/java/com/allen/apputils/WeakRefHander.java diff --git a/README.md b/README.md index 2eee19d..bb65c50 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ allprojects { | StatusBarUtils |状态栏处理工具类 ,支持沉浸式状态栏| [StatusBarUtils][17] | | | ToolResource | Android 获取资源工具类 | [ToolResource][18] | | | TouchEventUtil | Android Touch事件打印辅助工具类 | [TouchEventUtil][19] | | +| WeakRefHander | 弱引用 handler 防止内存泄露 | [WeakRefHander][23] | | +| SecretUtils | 3DES 加密/解密 | [SecretUtils][24] | | ``` dependencies { @@ -119,4 +121,6 @@ dependencies { [19]: https://github.com/AllenCoder/SuperUtils/blob/master/apputils/src/main/java/com/allen/apputils/TouchEventUtil.java [20]: https://github.com/AllenCoder/SuperUtils/blob/master/dbutils/src/main/java/com/allen/dbutils/Utils.java [21]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/ImageTakerHelper.java - [22]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/Utils.java \ No newline at end of file + [22]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/Utils.java + [23]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/WeakRefHander.java + [24]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/SecretUtils.java \ No newline at end of file diff --git a/apputils/src/main/java/com/allen/apputils/SecretUtils.java b/apputils/src/main/java/com/allen/apputils/SecretUtils.java new file mode 100644 index 0000000..0d7afc1 --- /dev/null +++ b/apputils/src/main/java/com/allen/apputils/SecretUtils.java @@ -0,0 +1,112 @@ +/* + * Copyright 2017 [AllenCoderr] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.allen.apputils; + + +import java.io.UnsupportedEncodingException; +import java.security.NoSuchAlgorithmException; +import javax.crypto.Cipher; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; + +/** + * 3DES 加密/解密 + */ + + + + +/** + * SecretUtils {3DES加密解密的工具类 } + + */ +public class SecretUtils { + + //定义加密算法,有DES、DESede(即3DES)、Blowfish + private static final String Algorithm = "DESede"; + private static final String PASSWORD_CRYPT_KEY = "2017abcdefghijklmnopQrst123"; + + + /** + * 加密方法 + * @param src 源数据的字节数组 + * @return + */ + public static byte[] encryptMode(byte[] src) { + try { + SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm); //生成密钥 + Cipher c1 = Cipher.getInstance(Algorithm); //实例化负责加密/解密的Cipher工具类 + c1.init(Cipher.ENCRYPT_MODE, deskey); //初始化为加密模式 + return c1.doFinal(src); + } catch (java.security.NoSuchAlgorithmException e1) { + e1.printStackTrace(); + } catch (javax.crypto.NoSuchPaddingException e2) { + e2.printStackTrace(); + } catch (java.lang.Exception e3) { + e3.printStackTrace(); + } + return null; + } + + + /** + * 解密函数 + * @param src 密文的字节数组 + * @return + */ + public static byte[] decryptMode(byte[] src) { + try { + SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm); + Cipher c1 = Cipher.getInstance(Algorithm); + c1.init(Cipher.DECRYPT_MODE, deskey); //初始化为解密模式 + return c1.doFinal(src); + } catch (java.security.NoSuchAlgorithmException e1) { + e1.printStackTrace(); + } catch (javax.crypto.NoSuchPaddingException e2) { + e2.printStackTrace(); + } catch (java.lang.Exception e3) { + e3.printStackTrace(); + } + return null; + } + + + /* + * 根据字符串生成密钥字节数组 + * @param keyStr 密钥字符串 + * @return + * @throws UnsupportedEncodingException + */ + public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException{ + byte[] key = new byte[24]; //声明一个24位的字节数组,默认里面都是0 + byte[] temp = keyStr.getBytes("UTF-8"); //将字符串转成字节数组 + + /* + * 执行数组拷贝 + * System.arraycopy(源数组,从源数组哪里开始拷贝,目标数组,拷贝多少位) + */ + if(key.length > temp.length){ + //如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中 + System.arraycopy(temp, 0, key, 0, temp.length); + }else{ + //如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中 + System.arraycopy(temp, 0, key, 0, key.length); + } + return key; + } +} diff --git a/apputils/src/main/java/com/allen/apputils/WeakRefHander.java b/apputils/src/main/java/com/allen/apputils/WeakRefHander.java new file mode 100644 index 0000000..d09805b --- /dev/null +++ b/apputils/src/main/java/com/allen/apputils/WeakRefHander.java @@ -0,0 +1,92 @@ +/* + * Copyright 2017 [AllenCoderr] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.allen.apputils; + +import android.os.Handler; +import android.os.Message; + +import java.lang.ref.WeakReference; + +/** + * 作者: allen on 15/11/24. + */ + +/** + * 弱引用 handler 防止内存泄露 + */ +public class WeakRefHander extends Handler { + + private final WeakReference mRef; + private final int mLoopTime; + private int NO_LOOP = -1; + private int what =0; + + /** + * 循环 + * + * @param loopAction + * @param loopTime + */ + public WeakRefHander(Callback loopAction, int loopTime) { + super(); + this.mRef = new WeakReference<>(loopAction); + this.mLoopTime = loopTime; + + } + + /** + * 不循环 + * + * @param loopAction + */ + public WeakRefHander(Callback loopAction) { + super(); + mRef = new WeakReference<>(loopAction); + mLoopTime = NO_LOOP; + } + + @Override + public void handleMessage(Message msg) { + Callback action = mRef.get(); + if (action != null) { + action.handleMessage(msg); + if (mLoopTime != NO_LOOP) { + sendEmptyMessageDelayed(what, mLoopTime); + } + } + } + + public void start() { + removeMessages(0); + sendEmptyMessageDelayed(0, 0); + } + + public void start(int what, long delay) { + this.what = what; + removeMessages(what); + sendEmptyMessageDelayed(what, delay); + } + + public void stop() { + removeMessages(what); + } + + public void clear() { + removeMessages(what); + mRef.clear(); + } +}