refactor(utils): 清理未使用的工具类和方法
- 删除整个 AppsManagerUtils、AppUpdateInfo、ForegroundAppUtil 类 - 移除 ApkUtils 中未使用的方法,包括 getRootAhth、openApp、installApkInSilence、addShortcut、getTaskPackname、queryFilterAppInfo、getAppIcon 等 - 移除 CacheUtils 中未使用的 getApplicationCache 方法和 PackageStatsObserver 内部类 - 移除 CXAESUtil 中未使用的加解密及文件操作方法 - 将 StringUtils.join 替换为 String.join 以消除自定义依赖 - 清理各文件中未使用的 import 语句和冗余字段
This commit is contained in:
1 parent
cf1dfe64df
commit
df8a7bc153
22 files changed
+6
-2803
No files matched your search
@@ -1,28 +1,11 @@
|
||||
package com.fuying.sn.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.CipherInputStream;
|
||||
import javax.crypto.CipherOutputStream;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
public class CXAESUtil {
|
||||
/**
|
||||
* 加解密算法/工作模式/填充方式
|
||||
*/
|
||||
private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
|
||||
public final static String HEX = "0123456789ABCDEF";
|
||||
private static final int keyLenght = 16;
|
||||
private static final String defaultV = "0";
|
||||
@@ -48,39 +31,6 @@ public class CXAESUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密
|
||||
*
|
||||
* @param key 密钥
|
||||
* @param src 加密文本
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String encrypt2Java(String key, String src) throws Exception {
|
||||
// /src = Base64.encodeToString(src.getBytes(), Base64.DEFAULT);
|
||||
byte[] rawKey = toMakekey(key, keyLenght, defaultV).getBytes();// key.getBytes();
|
||||
byte[] result = encrypt2Java(rawKey, src.getBytes("utf-8"));
|
||||
// result = Base64.encode(result, Base64.DEFAULT);
|
||||
return toHex(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
*
|
||||
* @param key 密钥
|
||||
* @param encrypted 待揭秘文本
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String decrypt(String key, String encrypted) throws Exception {
|
||||
byte[] rawKey = toMakekey(key, keyLenght, defaultV).getBytes();// key.getBytes();
|
||||
byte[] enc = toByte(encrypted);
|
||||
// enc = Base64.decode(enc, Base64.DEFAULT);
|
||||
byte[] result = decrypt(rawKey, enc);
|
||||
// /result = Base64.decode(result, Base64.DEFAULT);
|
||||
return new String(result, "utf-8");
|
||||
}
|
||||
|
||||
/**
|
||||
* 密钥key ,默认补的数字,补全16位数,以保证安全补全至少16位长度,android和ios对接通过
|
||||
*
|
||||
@@ -121,62 +71,6 @@ public class CXAESUtil {
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
/**
|
||||
* 真正的加密过程
|
||||
*
|
||||
* @param key
|
||||
* @param src
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private static byte[] encrypt2Java(byte[] key, byte[] src) throws Exception {
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
|
||||
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
|
||||
byte[] encrypted = cipher.doFinal(src);
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
/**
|
||||
* 真正的解密过程
|
||||
*
|
||||
* @param key
|
||||
* @param encrypted
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private static byte[] decrypt(byte[] key, byte[] encrypted) throws Exception {
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES");
|
||||
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
|
||||
byte[] decrypted = cipher.doFinal(encrypted);
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
public static String toHex(String txt) {
|
||||
return toHex(txt.getBytes());
|
||||
}
|
||||
|
||||
public static String fromHex(String hex) {
|
||||
return new String(toByte(hex));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 把16进制转化为字节数组
|
||||
*
|
||||
* @param hexString
|
||||
* @return
|
||||
*/
|
||||
public static byte[] toByte(String hexString) {
|
||||
int len = hexString.length() / 2;
|
||||
byte[] result = new byte[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 二进制转字符,转成了16进制
|
||||
* 0123456789abcdefg
|
||||
@@ -198,134 +92,4 @@ public class CXAESUtil {
|
||||
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化 AES Cipher
|
||||
*
|
||||
* @param sKey
|
||||
* @param cipherMode
|
||||
* @return
|
||||
*/
|
||||
public static Cipher initAESCipher(String sKey, int cipherMode) {
|
||||
// 创建Key gen
|
||||
// KeyGenerator keyGenerator = null;
|
||||
Cipher cipher = null;
|
||||
try {
|
||||
/*
|
||||
* keyGenerator = KeyGenerator.getInstance("AES");
|
||||
* keyGenerator.init(128, new SecureRandom(sKey.getBytes()));
|
||||
* SecretKey secretKey = keyGenerator.generateKey(); byte[]
|
||||
* codeFormat = secretKey.getEncoded(); SecretKeySpec key = new
|
||||
* SecretKeySpec(codeFormat, "AES"); cipher =
|
||||
* Cipher.getInstance("AES"); //初始化 cipher.init(cipherMode, key);
|
||||
*/
|
||||
byte[] rawKey = toMakekey(sKey, keyLenght, defaultV).getBytes();
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
|
||||
cipher = Cipher.getInstance("AES");
|
||||
cipher.init(cipherMode, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace(); // To change body of catch statement use File |
|
||||
// Settings | File Templates.
|
||||
} catch (NoSuchPaddingException e) {
|
||||
e.printStackTrace(); // To change body of catch statement use File |
|
||||
// Settings | File Templates.
|
||||
} catch (InvalidKeyException e) {
|
||||
e.printStackTrace(); // To change body of catch statement use File |
|
||||
// Settings | File Templates.
|
||||
} catch (InvalidAlgorithmParameterException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return cipher;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对文件进行AES加密
|
||||
*
|
||||
* @param sourceFile
|
||||
* @param sKey
|
||||
* @return
|
||||
*/
|
||||
public static File encryptFile(File sourceFile, String toFile, String dir, String sKey) {
|
||||
// 新建临时加密文件
|
||||
File encrypfile = null;
|
||||
InputStream inputStream = null;
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
inputStream = new FileInputStream(sourceFile);
|
||||
encrypfile = new File(dir + toFile);
|
||||
outputStream = new FileOutputStream(encrypfile);
|
||||
Cipher cipher = initAESCipher(sKey, Cipher.ENCRYPT_MODE);
|
||||
// 以加密流写入文件
|
||||
CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
|
||||
byte[] cache = new byte[1024];
|
||||
int nRead = 0;
|
||||
while ((nRead = cipherInputStream.read(cache)) != -1) {
|
||||
outputStream.write(cache, 0, nRead);
|
||||
outputStream.flush();
|
||||
}
|
||||
cipherInputStream.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace(); // To change body of catch statement use File |
|
||||
// Settings | File Templates.
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); // To change body of catch statement use File |
|
||||
// Settings | File Templates.
|
||||
} finally {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); // To change body of catch statement use
|
||||
// File | Settings | File Templates.
|
||||
}
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); // To change body of catch statement use
|
||||
// File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
return encrypfile;
|
||||
}
|
||||
|
||||
/**
|
||||
* AES方式解密文件
|
||||
*
|
||||
* @param sourceFile
|
||||
* @return
|
||||
*/
|
||||
public static File decryptFile(File sourceFile, String toFile, String dir, String sKey) {
|
||||
File decryptFile = null;
|
||||
InputStream inputStream = null;
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
decryptFile = new File(dir + toFile);
|
||||
Cipher cipher = initAESCipher(sKey, Cipher.DECRYPT_MODE);
|
||||
inputStream = new FileInputStream(sourceFile);
|
||||
outputStream = new FileOutputStream(decryptFile);
|
||||
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
|
||||
byte[] buffer = new byte[1024];
|
||||
int r;
|
||||
while ((r = inputStream.read(buffer)) >= 0) {
|
||||
cipherOutputStream.write(buffer, 0, r);
|
||||
}
|
||||
cipherOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); // To change body of catch statement use File |
|
||||
// Settings | File Templates.
|
||||
} finally {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); // To change body of catch statement use
|
||||
// File | Settings | File Templates.
|
||||
}
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); // To change body of catch statement use
|
||||
// File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
return decryptFile;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user