⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 17-4 文件加密、解密.hta

📁 JAVASCRIPT完全自学手册,中源码的验证修订实例
💻 HTA
字号:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=GB2312" />
<title>17-4  文件加密、解密</title>
<style>
* { font-size:12px; font-family:宋体, Arial; } /*规定了所有的字体样式*/
body { overflow:auto; }
a { color:blue; }
</style>
<script>
var fso, fin, fout, fin_path, fout_path;
//函数“$”根据指定字符串获取相应ID的对象
function $(str){ return(document.getElementById(str)); }
//窗体载入完毕时初始化
window.onload = function(){
    //创建FSO控件
    fso = new ActiveXObject("Scripting.FileSystemObject");   
}
//当窗口卸载时确保文本流关闭
window.onunload = function(){
    try{ fin.Close(); }catch(e){}
    try{ fout.Close(); }catch(e){}
}
//获取输入的文本流
function getFile(){
    //获取用户选择的文件地址
    var f = selectFile();
    //如果用户没有选择文件,则返回
    if(!f)return;
    //试图关闭已打开的文本流
    try{ fin.Close(); }catch(e){}
    //记录输入文件的路径
    fin_path = f;
    //输出源文件信息
    $("l1").innerHTML = "源文件:" + fin_path;
    $("l2").innerHTML = "需要处理的文件大小:" + fso.GetFile(f).size;
    //打开文本流
    fin = fso.OpenTextFile(fin_path);
}
//根据给定的扩展名生成新文件名
function getNewFile(ext){
    var f, f2, i, name;
    f2 = fso.GetParentFolderName(fin_path);
    name = fso.GetFileName(fin_path);
    //如果新文件存在
    if(fso.FileExists(fso.BuildPath(f2, name+"."+ext))){
        i=0;
        //在文件后缀名前加上数字,避免名称重复
        while(fso.FileExists(fso.BuildPath(f2, name+"."+i+ext)))i++;
        fout_path = fso.BuildPath(f2, name+"."+i+ext);
    }else{
        //直接获得输出文件的文件名
        fout_path = fso.BuildPath(f2, name+"."+ext);
    }
    $("l5").innerHTML = "输出文件的路径:" + fout_path;
    //试图关闭可能存在的输出文本流
    try{ fout.Close(); }catch(e){}
    //打开同时新建文本流
    fout = fso.OpenTextFile(fout_path, 2, true);
    return;
}
//加密函数核心
function core_encode(str, pwd, line){
    var i, re = [], len, len_p;
    i = line; len = str.length; len_p = pwd.length;
    //对一行内的每个字符,根据给定的密码和行号执行异或操作
    for(var i=0; i< len; i++){
        //对异或后得到的字符执行escape编码
        re.push(escape(String.fromCharCode(str.charCodeAt(i)^pwd.charCodeAt((i+line)%len_p))));
    }
    //返回加密结果
    return(re.join(""));
}
//解密函数核心
function core_decode(str, pwd, line){
    var i, re = [], len, len_p;
    //首先对输入的字符串unescape反编码
    str = unescape(str);
    i = line; len = str.length; len_p = pwd.length;
    //对一行内的每个字符,根据给定的密码和行号执行异或操作
    for(var i=0; i< len; i++){
        re.push(String.fromCharCode(str.charCodeAt(i)^pwd.charCodeAt((i+line)%len_p)));
    }
    return(re.join(""));
}
//加密函数
function encode(){
    var strLine, line, pwd;
    //打开输出文本流
    getNewFile("hen");
    //获取密码
    pwd = $("pass").value;
    $("l4").innerHTML = "操作:加密";
    //循环依次读入输入流中的每行,分别加密
    while(!fin.AtEndOfStream){
        line = fin.Line;
        strLine = fin.ReadLine();
        fout.WriteLine(core_encode(strLine, pwd, line));
    }
    //关闭输出
    try{ fin.Close(); }catch(e){}
    try{ fout.Close(); }catch(e){}
    $("l6").innerHTML = "加密成功,加密后文件大小:"+fso.GetFile(fout_path).size;
}

function decode(){
    var strLine, line;
    //打开输出文本流
    getNewFile("hde");
    //获取密码
    pwd = $("pass").value;
    $("l4").innerHTML = "操作:解密";
    while(!fin.AtEndOfStream){
        line = fin.Line;
        strLine = fin.ReadLine();
        fout.WriteLine(core_decode(strLine, pwd, line));
    }
    //循环依次读入输入流中的每行,分别加密
    try{ fin.Close(); }catch(e){}
    try{ fout.Close(); }catch(e){}
    $("l6").innerHTML = "解密成功,加密后文件大小:"+fso.GetFile(fout_path).size;
}

//弹出一个选择文件对话框
function selectFile(){
    var obj, strFilePath;
    //创建一个新的文件选择输入框
    obj = document.createElement("input");
    obj.type = "file"; obj.style.display = "none";
    document.body.appendChild(obj);
    //模拟其鼠标单击事件,以弹出文件选择对话框
    obj.click();
    strFilePath = obj.value;
    //移除文件选择输入框
    document.body.removeChild(obj);
    return(strFilePath);
}

</script>
</head>
<body>
<fieldset>
    <legend>文件加密/解密</legend>
    <ol>
        <li id="l1">请选择需要加密或解密的文件:<a href="#" onclick="getFile();">选择</a></li>
        <li id="l2">需要处理的文件大小</li>
        <li id="l3">设置加密/解密的密码:<input type="password" id="pass" /></li>
        <li id="l4">点击执行
            <a href="#" onclick="encode();">加密</a>
            /<a href="#" onclick="decode();">解密</a>操作
        </li>
        <li id="l5">输出文件的路径</li>
        <li id="l6">执行结果</li>
    </ol>
</fieldset>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -