-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDes3EncryptStrategy.java
92 lines (78 loc) · 2.49 KB
/
Des3EncryptStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.haohaohu.cachemanage.strategy;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import com.haohaohu.cachemanage.util.Des3Util;
import com.haohaohu.cachemanage.util.KeyStoreHelper;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
/**
* Des3加密策略
*
* @author haohao(ronghao3508@gmail.com) on 2018/5/28 16:18
* @version v1.0
*/
public class Des3EncryptStrategy implements IEncryptStrategy {
private String secretKey;//秘钥
private String iv = "haohaoha";//移动位置8位
private Context mContext;
public Des3EncryptStrategy(Context context, String secretKey, String iv) {
this.mContext = context;
this.secretKey = secretKey;
this.iv = iv;
}
@Override
public String encrypt(String str) {
try {
return Des3Util.encode(str, this.secretKey, this.iv);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
@Override
public String decode(String str) {
try {
return Des3Util.decode(str, this.secretKey, this.iv);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
private String createSecretKey() {
String secretKey;
String str = getAndroidID();
if (str.length() > 24) {
secretKey = str.substring(0, 24);
} else {
secretKey = str + getStr(24 - str.length());
}
return secretKey;
}
private String get24Str(String str) {
if (TextUtils.isEmpty(str)) return "";
if (str.length() >= 24) {
return str.substring(0, 24);
}
return str + getStr(24 - str.length());
}
private String getStr(int num) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < num; i++) {
builder.append("a");
}
return builder.toString();
}
@SuppressLint("HardwareIds")
public String getAndroidID() {
return Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void createKeyStoreSecretKey() {
KeyStoreHelper.createKeys(mContext, mContext.getPackageName());
}
}