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

admin
2021-09-01 / 1 评论 / 309 阅读 / 正在检测是否收录...

具体实现代码(理论支持全版本):

importClass("javax.crypto.spec.IvParameterSpec");
importClass("javax.crypto.spec.DESKeySpec");
importClass("javax.crypto.SecretKeyFactory");
importClass("javax.crypto.Cipher");
/*
*来源: 苏酥博客(blog.ssyyds.top)
*作者:苏酥 (HuaYin)
*QQ: 499933521
*/
var str = "ghhhh";
var key = "1255555555555555"; //十六位key
var iv = "12555555"; //8位iv
var DesResult = encryptDES(key, iv, str)
log("Des加密结果: " + DesResult)
log("Des解密结果: " + decryptDES(key, iv, DesResult))

function encryptDES(key, iv, str) {
    var key1 = java.lang.String(key).getBytes("utf-8")
    var iv = java.lang.String(iv).getBytes("utf-8")
    var iv = new IvParameterSpec(iv);
    var wod = java.lang.String(str).getBytes("utf-8")
    var desKey = new DESKeySpec(key1);
    var keyFactory = SecretKeyFactory.getInstance("DES");
    var secretKey = keyFactory.generateSecret(desKey);
    var cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
    var opq = cipher.doFinal(wod);
    var result = java.lang.String(android.util.Base64.encode(opq, 0))
    return result
}

function decryptDES(key, iv, str) {
    var key1 = java.lang.String(key).getBytes("utf-8")
    var iv = java.lang.String(iv).getBytes("utf-8")
    var iv = new IvParameterSpec(iv);
    var wod = android.util.Base64.decode(str, 0);
    var desKey = new DESKeySpec(key1);
    var keyFactory = SecretKeyFactory.getInstance("DES");
    var secretKey = keyFactory.generateSecret(desKey);
    var cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    var cg = cipher.doFinal(wod);
    var result = java.lang.String(cg, "utf-8")
    return result;
}
3

评论 (1)

取消
  1. 头像
    1082
    Android Pie · Google Chrome

    大佬Auto.Js调用安卓实现DES/ECB/PKCS5Padding加解密能出个吗

    回复