# Pistols
**Repository Path**: tom.shen/Mark04
## Basic Information
- **Project Name**: Pistols
- **Description**: Android Assets内文件安全防护解决方案
- **Primary Language**: Android
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2020-03-21
- **Last Updated**: 2022-04-02
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Pistols


> Android Assets内文件安全防护解决方案
## **Encrypt**
1. 读取文件流并转换成String
2. 将String格式的文件数据按117字节长度分割
3. 分别将各段数据进行RSA加密Base64转码
4. 拼接各段的结果,并输出到文件中
```java
public final class DataEncrypt {
private final static int RSA_CONTENT_MAX_LENGTH = 117;
public static String getEncryptData(String input) throws Exception {
StringBuilder sb = new StringBuilder();
int times = input.length() / RSA_CONTENT_MAX_LENGTH;
if (input.length() % RSA_CONTENT_MAX_LENGTH != 0) {
times++;
}
for (int i = 0; i < times; i++) {
int startIndex = RSA_CONTENT_MAX_LENGTH * i;
int endIndex = RSA_CONTENT_MAX_LENGTH * (i + 1) > input.length() ? input.length() : RSA_CONTENT_MAX_LENGTH * (i + 1);
String subStr = input.substring(startIndex, endIndex);
sb.append(getEncryptStr(subStr));
}
return sb.toString();
}
private static String getEncryptStr(String input) throws Exception {
return new String(Base64Encryption.encrypt(RSAEncryption.encrypt(PUBLIC_KEY, input.getBytes("utf-8"))), "utf-8");
}
}
```
## **Decrypt**
1. 读取文件流并转换成String
2. 将String格式的文件数据按172字节长度分割
3. 分别将各段数据进行Base64解码RSA解密
4. 拼接各段的结果,并输出到文件中
```java
public final class DataDecrypt {
private final static int RSA_DECRYPT_DATA_LENGTH = 172;
public static String getDecryptData(String input, String privateKey) throws Exception {
StringBuilder sb = new StringBuilder();
int times = input.length() / RSA_DECRYPT_DATA_LENGTH;
if (input.length() % RSA_DECRYPT_DATA_LENGTH != 0) {
times++;
}
for (int i = 0; i < times; i++) {
int startIndex = RSA_DECRYPT_DATA_LENGTH * i;
int endIndex = RSA_DECRYPT_DATA_LENGTH * (i + 1);
String subStr = input.substring(startIndex, endIndex);
sb.append(getDecryptStr(subStr, privateKey));
}
return sb.toString();
}
private static String getDecryptStr(String input, String privateKey) throws Exception {
return new String(RSAEncryption.decrypt(privateKey, Base64Encryption.decrypt(input)), "utf-8");
}
}
```
## **Fingerprint**
1. 收集被加密的文件列表和RSA私钥
2. 将两者数据按照一定规则编码并生成String串(如果不考虑方法数的问题,可以使用protobuf进行数据存储)
3. 将编码后的数据输出到Assets下ytsInfo.jpg文件中
```java
public final class KeyChainMixer {
public static String getKeyChainInfo(List fileNames) throws Exception {
if (null == fileNames || fileNames.size() == 0) {
return null;
}
SecurityModel.Builder securityModelBuilder = SecurityModel.newBuilder();
for (String fileName : fileNames) {
securityModelBuilder.addInclude(EncryptFile.newBuilder().setName(fileName).build());
}
securityModelBuilder.setDecryptKey(new StringBuilder(PRIVATE_KEY).reverse().toString());
SecurityModel securityModel = securityModelBuilder.build();
return new String(Base64Encryption.encrypt(securityModel.toByteArray()), "utf-8");
}
}
```
## **Usage**
1. 在build.gradle(Project)中添加
```bash
buildscript {
repositories {
maven { url "http://121.43.186.82:8081/nexus/content/repositories/Android" }
}
dependencies {
classpath 'com.yuntai.security:yt-security-plugin:1.0.2'
}
}
```
2. 在builde.gradle(Module)中添加
```bash
apply plugin: 'com.yuntai.config.security'
securityAssetsFiles {
include = ["XXX", "XXXX"] // 添加需要加密的文件名
}
dependencies {
// 提供解密的api
compile 'com.yuntai.security:yt-security-transfer:1.0.2'
}
```
3. 在业务中使用时
```java
public final class TransferLoader {
...
// 调用此方法即可
public static String openAssetsFile(@NonNull Context context, @NonNull String fileName) {
....
}
}
````
## **Changelog**
### **v1.0.0**
* First release!
* Fingerprint文件中的数据使用protobuf进行编码
### **v1.0.1**
* 修改Fingerprint文件数据的编码规则
### **v1.0.2**
* 修改生成Fingerprint文件时String编码集未设置的问题
## **LICENSE**
Copyright 2018 Tom.Shen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.