7
7
import android .util .Log ;
8
8
import android .view .View ;
9
9
import android .widget .EditText ;
10
+ import android .widget .RadioGroup ;
10
11
import android .widget .TextView ;
11
12
12
13
import com .fantasy .blogdemo .Constant ;
13
14
import com .fantasy .blogdemo .R ;
14
15
import com .fantasy .blogdemo .base .BaseActivity ;
16
+ import com .fantasy .blogdemo .crypto .utils .AESUtils ;
15
17
import com .fantasy .blogdemo .crypto .utils .CryptoHelper ;
16
18
import com .fantasy .blogdemo .crypto .utils .TripleDESUtils ;
17
19
18
20
/**
19
21
* 加解密
20
22
* <pre>
21
23
* author : Fantasy
22
- * version : 1.0 , 2019-07-10
24
+ * version : 1.1 , 2019-07-12
23
25
* since : 1.0, 2019-07-10
24
26
* </pre>
25
27
*/
@@ -29,6 +31,9 @@ public class CryptoActivity extends BaseActivity implements View.OnClickListener
29
31
private EditText mEtData ;
30
32
private EditText mEtResult ;
31
33
34
+ private int mEncryptionModeCheckedId ;
35
+ private int mOutputModeCheckedId ;
36
+
32
37
/**
33
38
* 打开“加解密”模块
34
39
*
@@ -54,14 +59,27 @@ private void bindEvent() {
54
59
mEtData = findViewById (R .id .et_crypto_data );
55
60
mEtResult = findViewById (R .id .et_crypto_result );
56
61
findViewById (R .id .btn_crypto_md5 ).setOnClickListener (this );
57
- findViewById (R .id .btn_crypto_3des_ecb_encrypt ).setOnClickListener (this );
58
- findViewById (R .id .btn_crypto_3des_ecb_decrypt ).setOnClickListener (this );
59
- findViewById (R .id .btn_crypto_3des_cbc_encrypt ).setOnClickListener (this );
60
- findViewById (R .id .btn_crypto_3des_cbc_decrypt ).setOnClickListener (this );
62
+ findViewById (R .id .btn_crypto_3des_encrypt ).setOnClickListener (this );
63
+ findViewById (R .id .btn_crypto_3des_decrypt ).setOnClickListener (this );
64
+ findViewById (R .id .btn_crypto_aes_encrypt ).setOnClickListener (this );
65
+ findViewById (R .id .btn_crypto_aes_decrypt ).setOnClickListener (this );
66
+ ((RadioGroup ) findViewById (R .id .rg_crypto_encryption_mode )).setOnCheckedChangeListener (new RadioGroup .OnCheckedChangeListener () {
67
+ @ Override
68
+ public void onCheckedChanged (RadioGroup group , int checkedId ) {
69
+ mEncryptionModeCheckedId = checkedId ;
70
+ }
71
+ });
72
+ ((RadioGroup ) findViewById (R .id .rg_crypto_output_mode )).setOnCheckedChangeListener (new RadioGroup .OnCheckedChangeListener () {
73
+ @ Override
74
+ public void onCheckedChanged (RadioGroup group , int checkedId ) {
75
+ mOutputModeCheckedId = checkedId ;
76
+ }
77
+ });
61
78
}
62
79
63
80
private void initData () {
64
- mEtKey .requestFocus ();
81
+ mEncryptionModeCheckedId = R .id .rb_crypto_ecb ;
82
+ mOutputModeCheckedId = R .id .rb_crypto_base64 ;
65
83
}
66
84
67
85
@ Override
@@ -79,44 +97,118 @@ public void onClick(View v) {
79
97
Log .d (Constant .TAG , "data : " + data + " result : " + result );
80
98
}
81
99
break ;
82
- case R .id .btn_crypto_3des_ecb_encrypt : // 3DES(ECB模式)加密
83
- if (checkKey () && checkData ()) {
84
- String key = mEtKey .getText ().toString (); // 密钥长度16位或者24位
85
- String data = mEtData .getText ().toString ();
86
- String result = TripleDESUtils .encryptECB (key , data );
87
- mEtResult .setText (result );
88
- Log .d (Constant .TAG , "data : " + data + " result : " + result + " key : " + key );
100
+ case R .id .btn_crypto_3des_encrypt : // 3DES加密
101
+ String key = mEtKey .getText ().toString (); // 密钥长度16位、24位
102
+ String iv = mEtIv .getText ().toString (); // IV偏移量的长度必须为8位
103
+ String data = mEtData .getText ().toString ();
104
+ String transformation ;
105
+ String result ;
106
+ if (mEncryptionModeCheckedId == R .id .rb_crypto_ecb ) {
107
+ transformation = "DESede/ECB/PKCS5Padding" ;
108
+ if (checkKey () && checkData ()) {
109
+ if (mOutputModeCheckedId == R .id .rb_crypto_base64 ) {
110
+ result = TripleDESUtils .encryptBase64 (data , key , transformation , null );
111
+ } else {
112
+ result = TripleDESUtils .encryptHex (data , key , transformation , null );
113
+ }
114
+ mEtResult .setText (result );
115
+ Log .d (Constant .TAG , "data : " + data + " result : " + result + " key : " + key );
116
+ }
117
+ } else {
118
+ transformation = "DESede/CBC/PKCS5Padding" ;
119
+ if (checkKey () && checkIV () && checkData ()) {
120
+ if (mOutputModeCheckedId == R .id .rb_crypto_base64 ) {
121
+ result = TripleDESUtils .encryptBase64 (data , key , transformation , iv );
122
+ } else {
123
+ result = TripleDESUtils .encryptHex (data , key , transformation , iv );
124
+ }
125
+ mEtResult .setText (result );
126
+ Log .d (Constant .TAG , "data : " + data + " result : " + result + " key : " + key + " iv : " + iv );
127
+ }
89
128
}
90
129
break ;
91
- case R .id .btn_crypto_3des_ecb_decrypt : // 3DES(ECB模式)解密
92
- if (checkKey () && checkData ()) {
93
- String key = mEtKey .getText ().toString (); // 密钥长度16位或者24位
94
- String data = mEtData .getText ().toString ();
95
- String result = TripleDESUtils .decryptECB (key , data );
96
- mEtResult .setText (result );
97
- Log .d (Constant .TAG , "data : " + data + " result : " + result + " key : " + key );
130
+ case R .id .btn_crypto_3des_decrypt : // 3DES解密
131
+ key = mEtKey .getText ().toString (); // 密钥长度16位、24位
132
+ iv = mEtIv .getText ().toString (); // IV偏移量的长度必须为8位
133
+ data = mEtData .getText ().toString ();
134
+ if (mEncryptionModeCheckedId == R .id .rb_crypto_ecb ) {
135
+ transformation = "DESede/ECB/PKCS5Padding" ;
136
+ if (checkKey () && checkData ()) {
137
+ if (mOutputModeCheckedId == R .id .rb_crypto_base64 ) {
138
+ result = TripleDESUtils .decryptBase64 (data , key , transformation , null );
139
+ } else {
140
+ result = TripleDESUtils .decryptHex (data , key , transformation , null );
141
+ }
142
+ mEtResult .setText (result );
143
+ Log .d (Constant .TAG , "data : " + data + " result : " + result + " key : " + key );
144
+ }
145
+ } else {
146
+ transformation = "DESede/CBC/PKCS5Padding" ;
147
+ if (checkKey () && checkIV () && checkData ()) {
148
+ if (mOutputModeCheckedId == R .id .rb_crypto_base64 ) {
149
+ result = TripleDESUtils .decryptBase64 (data , key , transformation , iv );
150
+ } else {
151
+ result = TripleDESUtils .decryptHex (data , key , transformation , iv );
152
+ }
153
+ mEtResult .setText (result );
154
+ Log .d (Constant .TAG , "data : " + data + " result : " + result + " key : " + key + " iv : " + iv );
155
+ }
98
156
}
99
157
break ;
100
- case R .id .btn_crypto_3des_cbc_encrypt : // 3DES(CBC模式)加密
101
- if (checkKey () && checkIV () && checkData ()) {
102
- String key = mEtKey .getText ().toString (); // 密钥长度16位或者24位
103
- String iv = mEtIv .getText ().toString (); // IV偏移量的长度必须为8位
104
- String data = mEtData .getText ().toString ();
105
- String result = TripleDESUtils .encryptCBC (key , iv , data );
106
- mEtResult .setText (result );
107
- Log .d (Constant .TAG , "data : " + data + " result : " + result
108
- + " key : " + key + " iv : " + iv );
158
+ case R .id .btn_crypto_aes_encrypt : // AES加密
159
+ key = mEtKey .getText ().toString (); // 密钥长度16位、24位、32位
160
+ iv = mEtIv .getText ().toString (); // IV偏移量的长度必须为16位
161
+ data = mEtData .getText ().toString ();
162
+ if (mEncryptionModeCheckedId == R .id .rb_crypto_ecb ) {
163
+ transformation = "AES/ECB/PKCS5Padding" ;
164
+ if (checkKey () && checkData ()) {
165
+ if (mOutputModeCheckedId == R .id .rb_crypto_base64 ) {
166
+ result = AESUtils .encryptBase64 (data , key , transformation , null );
167
+ } else {
168
+ result = AESUtils .encryptHex (data , key , transformation , null );
169
+ }
170
+ mEtResult .setText (result );
171
+ Log .d (Constant .TAG , "data : " + data + " result : " + result + " key : " + key );
172
+ }
173
+ } else {
174
+ transformation = "AES/CBC/PKCS5Padding" ;
175
+ if (checkKey () && checkIV () && checkData ()) {
176
+ if (mOutputModeCheckedId == R .id .rb_crypto_base64 ) {
177
+ result = AESUtils .encryptBase64 (data , key , transformation , iv );
178
+ } else {
179
+ result = AESUtils .encryptHex (data , key , transformation , iv );
180
+ }
181
+ mEtResult .setText (result );
182
+ Log .d (Constant .TAG , "data : " + data + " result : " + result + " key : " + key + " iv : " + iv );
183
+ }
109
184
}
110
185
break ;
111
- case R .id .btn_crypto_3des_cbc_decrypt : // 3DES(CBC模式)解密
112
- if (checkKey () && checkIV () && checkData ()) {
113
- String key = mEtKey .getText ().toString (); // 密钥长度16位或者24位
114
- String iv = mEtIv .getText ().toString (); // IV偏移量的长度必须为8位
115
- String data = mEtData .getText ().toString ();
116
- String result = TripleDESUtils .decryptCBC (key , iv , data );
117
- mEtResult .setText (result );
118
- Log .d (Constant .TAG , "data : " + data + " result : " + result
119
- + " key : " + key + " iv : " + iv );
186
+ case R .id .btn_crypto_aes_decrypt : // AES解密
187
+ key = mEtKey .getText ().toString (); // 密钥长度16位或者24位
188
+ iv = mEtIv .getText ().toString (); // IV偏移量的长度必须为8位
189
+ data = mEtData .getText ().toString ();
190
+ if (mEncryptionModeCheckedId == R .id .rb_crypto_ecb ) {
191
+ transformation = "AES/ECB/PKCS5Padding" ;
192
+ if (checkKey () && checkData ()) {
193
+ if (mOutputModeCheckedId == R .id .rb_crypto_base64 ) {
194
+ result = AESUtils .decryptBase64 (data , key , transformation , null );
195
+ } else {
196
+ result = AESUtils .decryptHex (data , key , transformation , null );
197
+ }
198
+ mEtResult .setText (result );
199
+ Log .d (Constant .TAG , "data : " + data + " result : " + result + " key : " + key );
200
+ }
201
+ } else {
202
+ transformation = "AES/CBC/PKCS5Padding" ;
203
+ if (checkKey () && checkIV () && checkData ()) {
204
+ if (mOutputModeCheckedId == R .id .rb_crypto_base64 ) {
205
+ result = AESUtils .decryptBase64 (data , key , transformation , iv );
206
+ } else {
207
+ result = AESUtils .decryptHex (data , key , transformation , iv );
208
+ }
209
+ mEtResult .setText (result );
210
+ Log .d (Constant .TAG , "data : " + data + " result : " + result + " key : " + key + " iv : " + iv );
211
+ }
120
212
}
121
213
break ;
122
214
default :
0 commit comments