allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.huangxiaoguo1:hxgsecurity:1.0.0'
}
MD5Utils.md5(String string);
MD5Utils.md5(File file);
MD5Utils.md5Nio(File file);
MD5Utils.md5(String string, int times);
MD5Utils.md5(String string, String slat);
Base64Utils.encodeString(String str);
Base64Utils.decodeString(String encodedString);
Base64Utils.encodeFile(String path);
Base64Utils.decodeFile(String encodedPath);
AESUtils.setJavaKey(String skey);
AESUtils.setJavaIvParameter(String ivParameter);
设置key必须为16位,可更改为自己的key,例如:
AESUtils.setJavaIvParameter("huangxiaoguo1234");
AESUtils.setJavaKey("5682huangxiaoguo");
方式为进行NDK开发打包成.so文件,放进你当前类对应的module的libs中,
并进行build.gradle配置
android {
...
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
jni对应的包名为cn.tsou.lib_security.aes
jni对应的获取key的方法名为getKey()
jni对应的获取IvParameter的方法名为getIvParameter()
如下JNI简例:
#include
//JNIEXPORT jstring JNICALL
jstring Java_cn_tsou_lib_1security_aes_AEScbc_getKey(JNIEnv
*env, jobject instance) {
return (*env)->NewStringUTF(env, "huangxiaoguo1234");
}
jstring Java_cn_tsou_lib_1security_aes_AEScbc_getIvParameter(JNIEnv
*env, jobject instance) {
return (*env)->NewStringUTF(env, "1234huangxiaoguo");
}
检查你设置的key和IvParameter是否生效的Log日志筛选tag条件为:huangxiaoguo
AESUtils.getInstance().encrypt(String plaintext);
AESUtils.getInstance().decrypt(String ciphertext_base64);
创建一个公共和私人密钥,并将其存储使用Android密钥存储库中,因此,只有
这个应用程序将能够访问键。
最好放在启动页初始化需要一些时间
private RSAPublicKey publicKey = RSAinit.initRSA(this, null, null, 0, null);
initRSA参数介绍
initRSA(Context context,String pcksPadding,String split,int keySize,String alias);
context:上下文
pcksPadding: 加密填充方式 传null,默认:"RSA/ECB/PKCS1Padding"
split: 当要加密的内容超过bufferSize,则采用partSplit进行分块加密,
传null,默认:"#HUANGXIAOGUO#"
keySize: 秘钥默认长度,传0,默认:2048
alias: 自己给你的别名,方便在keystore中查找秘钥,传null,默认:"xiaoGuoKey"
String localPublicKey = EncodeUtils.base64Encode2String(publicKey.getEncoded());
AndroidKeyStoreRSAUtils.encryptByPublicKey(byte[] data, byte[] publicKey);
简例:
byte[] encryptBytes = AndroidKeyStoreRSAUtils.encryptByPublicKey(encryptionRSAString.getBytes(),
publicKey.getEncoded());
String encryStr = Base64Encoder.encode(encryptBytes);
AndroidKeyStoreRSAUtils.encryptByPrivateKey(byte[] data, byte[] privateKey);
AndroidKeyStoreRSAUtils.decryptByPublicKey(byte[] data, byte[] publicKey);
AndroidKeyStoreRSAUtils.decryptByPrivateKey(byte[] encrypted);
简例:
byte[] decryptBytes = AndroidKeyStoreRSAUtils.decryptByPrivateKey(Base64Decoder.decodeToBytes(decodeRSAString));
mTvRsaDecode.setText(new String(decryptBytes));
AndroidKeyStoreRSAUtils.encryptByPublicKeyForSpilt(byte[] data, byte[] publicKey);
AndroidKeyStoreRSAUtils.encryptByPrivateKeyForSpilt(byte[] data, byte[] privateKey);
AndroidKeyStoreRSAUtils.decryptByPublicKeyForSpilt(byte[] encrypted, byte[] publicKey);
AndroidKeyStoreRSAUtils.decryptByPrivateKeyForSpilt(byte[] encrypted);
AndroidKeyStoreRSAUtils.getPrivateKey(String privateKeyData);
AndroidKeyStoreRSAUtils.getPublicKey(String publicKeyData);
AndroidKeyStoreRSAUtils.isHaveKeyStore();
AndroidKeyStoreRSAUtils.getLocalPublicKey();
使用之前请先初始化RSAinit
###### 签名
AndroidKeyStoreRSAUtils.signData(String inputStr);
###### 校验签名的字符串
boolean b = AndroidKeyStoreRSAUtils. verifyData(String input, String signatureStr);
if (b) {
mTvRsaVerify.setText("签名一致");
} else {
mTvRsaVerify.setText("签名不一致");
}
使用之前请先初始化RSAinit
SPSecuredUtils.put(Context context, String key, Object object, RSAPublicKey publicKey);
简例:
SPSecuredUtils.put(this, "huangxiaoguo", encryptionSpString, publicKey);
SPSecuredUtils.put(this, "huangxiaoguo1", 1, publicKey);
SPSecuredUtils.put(this, "huangxiaoguo2", 0.01, publicKey);
SPSecuredUtils.put(this, "huangxiaoguo3", true, publicKey);
SPSecuredUtils.get(Context context, String key, Object defaultObject);
String huangxiaoguo = (String) SPSecuredUtils.get(this, "huangxiaoguo", "");
int huangxiaoguo1 = (int) SPSecuredUtils.get(this, "huangxiaoguo1", 0);
double huangxiaoguo2 = (double) SPSecuredUtils.get(this, "huangxiaoguo2", 0.0);
boolean huangxiaoguo3 = (boolean) SPSecuredUtils.get(this, "huangxiaoguo3", false);
SPSecuredUtils.saveDeviceData(Context context, String key, T device, RSAPublicKey publicKey);
SPSecuredUtils.getDeviceData(Context context, String key)
SPSecuredUtils.remove(Context context, String key);
SPSecuredUtils.clear(Context context);
SPSecuredUtils.contains(Context context, String key);
SPSecuredUtils.getAll(Context context);
github地址:https://github.com/huangxiaoguo1/hxgsecurity