应该坚决拒绝 不加密地明文传输敏感数据 这对整个安卓生态都是不负责的!
最危险的是直接使用HTTP协议登录账户或交换数据。例如,攻击者在自己设置的钓鱼网络中配置DNS服务器,将软件要连接的服务器域名解析至攻击者的另一台服务器在,这台服务器就可以获得用户登录信息,或者充当客户端与原服务器的中间人,转发双方数据。
这类问题的解决方法很显然-----对敏感数据采用基于SSL/TLS的HTTPS进行传输。
HTTPS和HTTP的区别主要如下:
安卓里使用https的步骤如下,创建一个HttpsURLConnection并设置属性,然后去效验本地证书,接着发起请求,接受结果。
1.创建一个HttpsURLConnection并设置属性
URL url = new URL("https://baidu.com");
HttpsURLConnection urlConnection = url.openConnection();
InputStream in = urlConnection.getInputStream();
urlConnection.setInstanceFollowRedirects(false);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setConnectTimeout(Common.TIME_OUT);
urlConnection.setReadTimeout(Common.FILE_TIME_OUT);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
urlConnection.setRequestProperty("Accept-Encoding", "gzip");
2.效验本地证书有两种方式,一种是不安全的,什么证书都相信,另一种则是只相信指定的证书,对其进行效验。
效验要通过SSLSocketFactory创建的 SSLSocket来进行,默认的 SSLSocketFactory校验服务器的证书时,会信任设备内置的100多个根证书。
private synchronized SSLSocketFactory getDefaultSSLSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
return defaultSslSocketFactory = sslContext.getSocketFactory();
} catch (GeneralSecurityException e) {
throw new AssertionError();
}
}
校验服务器的证书,其实就是通过TrustManager来操作的,更一般的说是X509TrustManager;
private static synchronized SSLSocketFactory getDefaultSSLSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{
new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
}, null);
return sslContext.getSocketFactory();
} catch (GeneralSecurityException e) {
throw new AssertionError();
}
}
这种效验过程什么证书都会信任,没有安全性可言,接下来我们看正确的配置
public static SSLContext getSSLContext() {
// 从assets中加载证书,取到证书的输入流
InputStream is = getApplicationContext().getAssets().open("srca.cer");
// 证书工厂
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate ca = cf.generateCertificate(is);
// 加载证书到密钥库中
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null);
keyStore.setCertificateEntry("cert", ca);
// 加载密钥库到信任管理器
String algorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(algorithm);
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
// 用 TrustManager 初始化一个 SSLContext
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, null);
urlCon.setSSLSocketFactory(sslContext.getSocketFactory());
}
Android P 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉,如果当前应用的请求是http请求,而非 https,这样就会导系统禁止当前应用进行该请求,如果WebView 的url用http协议,同样会出现加载失败,https则不受影响。
所以需要做一个简单的配置,在manifest的application标签下增加一行配置,同时增加一个xml文件。
android:networkSecurityConfig="@xml/network_security_config"
然后传递参数并获取结果
Iterator> it = params.entrySet().iterator();
while (it.hasNext()) {
Entry element = (Entry) it.next();
sb2.append(element.getKey());
sb2.append("=");
sb2.append(element.getValue());
sb2.append("&");
}
if (sb2.length() > 0) {
sb2.deleteCharAt(sb2.length() - 1);
}
os = urlCon.getOutputStream();
os.write(sb2.toString().getBytes());
os.flush();
int statusCode = urlCon.getResponseCode();
if (statusCode == HttpsURLConnection.HTTP_OK) {
is = urlCon.getInputStream();
String contentEncoding = urlCon.getContentEncoding();
if ((contentEncoding != null) && contentEncoding.contains("gzip")) {
is = new GZIPInputStream(new BufferedInputStream(is));
}
// 创建字节输出流对象
baos = new ByteArrayOutputStream();
// 定义读取的长度
int len = 0;
// 定义缓冲区
byte[] buffer = new byte[1024];
// 按照缓冲区的大小,循环读取
while ((len = is.read(buffer)) != -1) {
// 根据读取的长度写入到os对象中
baos.write(buffer, 0, len);
}
// 返回字符串
String result = new String(baos.toByteArray(), "utf-8");
}
https比较消耗流量,所以可以采用http配合高强度加密的方式来进行网络传输
常见的加解密方式:
Base64加密, 单向加密(MD5和SHA), 对称加密(DES和AES), 非对称加密(RSA), 非数字签名等。
// 需要引入包:java.util.Base64
// Base64加密
private static String encode(String str) {
byte[] encodeBytes = Base64.getEncoder().encode(str.getBytes());
return new String(encodeBytes);
}
// Base64解密
private static String decode(String str) {
byte[] decodeBytes = Base64.getDecoder().decode(str.getBytes());
return new String(decodeBytes);
}
@NonNull
public static String md5(String string) {
if (TextUtils.isEmpty(string)) {
return "";
}
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(string.getBytes());
StringBuilder result = new StringBuilder();
for (byte b : bytes) {
String temp = Integer.toHexString(b & 0xff);
if (temp.length() == 1) {
temp = "0" + temp;
}
result.append(temp);
}
return result.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
计算文件的MD5值
@NonNull
public static String md5(File file) {
if (file == null || !file.isFile() || !file.exists()) {
return "";
}
FileInputStream in = null;
String result = "";
byte buffer[] = new byte[8192];
int len;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer)) != -1) {
md5.update(buffer, 0, len);
}
byte[] bytes = md5.digest();
for (byte b : bytes) {
String temp = Integer.toHexString(b & 0xff);
if (temp.length() == 1) {
temp = "0" + temp;
}
result += temp;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
虽然MD5加密本身是不可逆的,但并不是不可破译的,常见破解机制为穷举法,即 跑字典,一些常见的密码很容易在大型数据库中匹配到相同的MD5值,所以我们要想办法增加加密安全性。
1、对字符串多次MD5加密
@NonNull
public static String md5(String string, int times) {
if (TextUtils.isEmpty(string)) {
return "";
}
String md5 = md5(string);
for (int i = 0; i < times - 1; i++) {
md5 = md5(md5);
}
return md5(md5);
}
2、MD5加盐
加盐就是使用一个额外的盐值与原字符串一起加密,通常盐值可以使用用户名、string明文的hascode或是随机生成的字符串。
@NonNull
public static String md5(String string, String slat) {
if (TextUtils.isEmpty(string)) {
return "";
}
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest((string + slat).getBytes());
StringBuilder result = new StringBuilder();
for (byte b : bytes) {
String temp = Integer.toHexString(b & 0xff);
if (temp.length() == 1) {
temp = "0" + temp;
}
result.append(temp);
}
return result.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
/**
* 数据标准加密
* DES算法经过16论迭代,使用56比特长度密钥加密64比特长度(分组长度)的明文获得64比特的密文。
*/
public class DESUtil {
// 初始化向量
private static byte[] iv = { 'a', 'b', 'c', 'd', 'e', 1, 2, '*' };
// DES加密
// encryptText为原文
// encryptKey为密匙
private static String encryptDES(String encryptText, String encryptKey)
throws Exception {
// 实例化IvParameterSpec对象,使用指定的初始化向量
IvParameterSpec spec = new IvParameterSpec(iv);
// 实例化SecretKeySpec类,根据字节数组来构造SecretKeySpec
SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
// 创建密码器
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
// 用密码初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
// 执行加密操作
byte[] encryptData = cipher.doFinal(encryptText.getBytes());
// 返回加密后的数据
return Base64.getEncoder().encodeToString(encryptData);
}
// 解密
private static String decryptDES(String decryptString, String decryptKey)
throws Exception {
// 先使用Base64解密
byte[] base64byte = Base64.getDecoder().decode(decryptString);
// 实例化IvParameterSpec对象,使用指定的初始化向量
IvParameterSpec spec = new IvParameterSpec(iv);
// 实例化SecretKeySpec类,根据字节数组来构造SecretKeySpec
SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
// 创建密码器
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
// 用密码初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, key, spec);
// 获取解密后的数据
byte decryptedData[] = cipher.doFinal(base64byte);
// 将解密后数据转换为字符串输出
return new String(decryptedData);
}
}
2.AES:高级加密标准
AES算法用于替代DES,保护敏感信息,AES算法的分组长度为128比特,其密钥长度分别为128比特,192比特,256比特。
/**
* AES:高级加密标准
* AES算法用于替代DES,保护敏感信息,AES算法的分组长度为128比特,其密钥长度分别为128比特,192比特,256比特。
*/
public class AESUtil {
// 采用对称分组密码体制,密钥长度的最少支持为128、192、256
String key = "abcdefghijklmnop";
// 初始化向量参数,AES 为16bytes. DES 为8bytes, 16*8=128
String initVector = "0000000000000000";
IvParameterSpec iv;
SecretKeySpec skeySpec;
Cipher cipher;
private static class HOLDER {
private static AESUtil instance = new AESUtil();
}
public static AESUtil getInstance() {
return HOLDER.instance;
}
private AESUtil() {
try {
iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
// 这是CBC模式
// cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
// 默认就是ECB模式
cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public String encrypt(String value) {
try {
// CBC模式需要传入向量,ECB模式不需要
// cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(value.getBytes());
return Base64.encodeToString(encrypted, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String decrypt(String encrypted) {
try {
// CBC模式需要传入向量,ECB模式不需要
// cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(Base64.decode(encrypted, Base64.DEFAULT));
return new String(original);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
AES和DES在SecretKeySpec和Cipher创建的时候有不同,Cipher传入的参数"AES/ECB/PKCS5PADDING"分别是“算法/工作模式/填充模式”,工作模式默认是ECB,不需要偏移,也就不需要传入向量,而CBC和OFB是带偏移的,需要传入向量。
/**
* 非对称加密算法
*/
public class RSAUtil {
public static final String RSA = "RSA";
public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";
// 秘钥默认长度
public static final int DEFAULT_KEY_SIZE = 2048;
// 当要加密的内容超过bufferSize,则采用partSplit进行分块加密
public static final byte[] DEFAULT_SPLIT = "#PART#".getBytes();
// 当前秘钥支持加密的最大字节数
public static final int DEFAULT_BUFFERSIZE = (DEFAULT_KEY_SIZE / 8) - 11;
// 随机生成RSA密钥对,密钥长度,范围:512~2048
public static KeyPair generateRSAKeyPair(int keyLength) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
kpg.initialize(keyLength);
return kpg.genKeyPair();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
/**
* 私钥加密
* @param data 待加密数据
* @param privateKey 密钥
* @return byte[] 加密数据
*/
public static byte[] encryptByPrivateKey(byte[] data, byte[] privateKey) throws Exception {
// 得到私钥
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory kf = KeyFactory.getInstance(RSA);
PrivateKey keyPrivate = kf.generatePrivate(keySpec);
// 数据加密
Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, keyPrivate);
return cipher.doFinal(data);
}
// 使用私钥进行解密
public static byte[] decryptByPrivateKey(byte[] encrypted, byte[] privateKey) throws Exception {
// 得到私钥
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory kf = KeyFactory.getInstance(RSA);
PrivateKey keyPrivate = kf.generatePrivate(keySpec);
// 解密数据
Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
cp.init(Cipher.DECRYPT_MODE, keyPrivate);
byte[] arr = cp.doFinal(encrypted);
return arr;
}
// 用公钥对字符串进行加密
public static byte[] encryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {
// 得到公钥
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
KeyFactory kf = KeyFactory.getInstance(RSA);
PublicKey keyPublic = kf.generatePublic(keySpec);
// 加密数据
Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
cp.init(Cipher.ENCRYPT_MODE, keyPublic);
return cp.doFinal(data);
}
/**
* 公钥解密
* @param data 待解密数据
* @param publicKey 密钥
* @return byte[] 解密数据
*/
public static byte[] decryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {
// 得到公钥
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
KeyFactory kf = KeyFactory.getInstance(RSA);
PublicKey keyPublic = kf.generatePublic(keySpec);
// 数据解密
Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
cipher.init(Cipher.DECRYPT_MODE, keyPublic);
return cipher.doFinal(data);
}
// 以下开始分段解密
// 使用私钥分段解密
public static byte[] decryptByPrivateKeyForSpilt(byte[] encrypted, byte[] privateKey) throws Exception {
int splitLen = DEFAULT_SPLIT.length;
if (splitLen <= 0) {
return decryptByPrivateKey(encrypted, privateKey);
}
int dataLen = encrypted.length;
List allBytes = new ArrayList(1024);
int latestStartIndex = 0;
for (int i = 0; i < dataLen; i++) {
byte bt = encrypted[i];
boolean isMatchSplit = false;
if (i == dataLen - 1) {
// 到data的最后了
byte[] part = new byte[dataLen - latestStartIndex];
System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
byte[] decryptPart = decryptByPrivateKey(part, privateKey);
for (byte b : decryptPart) {
allBytes.add(b);
}
latestStartIndex = i + splitLen;
i = latestStartIndex - 1;
} else if (bt == DEFAULT_SPLIT[0]) {
// 这个是以split[0]开头
if (splitLen > 1) {
if (i + splitLen < dataLen) {
// 没有超出data的范围
for (int j = 1; j < splitLen; j++) {
if (DEFAULT_SPLIT[j] != encrypted[i + j]) {
break;
}
if (j == splitLen - 1) {
// 验证到split的最后一位,都没有break,则表明已经确认是split段
isMatchSplit = true;
}
}
}
} else {
// split只有一位,则已经匹配了
isMatchSplit = true;
}
}
if (isMatchSplit) {
byte[] part = new byte[i - latestStartIndex];
System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
byte[] decryptPart = decryptByPrivateKey(part, privateKey);
for (byte b : decryptPart) {
allBytes.add(b);
}
latestStartIndex = i + splitLen;
i = latestStartIndex - 1;
}
}
byte[] bytes = new byte[allBytes.size()];
{
int i = 0;
for (Byte b : allBytes) {
bytes[i++] = b.byteValue();
}
}
return bytes;
}
// 私钥分段加密
public static byte[] encryptByPrivateKeyForSpilt(byte[] data, byte[] privateKey) throws Exception {
int dataLen = data.length;
if (dataLen <= DEFAULT_BUFFERSIZE) {
return encryptByPrivateKey(data, privateKey);
}
List allBytes = new ArrayList(2048);
int bufIndex = 0;
int subDataLoop = 0;
byte[] buf = new byte[DEFAULT_BUFFERSIZE];
for (int i = 0; i < dataLen; i++) {
buf[bufIndex] = data[i];
if (++bufIndex == DEFAULT_BUFFERSIZE || i == dataLen - 1) {
subDataLoop++;
if (subDataLoop != 1) {
for (byte b : DEFAULT_SPLIT) {
allBytes.add(b);
}
}
byte[] encryptBytes = encryptByPrivateKey(buf, privateKey);
for (byte b : encryptBytes) {
allBytes.add(b);
}
bufIndex = 0;
if (i == dataLen - 1) {
buf = null;
} else {
buf = new byte[Math.min(DEFAULT_BUFFERSIZE, dataLen - i - 1)];
}
}
}
byte[] bytes = new byte[allBytes.size()];
{
int i = 0;
for (Byte b : allBytes) {
bytes[i++] = b.byteValue();
}
}
return bytes;
}
// 用公钥对字符串进行分段加密
public static byte[] encryptByPublicKeyForSpilt(byte[] data, byte[] publicKey) throws Exception {
int dataLen = data.length;
if (dataLen <= DEFAULT_BUFFERSIZE) {
return encryptByPublicKey(data, publicKey);
}
List allBytes = new ArrayList(2048);
int bufIndex = 0;
int subDataLoop = 0;
byte[] buf = new byte[DEFAULT_BUFFERSIZE];
for (int i = 0; i < dataLen; i++) {
buf[bufIndex] = data[i];
if (++bufIndex == DEFAULT_BUFFERSIZE || i == dataLen - 1) {
subDataLoop++;
if (subDataLoop != 1) {
for (byte b : DEFAULT_SPLIT) {
allBytes.add(b);
}
}
byte[] encryptBytes = encryptByPublicKey(buf, publicKey);
for (byte b : encryptBytes) {
allBytes.add(b);
}
bufIndex = 0;
if (i == dataLen - 1) {
buf = null;
} else {
buf = new byte[Math.min(DEFAULT_BUFFERSIZE, dataLen - i - 1)];
}
}
}
byte[] bytes = new byte[allBytes.size()];
{
int i = 0;
for (Byte b : allBytes) {
bytes[i++] = b.byteValue();
}
}
return bytes;
}
// 公钥分段解密
public static byte[] decryptByPublicKeyForSpilt(byte[] encrypted, byte[] publicKey) throws Exception {
int splitLen = DEFAULT_SPLIT.length;
if (splitLen <= 0) {
return decryptByPublicKey(encrypted, publicKey);
}
int dataLen = encrypted.length;
List allBytes = new ArrayList(1024);
int latestStartIndex = 0;
for (int i = 0; i < dataLen; i++) {
byte bt = encrypted[i];
boolean isMatchSplit = false;
if (i == dataLen - 1) {
// 到data的最后了
byte[] part = new byte[dataLen - latestStartIndex];
System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
byte[] decryptPart = decryptByPublicKey(part, publicKey);
for (byte b : decryptPart) {
allBytes.add(b);
}
latestStartIndex = i + splitLen;
i = latestStartIndex - 1;
} else if (bt == DEFAULT_SPLIT[0]) {
// 这个是以split[0]开头
if (splitLen > 1) {
if (i + splitLen < dataLen) {
// 没有超出data的范围
for (int j = 1; j < splitLen; j++) {
if (DEFAULT_SPLIT[j] != encrypted[i + j]) {
break;
}
if (j == splitLen - 1) {
// 验证到split的最后一位,都没有break,则表明已经确认是split段
isMatchSplit = true;
}
}
}
} else {
// split只有一位,则已经匹配了
isMatchSplit = true;
}
}
if (isMatchSplit) {
byte[] part = new byte[i - latestStartIndex];
System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
byte[] decryptPart = decryptByPublicKey(part, publicKey);
for (byte b : decryptPart) {
allBytes.add(b);
}
latestStartIndex = i + splitLen;
i = latestStartIndex - 1;
}
}
byte[] bytes = new byte[allBytes.size()];
{
int i = 0;
for (Byte b : allBytes) {
bytes[i++] = b.byteValue();
}
}
return bytes;
}
}