Auto.Js调用安卓实现AES-CBC加解密

admin
2021-08-30 / 1 评论 / 908 阅读 / 正在检测是否收录...

理论支持任意版本Auto.Js:

importClass("java.security.SecureRandom");
importClass("javax.crypto.spec.DESKeySpec");
importClass("javax.crypto.SecretKeyFactory");
importClass("javax.crypto.Cipher");
importClass("java.security.NoSuchAlgorithmException");
importClass("javax.crypto.KeyGenerator");
importClass("javax.crypto.SecretKey");
importClass("javax.crypto.spec.SecretKeySpec");
importClass("javax.crypto.KeyGenerator");
importClass("javax.crypto.spec.IvParameterSpec");
/*
*来源: 苏酥博客(blog.ssyyds.top)
*作者:苏酥 (HuaYin)
*QQ: 499933521
*/
var config = {
    VIPARA: "1255555555555555", // Iv
    bm: "UTF-8", // 编码
    dataPassword: '9955555555555555', //key
    cleartext: 'hello world' //加密原文
}
for (var k in config) {
    var v = config[k]
    config[k] = new java.lang.String(v)
}
var dataPassword = config.dataPassword
var cleartext = config.cleartext
var encrypted = encrypt(dataPassword, cleartext)
log('加密结果=')
log(encrypted)
var decrypted = decrypt(dataPassword, encrypted)
log('解密结果=')
log(decrypted)

function encrypt(dataPassword, cleartext) {
    var VIPARA = config.VIPARA
    var bm = config.bm
    var zeroIv = new IvParameterSpec(config.VIPARA.getBytes());
    var key = new SecretKeySpec(dataPassword.getBytes(), "AES");
    var cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
    var encryptedData = cipher.doFinal(cleartext.getBytes(bm)); //    byte[]
    var result = base64Encode(encryptedData);
    result = new java.lang.String(result)
    return result
}
/**
 *
 * @param {String} dataPassword
 * @param {String} encrypted
 * @return {String}
 */
function decrypt(dataPassword, encrypted) {
    var VIPARA = config.VIPARA
    var bm = config.bm
    var byteMi = base64Decode(encrypted); // byte[]
    var zeroIv = new IvParameterSpec(config.VIPARA.getBytes());
    var key = new SecretKeySpec(dataPassword.getBytes(), "AES");
    var cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
    var decryptedData = cipher.doFinal(byteMi); //byte[]
    return new java.lang.String(decryptedData, bm);
}

function base64Encode(r) {
    var r = android.util.Base64.encodeToString(r, 0);
    return r
}

function base64Decode(r) {
    var r = android.util.Base64.decode(r, 0)
    return r
}

当然,本博主还提供了另一种简便的方法,相对于上面方法来说,实现代码更少(推荐):

/*
*来源: 苏酥博客(blog.ssyyds.top)
*作者:苏酥 (HuaYin)
*QQ: 499933521
*/
var key = "1255555555555555"; //key
var iv = "1255555555555555"; //iv
var cleartext = "c图库s" //加密数据
var encrypted = encrypt(key, iv, cleartext)
log('加密结果=')
log(encrypted)
var decrypted = decrypt(key, iv, encrypted)
log('解密结果=')
log(decrypted)

function encrypt(key, iv, str) {
    var key1 = java.lang.String(key).getBytes("utf-8")
    var iv = java.lang.String(iv).getBytes("utf-8")
    var wod = java.lang.String(str).getBytes("utf-8")
    var key = javax.crypto.spec.SecretKeySpec(key1, "AES");
    var cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS7Padding");
    cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key, javax.crypto.spec.IvParameterSpec(iv));
    opq = cipher.doFinal(wod);
    var result = java.lang.String(android.util.Base64.encode(opq, 0))
    return result
}

function decrypt(key, iv, str) {
    var key1 = java.lang.String(key).getBytes("utf-8")
    var iv = java.lang.String(iv).getBytes("utf-8")
    var wod = text = android.util.Base64.decode(str, 0);
    var key = javax.crypto.spec.SecretKeySpec(key1, "AES");
    var cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, javax.crypto.spec.IvParameterSpec(iv));
    var cg = cipher.doFinal(wod);
    var result = java.lang.String(cg, "utf-8")
    return result;
}
2

评论 (1)

取消
  1. 头像
    哈哈
    iPhone · QQ Browser

    画图

    回复