-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
141 lines (123 loc) · 5.08 KB
/
Copy pathMainActivity.java
File metadata and controls
141 lines (123 loc) · 5.08 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.example.encrypt;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class MainActivity extends AppCompatActivity {
public static String result;
public static int i;
public static String Decrypt;
public static String Encrypt;
public static Button BTN;
byte [] KEY = new byte[16];
byte [] IV = new byte[16];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BTN = findViewById(R.id.button);
Do();
}
public void Do(){
final String Address = "0xdaF0eE10cD837166331dAfaE6d24f1E859Bd7ED7";
final String TEST="0a4ee7313109b655532c4ae0c8179214eafea0e8d9335a95d58eac6b2d343bebd74201f7a0db578ba8620bd21de4ead3";
String Key = "7081cfec455576b1befbe58da496fafb";
String iv = "4372546a795ebf2274ed23e99853b521";
KEY = hexToBytes(Key);
IV = hexToBytes(iv);
final byte[] finalKEY = KEY;
final byte[] finalIV = IV;
BTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Encrypt = Encrypt(Address, finalKEY, finalIV);
System.out.println("Encrypt : "+Encrypt);
} catch (Exception e) {
e.printStackTrace();
}
try {
Decrypt = Decrypt(TEST, finalKEY, finalIV);
System.out.println("Decrypt : "+Decrypt);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//加密
public static String Encrypt(String sSrc, byte[] sKey,byte[] Iv) throws Exception {
System.out.println("String sSrc.length = " + sSrc.length());
int i = 16-sSrc.length()%16;
System.out.println("int i = :"+ i);
byte[] text = sSrc.getBytes("UTF-8");
System.out.println("byte[] text2.length : "+ text.length);
String t="";
for (int j =0;j<i;j++){
t+="\b";
}
System.out.println("T : "+t);
byte[] b = t.getBytes("UTF-8");
byte[] data3 = new byte[text.length+b.length];
System.arraycopy(text, 0, data3, 0, text.length);
System.arraycopy(b, 0, data3, text.length, b.length);
System.out.println("DATA3 : "+data3+" length:"+data3.length);
SecretKeySpec skeySpec = new SecretKeySpec(sKey, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//"算法/模式/補碼方式"
IvParameterSpec iv = new IvParameterSpec(Iv);//使用CBC模式,需要IV,增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(data3);
return byte2Hex(encrypted);
}
// 解密
public static String Decrypt(String sSrc, byte[] sKey,byte[] IV) throws Exception {
try {
SecretKeySpec skeySpec = new SecretKeySpec(sKey, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
IvParameterSpec iv = new IvParameterSpec(IV);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = hexToBytes(sSrc);
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
String result[] = originalString.split("\b");
String need = result[0];
return need;
} catch (Exception e) {
System.out.println("Decrypted Failed");
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println("Decrypted Failed");
System.out.println(ex.toString());
return null;
}
}
//HexString 轉 Byte
public static byte[] hexToBytes(String hexString) {
char[] hex = hexString.toCharArray();
int length = hex.length / 2;
byte[] rawData = new byte[length];
for (int i = 0; i < length; i++) {
int high = Character.digit(hex[i * 2], 16);
int low = Character.digit(hex[i * 2 + 1], 16);
int value = (high << 4) | low;
if (value > 127)
value -= 256;
rawData [i] = (byte) value;
}
return rawData ;
}
//Byte 轉 HexString
public static String byte2Hex(byte[] b) {
result = "";
for (i=0 ; i<b.length ; i++){
result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
}