Skip to content

Commit 5a42ed5

Browse files
committed
新增 弱引用handler 3des加密工具类
1 parent 23f0cc7 commit 5a42ed5

File tree

3 files changed

+209
-1
lines changed

3 files changed

+209
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ allprojects {
4444
| StatusBarUtils |状态栏处理工具类 ,支持沉浸式状态栏| [StatusBarUtils][17] | |
4545
| ToolResource | Android 获取资源工具类 | [ToolResource][18] | |
4646
| TouchEventUtil | Android Touch事件打印辅助工具类 | [TouchEventUtil][19] | |
47+
| WeakRefHander | 弱引用 handler 防止内存泄露 | [WeakRefHander][23] | |
48+
| SecretUtils | 3DES 加密/解密 | [SecretUtils][24] | |
4749

4850
```
4951
dependencies {
@@ -119,4 +121,6 @@ dependencies {
119121
[19]: https://github.com/AllenCoder/SuperUtils/blob/master/apputils/src/main/java/com/allen/apputils/TouchEventUtil.java
120122
[20]: https://github.com/AllenCoder/SuperUtils/blob/master/dbutils/src/main/java/com/allen/dbutils/Utils.java
121123
[21]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/ImageTakerHelper.java
122-
[22]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/Utils.java
124+
[22]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/Utils.java
125+
[23]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/WeakRefHander.java
126+
[24]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/SecretUtils.java
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2017 [AllenCoderr]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.allen.apputils;
18+
19+
20+
import java.io.UnsupportedEncodingException;
21+
import java.security.NoSuchAlgorithmException;
22+
import javax.crypto.Cipher;
23+
import javax.crypto.NoSuchPaddingException;
24+
import javax.crypto.SecretKey;
25+
import javax.crypto.spec.SecretKeySpec;
26+
27+
/**
28+
* 3DES 加密/解密
29+
*/
30+
31+
32+
33+
34+
/**
35+
* SecretUtils {3DES加密解密的工具类 }
36+
37+
*/
38+
public class SecretUtils {
39+
40+
//定义加密算法,有DES、DESede(即3DES)、Blowfish
41+
private static final String Algorithm = "DESede";
42+
private static final String PASSWORD_CRYPT_KEY = "2017abcdefghijklmnopQrst123";
43+
44+
45+
/**
46+
* 加密方法
47+
* @param src 源数据的字节数组
48+
* @return
49+
*/
50+
public static byte[] encryptMode(byte[] src) {
51+
try {
52+
SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm); //生成密钥
53+
Cipher c1 = Cipher.getInstance(Algorithm); //实例化负责加密/解密的Cipher工具类
54+
c1.init(Cipher.ENCRYPT_MODE, deskey); //初始化为加密模式
55+
return c1.doFinal(src);
56+
} catch (java.security.NoSuchAlgorithmException e1) {
57+
e1.printStackTrace();
58+
} catch (javax.crypto.NoSuchPaddingException e2) {
59+
e2.printStackTrace();
60+
} catch (java.lang.Exception e3) {
61+
e3.printStackTrace();
62+
}
63+
return null;
64+
}
65+
66+
67+
/**
68+
* 解密函数
69+
* @param src 密文的字节数组
70+
* @return
71+
*/
72+
public static byte[] decryptMode(byte[] src) {
73+
try {
74+
SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);
75+
Cipher c1 = Cipher.getInstance(Algorithm);
76+
c1.init(Cipher.DECRYPT_MODE, deskey); //初始化为解密模式
77+
return c1.doFinal(src);
78+
} catch (java.security.NoSuchAlgorithmException e1) {
79+
e1.printStackTrace();
80+
} catch (javax.crypto.NoSuchPaddingException e2) {
81+
e2.printStackTrace();
82+
} catch (java.lang.Exception e3) {
83+
e3.printStackTrace();
84+
}
85+
return null;
86+
}
87+
88+
89+
/*
90+
* 根据字符串生成密钥字节数组
91+
* @param keyStr 密钥字符串
92+
* @return
93+
* @throws UnsupportedEncodingException
94+
*/
95+
public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException{
96+
byte[] key = new byte[24]; //声明一个24位的字节数组,默认里面都是0
97+
byte[] temp = keyStr.getBytes("UTF-8"); //将字符串转成字节数组
98+
99+
/*
100+
* 执行数组拷贝
101+
* System.arraycopy(源数组,从源数组哪里开始拷贝,目标数组,拷贝多少位)
102+
*/
103+
if(key.length > temp.length){
104+
//如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中
105+
System.arraycopy(temp, 0, key, 0, temp.length);
106+
}else{
107+
//如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中
108+
System.arraycopy(temp, 0, key, 0, key.length);
109+
}
110+
return key;
111+
}
112+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2017 [AllenCoderr]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.allen.apputils;
18+
19+
import android.os.Handler;
20+
import android.os.Message;
21+
22+
import java.lang.ref.WeakReference;
23+
24+
/**
25+
* 作者: allen on 15/11/24.
26+
*/
27+
28+
/**
29+
* 弱引用 handler 防止内存泄露
30+
*/
31+
public class WeakRefHander extends Handler {
32+
33+
private final WeakReference<Callback> mRef;
34+
private final int mLoopTime;
35+
private int NO_LOOP = -1;
36+
private int what =0;
37+
38+
/**
39+
* 循环
40+
*
41+
* @param loopAction
42+
* @param loopTime
43+
*/
44+
public WeakRefHander(Callback loopAction, int loopTime) {
45+
super();
46+
this.mRef = new WeakReference<>(loopAction);
47+
this.mLoopTime = loopTime;
48+
49+
}
50+
51+
/**
52+
* 不循环
53+
*
54+
* @param loopAction
55+
*/
56+
public WeakRefHander(Callback loopAction) {
57+
super();
58+
mRef = new WeakReference<>(loopAction);
59+
mLoopTime = NO_LOOP;
60+
}
61+
62+
@Override
63+
public void handleMessage(Message msg) {
64+
Callback action = mRef.get();
65+
if (action != null) {
66+
action.handleMessage(msg);
67+
if (mLoopTime != NO_LOOP) {
68+
sendEmptyMessageDelayed(what, mLoopTime);
69+
}
70+
}
71+
}
72+
73+
public void start() {
74+
removeMessages(0);
75+
sendEmptyMessageDelayed(0, 0);
76+
}
77+
78+
public void start(int what, long delay) {
79+
this.what = what;
80+
removeMessages(what);
81+
sendEmptyMessageDelayed(what, delay);
82+
}
83+
84+
public void stop() {
85+
removeMessages(what);
86+
}
87+
88+
public void clear() {
89+
removeMessages(what);
90+
mRef.clear();
91+
}
92+
}

0 commit comments

Comments
 (0)