一、前言
AES(高级加密标准)、DES(数据加密标准)、MD5(消息摘要算法5)和SHA-1(安全哈希算法1)都是常见的加密算法,用于数据加密和哈希计算。
二、加密算法实现
1、创建加密函数
java代码实现方式:
package com.yuanrenxue.course6;
import android.util.Base64;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class CryptoUtils implements ICryptoUtils {
@Override
public String aesEncrypt(String key, String iv, String content) throws Exception {
// 密钥和IV的处理
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
// Cipher对象的初始化获取Cipher对象,指定了AES算法、CBC模式和PKCS5填充。
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// iv 偏移量
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
// 初始化
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
// 加密
byte[] bytes = cipher.doFinal(content.getBytes());
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
@Override
public String aesDecrypt(String key, String iv, String content) throws Exception {
// 密钥和IV的处理
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
// Cipher对象的初始化获取Cipher对象,指定了AES算法、CBC模式和PKCS5填充。
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// iv 偏移量
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
// 初始化
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
// 解密
byte[] bytes = cipher.doFinal(Base64.decode(content, Base64.DEFAULT));
return new String(bytes);
}
@Override
public String desEncrypt(String key, String content) throws Exception {
// 密钥处理
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "DES");
// Cipher对象的初始化获取Cipher对象,指定了AES算法、CBC模式和PKCS5填充。
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// 初始化
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
// 加密
byte[] bytes = cipher.doFinal(content.getBytes());
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
@Override
public String desDecrypt(String key, String content) throws Exception {
// 密钥处理
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "DES");
// Cipher对象的初始化获取Cipher对象,指定了AES算法、CBC模式和PKCS5填充。
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// 初始化
cipher.init(Cipher.DECRYPT_MODE, keySpec);
// 解密
byte[] bytes = cipher.doFinal(Base64.decode(content, Base64.DEFAULT));
return new String(bytes);
}
@Override
public String getMD5(String content) throws Exception {
// 得到摘要实例
MessageDigest md = MessageDigest.getInstance("MD5");
// 摘要运算
byte[] digest = md.digest(content.getBytes());
// 返回16进制字符串
return new BigInteger(1, digest).toString(16);
}
@Override
public String getSHA1(String content) throws Exception {
// 得到摘要实例
MessageDigest md = MessageDigest.getInstance("SHA1");
// 摘要运算
byte[] digest = md.digest(content.getBytes());
// 返回16进制字符串
return new BigInteger(1, digest).toString(16);
// 第二种写法
// md.update(content.getBytes());
// md.update(content2.getBytes());
// md.update(content3.getBytes());
// return new BigInteger(1, md.digest()).toString(16);
}
}
2、hook 加密函数
java代码实现方式:
package com.example.plugintest;
import android.util.Log;
import com.virjar.ratel.api.rposed.IRposedHookLoadPackage;
import com.virjar.ratel.api.rposed.RC_MethodHook;
import com.virjar.ratel.api.rposed.RposedHelpers;
import com.virjar.ratel.api.rposed.callbacks.RC_LoadPackage;
import java.math.BigInteger;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class HookCryptoEntry implements IRposedHookLoadPackage {
private static final String TAG = "plugintest-->";
@Override
public void handleLoadPackage(RC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
System.out.println(TAG + "包名是什么:" + lpparam.packageName);
if (lpparam.packageName.equals("com.yuanrenxue.course6")) {
System.out.println(TAG + "确认hook的app是:" + lpparam.packageName);
// 想hook这个:new SecretKeySpec(key.getBytes(),"AES");这里用的是findAndHookConstructor因为new ... 就是一个
// 实例化的过程,所以需要hook的是构造方法而不是普通方法
RposedHelpers.findAndHookConstructor(SecretKeySpec.class, byte[].class, String.class, new RC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
Log.d(TAG, String.format("SecretKeySpec(%s,%s))", new String((byte[]) param.args[0]), param.args[1]));
}
});
// hook iv:new IvParameterSpec(iv.getBytes());
RposedHelpers.findAndHookConstructor(IvParameterSpec.class, byte[].class, new RC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
Log.d(TAG, String.format("IvParameterSpec(%s)", new String((byte[]) param.args[0])));
}
});
// hook 采用了什么加密算法:Cipher.getInstance("AES/CBC/PKCS5Padding")
RposedHelpers.findAndHookMethod(Cipher.class, "getInstance", String.class, new RC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
Log.d(TAG, "发现使用如下加密算法: " + param.args[0]);
}
});
// hook 加密解密过程:cipher.doFinal(content.getBytes());
RposedHelpers.findAndHookMethod(Cipher.class, "doFinal", byte[].class, new RC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
// 对于加密来说:入参(明文),返回值(密文)
// 对于解密来说:入参(密文),返回值(明文) 可以通过这来判断在加密还是在解密
// 加密的返回值因为是随机的字节,所以打印出来很可能是鬼画符一样的东西�7sZ������O��像这种,是正常的
Log.d(TAG, String.format("入参为:%s,返回值为:%s", new String((byte[]) param.args[0]), new String((byte[]) param.getResult())));
}
});
// hook采用了哪种哈希函数(摘要算法) MessageDigest.getInstance("MD5");
RposedHelpers.findAndHookMethod(MessageDigest.class, "getInstance", String.class, new RC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
Log.d(TAG, "发现使用如下哈希函数: " + param.args[0]);
}
});
// 对于摘要内容的hook
// 1. md.digest(content.getBytes());
RposedHelpers.findAndHookMethod(MessageDigest.class, "digest", byte[].class, new RC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
Log.d(TAG, "消息内容为: " + new String((byte[]) param.args[0]));
Log.d(TAG, "摘要结果为: " + new String((byte[]) param.getResult()));
Log.d(TAG, "摘要结果为: " + new BigInteger(1, (byte[]) param.getResult()).toString(16));
}
});
// 2. md.update(content.getBytes());
RposedHelpers.findAndHookMethod(MessageDigest.class, "update", byte[].class, new RC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
Log.d(TAG, "2.消息内容为: " + new String((byte[]) param.args[0]));
}
});
RposedHelpers.findAndHookMethod(MessageDigest.class, "digest", new RC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
Log.d(TAG, "2.摘要结果为: " + new BigInteger(1, (byte[]) param.getResult()).toString(16));
}
});
}
}
}