-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBase64Util.java
More file actions
68 lines (58 loc) · 1.75 KB
/
Base64Util.java
File metadata and controls
68 lines (58 loc) · 1.75 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
package com.realmo.utils;
import android.util.Base64;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.UnsupportedEncodingException;
/**
* Base64编码(加密、解密)
*/
public class Base64Util {
/**
* 加密
*/
public static String encode(String str){
try {
return Base64.encodeToString(str.getBytes("utf-8"), Base64.DEFAULT);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new String(Base64.encode(str.getBytes(), Base64.DEFAULT));
}
public static String encodeByte(byte[] bytes){
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
/**
* 对象转JSON后加密
*/
public static String encodeObject(Object object){
Gson gson=new GsonBuilder().create();
String str = gson.toJson(object);
try {
return Base64.encodeToString(str.getBytes("utf-8"), Base64.DEFAULT);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new String(Base64.encode(str.getBytes(), Base64.DEFAULT));
}
/**
* 解密
*/
public static String decode(String strBase64){
byte[] bytes = Base64.decode(strBase64, Base64.DEFAULT);
try {
return new String(bytes, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new String(bytes);
}
/**
* 解密返回byte[]
*/
public static byte[] decodeToBytes(String strBase64){
return Base64.decode(strBase64, Base64.DEFAULT);
}
public static byte[] decodeToBytes(byte[] strBase64){
return Base64.decode(strBase64, Base64.DEFAULT);
}
}