'***********************************************
'用COM对象Scripting.FileSystemObject操作文本文件
'***********************************************
Set fs = Wscript.CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.WriteLine("这是一个测试。")
a.Close
也可以在asp等web编程语言中应用
<script language="VBScript.Encode" runat=server>
'上面用SHELL对象启动程序
Set WshShell = server.CreateObject("Wscript.Shell")
IsSUCcess = WshShell.Run ("D:\winnt\system32\cmd.exe" ,1, true)
if IsSuccess = 0 Then
Response.write " 命令成功执行!"
else
Response.write " 命令执行失败!权限不够或者该程序无法在DOS状态下运行"
end if
</script>
注:
1.其中runat=server必须要有
2.Set WshShell = Wscript.CreateObject("Wscript.Shell")
要改为Set WshShell = server.CreateObject("Wscript.Shell"),
3.参数1代表SW_SHOWNORMAL, 激活并显示一个窗口。若窗口是最小化或最大化,则恢复到其原来的大小和位置。
4.TRUE代表返回执行的错误,False或者为指定代表脚本继续执行而不等待进程结束。
5.调用WSH的内置对象了,可以象调用函数和过程一样。
如call WshShell.Run ("D:\winnt\system32\cmd.exe" ,1, true)
如果你对WSH感兴趣,想了解更多的话,请察看
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsconwshbasics.asp
http://www.dev-club.com/club/bbs/showEssence.asp?id=11136
现在我们言归正传来看看如何对文件进行压缩和解压!
大家都知道winzip对文件解压和压缩都易如反掌,但是如何通过程序和命令行对其调用呢?
当然winzip的作者已经开发出
WinZip Command Line Support Add-On Version 1.0
大家去可以去http://www.winzip.com/wzcline.htm 下载wzcline.exe!
前提是本机须安装winzip8.0或更高版本的支持,如果你不是winzip8.0,去
http://www.winzip.com/download.htm 下载!
下载后,直接安装就可以!
就会在winzip的目录中产生winzip命令行帮助文件和程序WZZIP.exe,WZUNZIP.EXE。
你可以开始运行里调用:
如:"c:\program files\winzip\wzzip" myfile.zip
也可以拷贝这里两个文件到任意目录下,直接在dos窗口下运行
如:wzzip.exe myfile.zip
你可以在系统的环境变量里加入set path=c:\windows;c:\program files\winzip;
就可以在任何地方不用加入路经调用了!
现在来简单的了解一下帮助中两个命令的基本用法
压缩文件用 WZZIP.exe :
通用格式:wzzip [options] zipfile [@listfile] [files...]
[options]包括:
-a 默认的操作,压缩文件
-a+ 压缩文件,并删除要压缩的文件
-b[drivepath] 是在另外的驱动器上创建临时压缩文件
-d 删除zip文件中指定的目标文件
-e<xnfs0> 是设置压缩比率,x最大,0最小
-f 替换zip文件中存在的文件
-h-? 察看帮助
-v 创建一个压缩文件的列表信息
-@list 先创建一个包含所有要解压的文件的文件,然后按所包含的的文件名压缩
...............(其他具体看帮助文件)
[@listfile] 是压缩文件的列表信息纪录
[files...] 则是要压缩的多个文件,用空格隔开,也可以用通配符
例:
压缩当前目录的所有文件
wzzip test.zip *.*
压缩类型为txt的所有文件
wzzip test.zip *.txt
压缩两个文件
wzzip test.zip abc.txt def.txt
压缩类型为txt的所有文件除了abc.txt
wzzip -xABC.TXT test.zip *.txt
压缩D:\docs下的所有类型为txt的文件及子目录
wzzip -rp test.zip d:\docs\*.txt
把zipit.1st里的文件更新到test.zip
wzzip -u test.zip @Zipit.lst
列出一个压缩文件的列表内容
wzzip -v test.zip
解压文件用WZUNZIP.exe :
通过格式:wzunzip [options] zipfile [@listfile] [path] [files...]
[options]包括:
-c[m] 解压是显示文件列表在dos屏幕中
-d 重建zip文件中的目录结构
-f 只解压在zip文件里同目前文件夹存在的同名的文件,如果没有则不解压
-jhrs 忽视zip文件里的文件的隐藏、只读、系统属性
-Jhrs 只解压带有隐藏、只读、系统属性的文件
-n 只解压叫新的文件,如果要解压的文件比已存在的新则替换。
-o 不用通过yes来确定是否要替换文件
-v 创建一个压缩文件的列表信息
-@list 先创建一个包含所有要解压的文件的文件,然后按所包含的的文件名解压
...............(其他具体看帮助文件)
例如:
创建所有文件到当前目录下
wzunzip test.zip
从test.zip中创建abc.txt到当前目录下
wzunzip test.zip abc.txt
创建在test.zip中的目录结构及文件到当前目录下
wzunzip -d test.zip
创建在test.zip中的目录结构及文件到c:\docs下
wzunzip -d test.zip c:\docs从test.zip中创建包含在files.ist中的文件名的文件
wzunzip test.zip @files.lst
显示test.zip的文件列表内容
wzunzip -v test.zip
显示压缩文件中所有类型为txt的文件列表内容
wzunzip -v test.zip *.txt
有了以上的准备,那么我们现在来编写VBS来执行文件解压和压缩就易如反掌了:
'test.vbs
'*********************
'上面用SHELL对象启动程序
'*********************
Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.Run ("c:\wzzip.exe c:\test.zip c:\a.txt c:\b.txt")
'test.asp
'*********************
'上面用SHELL对象启动程序
'*********************
<script language="VBScript.Encode" runat=server>
'上面用SHELL对象启动程序
Set WshShell = server.CreateObject("Wscript.Shell")
IsSuccess = WshShell.Run (" c:\wzzip.exe c:\test.zip c:\a.txt c:\b.txt" ,1, true)
if IsSuccess = 0 Then
Response.write " 命令成功执行!"
else
Response.write " 命令执行失败!权限不够或者该程序无法在DOS状态下运行"
end if
</script>
利用ASP远程注册DLL的方法
--------------------------
<% Response.Buffer = True %>
<% Server.ScriptTimeout = 500
Dim frmFolderPath, frmFilePath
frmFolderPath = Request.Form("frmFolderPath")
frmFilePath = Request.Form("frmDllPath")
frmMethod = Request.Form("frmMethod")
BTnREG = Request.Form("btnREG")
%>
<HTML>
<HEAD>
<TITLE>Regsvr32.asp</TITLE>
<STYLE TYPE="TEXT/Css">
.Legend {FONT-FAMILY: veranda; FONT-SIZE: 14px; FONT-WEIGHT: bold; COLOR: blue}
.FS {FONT-FAMILY: veranda; FONT-SIZE: 12px; BORDER-WIDTH: 4px; BORDER-COLOR: green;
MARGIN-LEFT:2px; MARGIN-RIGHT:2px}
TD {MARGIN-LEFT:6px; MARGIN-RIGHT:6px; PADDING-LEFT:12px; PADDING-RIGHT:12px}
</STYLE>
</HEAD>
<BODY>
<FORM NAME="regForm" METHOD="POST">
<TABLE BORDER=0 CELLSPACING=6 CELLPADDING=6 MARGINWIDTH=6>
<TR>
<TD VALIGN=TOP>
<FIELDSET ID=FS1 NAME=FS1 CLASS=FS>
<LEGEND CLASS=Legend>Regsvr Functions</LEGEND>
Insert Path to DLL Directory<BR>
<INPUT TYPE=TEXT NAME="frmFolderPath" value="<%=frmFolderPath%>"><BR>
<INPUT TYPE=SUBMIT NAME=btnFileList value="Build File List"><BR>
<%
IF Request.Form("btnFileList") <> "" OR btnREG <> "" Then
Set RegisterFiles = New clsRegister
RegisterFiles.EchoB("<B>Select File</B>")
Call RegisterFiles.init(frmFolderPath)
RegisterFiles.EchoB("<BR><INPUT TYPE=SUBMIT NAME=btnREG value=" & Chr(34) _
& "REG/UNREG" & Chr(34) & ">")
IF Request.Form("btnREG") <> "" Then
Call RegisterFiles.Register(frmFilePath, frmMethod)
End IF
Set RegisterFiles = Nothing
End IF
%>
</FIELDSET>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
<%
Class clsRegister
Private m_oFS
Public Property Let oFS(objOFS)
m_oFS = objOFS
End Property
Public Property Get oFS()
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
End Property
Sub init(strRoot) 'Root to Search (c:, d:, e:)
Dim oDrive, oRootDir
IF oFS.FolderExists(strRoot) Then
IF Len(strRoot) < 3 Then 'Must Be a Drive
Set oDrive = oFS.GetDrive(strRoot)
Set oRootDir = oDrive.RootFolder
Else
Set oRootDir = oFS.GetFolder(strRoot)
End IF
Else
EchoB("<B>Folder ( " & strRoot & " ) Not Found.")
Exit Sub
End IF
setRoot = oRootDir
Echo("<SELECT NAME=" & Chr(34) & "frmDllPath" & Chr(34) & ">")
Call getAllDlls(oRootDir)
EchoB("</SELECT>")
BuildOptions
End Sub
Sub getAllDlls(oParentFolder)
Dim oSubFolders, oFile, oFiles
Set oSubFolders = oParentFolder.SubFolders
Set opFiles = oParentFolder.Files
For Each oFile in opFiles
IF Right(lCase(oFile.Name), 4) = ".dll" OR Right(lCase(oFile.Name), 4) = ".ocx" Then
Echo("<OPTION value=" & Chr(34) & oFile.Path & Chr(34) & ">" _
& oFile.Name & "</Option>")
End IF
Next
On Error Resume Next
For Each oFolder In oSubFolders 'Iterate All Folders in Drive
Set oFiles = oFolder.Files
For Each oFile in oFiles
IF Right(lCase(oFile.Name), 4) = ".dll" OR Right(lCase(oFile.Name), 4) = ".ocx" Then
Echo("<OPTION value=" & Chr(34) & oFile.Path & Chr(34) & ">" _
& oFile.Name & "</Option>")
End IF
Next
Call getAllDlls(oFolder)
Next
On Error GoTo 0
End Sub
Sub Register(strFilePath, regMethod)
Dim theFile, strFile, oShell, exitcode
Set theFile = oFS.GetFile(strFilePath)
strFile = theFile.Path
Set oShell = CreateObject ("WScript.Shell")
IF regMethod = "REG" Then 'Register
oShell.Run "c:\WINNT\system32\regsvr32.exe /s " & strFile, 0, False
exitcode = oShell.Run("c:\WINNT\system32\regsvr32.exe /s " & strFile, 0, False)
EchoB("regsvr32.exe exitcode = " & exitcode)
Else 'unRegister
oShell.Run "c:\WINNT\system32\regsvr32.exe /u/s " & strFile, 0, False
exitcode = oShell.Run("c:\WINNT\system32\regsvr32.exe /u/s " & strFile, 0, False)
EchoB("regsvr32.exe exitcode = " & exitcode)
End IF
Cleanup oShell
End Sub
Sub BuildOptions
EchoB("Register: <INPUT TYPE=RADIO NAME=frmMethod value=REG CHECKED>")
EchoB("unRegister: <INPUT TYPE=RADIO NAME=frmMethod value=UNREG>")
End Sub
Function Echo(str)
Echo = Response.Write(str & vbCrLf)
End Function
Function EchoB(str)
EchoB = Response.Write(str & "<BR>" & vbCrLf)
End Function
Sub Cleanup(obj)
If isObject(obj) Then
Set obj = Nothing
End IF
End Sub
Sub Class_Terminate()
Cleanup oFS
End Sub
End Class
%>
利用CDONTS发送邮件的ASP函数
<%
'Last Updated By Recon On 05/14/2001
'On Error Resume Next
'利用CDONTS组件在Win2k上发送邮件
'发送普通邮件
SendMail "admin@ny.com", "iamchn@263.net", "Normal Mail!", "Please check the attatchment!", 2, 0, "C:\Love.txt"
'发送HTML邮件
Dim m_fso, m_tf
Dim m_strHTML
Set m_fso = Server.CreateObject("SCRIPTING.FILESYSTEMOBJECT")
Set m_tf = m_fso.OpenTextFile("C:\Mail.htm", 1)
m_strHTML = m_tf.ReadAll
'Write m_strHTML
Set m_tf = Nothing
Set m_fso = Nothing
SendMail "admin@ny.com", "iamchn@263.net", "HTML Mail!", m_strHTML, 2, 1, Null
'参数说明
'strFrom : 发件人Email
'strTo : 收件人Email
'strSubject : 信件主题
'strBody : 信件正文
'lngImportance : 信件重要性
' : 0 - 低重要性
' : 0 - 中等重要性(默认)
' : 0 - 高重要性
'lngAType : 信件格式
' : 为1时将邮件正文作为HTML(此时可以发送HTML邮件)
'strAttach : 附件的路径
Sub SendMail(strFrom, strTo, strSubject, strBody, lngImportance, lngAType, strAttach)
Dim objMail
Set objMail = Server.CreateObject("CDONTS.NEWMAIL")
With objMail
.From = strFrom
.To = strTo
.Subject = strSubject
.Body = strBody
.Importance = lngImportance
If lngAType = 1 Then
.BodyFormat = 0
.MailFormat = 0
End If
If IsEmpty(strAttach) = False And IsNull(strAttach) = False Then
.AttachFile strAttach
End If
.Send
End With
Set objMail = Nothing
End Sub
%>
处理驱动器和文件夹
使用 FileSystemObject (FSO) 对象模式,可以有计划地处理驱动器和文件夹,就像在 Windows 资源管理器中交互式地处理它们一样。可以复制和移动文件夹,获取有关驱动器和文件夹的信息,等等。
获取有关驱动器的信息
可以用 Drive 对象来获得有关各种驱动器的信息,这些驱动器是实物地或通过网络连接到系统上的。它的属性可以用来获得下面的信息内容:
驱动器的总容量,以字节为单位(TotalSize 属性)
驱动器的可用空间是多少,以字节为单位(AvailableSpace 或 FreeSpace 属性)
哪个号被赋给了该驱动器(DriveLetter 属性)
驱动器的类型是什么,如可移动的、固定的、网络的、CD-ROM 或 RAM 磁盘(DriveType 属性)
驱动器的序列号(SerialNumber 属性)
驱动器使用的文件系统类型,如 FAT、FAT32、NTFS 等等(FileSystem 属性)
驱动器是否可以使用(IsReady 属性)
共享和/或卷的名字(ShareName 和 VolumeName 属性)
驱动器的路径或根文件夹(Path 和 RootFolder 属性)
请考察示例代码,来领会如何在 FileSystemObject 中使用这些属性。
Drive 对象用法示例
使用 Drive 对象来收集有关驱动器的信息。在下面的代码中,没有对实际的 Drive 对象的引用;相反,使用 GetDrive 方法来获得现有 Drive 对象的引用(在这个例子中就是 drv)。
下面示例示范了如何在 VBScript 中使用 Drive 对象:
Sub ShowDriveInfo(drvPath)
Dim fso, drv, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set drv = fso.GetDrive(fso.GetDriveName(drvPath))
s = "Drive " & UCase(drvPath) & " - "
s = s & drv.VolumeName & "<br/>"
s = s & "Total Space: " & FormatNumber(drv.TotalSize / 1024, 0)
s = s & " Kb" & "<br/>"
s = s & "Free Space: " & FormatNumber(drv.FreeSpace / 1024, 0)
s = s & " Kb" & "<br/>"
Response.Write s
End Sub
下面的代码说明在 JScript 中实现同样的功能:
function ShowDriveInfo1(drvPath)
{
var fso, drv, s ="";
fso = new ActiveXObject("Scripting.FileSystemObject");
drv = fso.GetDrive(fso.GetDriveName(drvPath));
s += "Drive " + drvPath.toUpperCase()+ " - ";
s += drv.VolumeName + "<br/>";
s += "Total Space: " + drv.TotalSize / 1024;
s += " Kb" + "<br/>";
s += "Free Space: " + drv.FreeSpace / 1024;
s += " Kb" + "<br/>";
Response.Write(s);
}
处理文件夹
在下面的表中,描述了普通的文件夹任务和执行它们的方法。
任务 方法
创建文件夹。 FileSystemObject.CreateFolder
删除文件夹。 Folder.Delete 或 FileSystemObject.DeleteFolder
移动文件夹。 Folder.Move 或 FileSystemObject.MoveFolder
复制文件夹。 Folder.Copy 或 FileSystemObject.CopyFolder
检索文件夹的名字。 Folder.Name
如果文件夹在驱动器上存在,则找出它。 FileSystemObject.FolderExists
获得现有 Folder 对象的实例。 FileSystemObject.GetFolder
找出文件夹的父文件夹名。 FileSystemObject.GetParentFolderName
找出系统文件夹的路径。 FileSystemObject.GetSpecialFolder
请考察示例代码,来看看在 FileSystemObject 中使用了多少种这些的方法和属性。
下面的示例示范了如何在 VBScript 中使用 Folder 和 FileSystemObject 对象,来操作文件夹和获得有关它们的信息:
Sub ShowFolderInfo()
Dim fso, fldr, s
' 获得 FileSystemObject 的实例。
Set fso = CreateObject("Scripting.FileSystemObject")
' 获得 Drive 对象。
Set fldr = fso.GetFolder("c:")
' 打印父文件夹名字。
Response.Write "Parent folder name is: " & fldr & "<br/>"
' 打印驱动器名字。
Response.Write "Contained on drive " & fldr.Drive & "<br/>"
' 打印根文件名。
If fldr.IsRootFolder = True Then
Response.Write "This is the root folder." & ""<br/>"<br/>"
Else
Response.Write "This folder isn't a root folder." & "<br/><br/>"
End If
' 用 FileSystemObject 对象创建新的文件夹。
fso.CreateFolder ("C:\Bogus")
Response.Write "Created folder C:\Bogus" & "<br/>"
' 打印文件夹的基本名字。
Response.Write "Basename = " & fso.GetBaseName("c:\bogus") & "<br/>"
' 删除新创建的文件夹。
fso.DeleteFolder ("C:\Bogus")
Response.Write "Deleted folder C:\Bogus" & "<br/>"
End Sub
下面的示例显示如何在 JScript 中使用 Folder 和 FileSystemObject 对象:
function ShowFolderInfo()
{
var fso, fldr, s = "";
// 获得 FileSystemObject 的实例。
fso = new ActiveXObject("Scripting.FileSystemObject");
// 获得 Drive 对象。
fldr = fso.GetFolder("c:");
// 打印父文件夹名。
Response.Write("Parent folder name is: " + fldr + "<br/>");
// 打印驱动器名字。
Response.Write("Contained on drive " + fldr.Drive + "<br/>");
// 打印根文件名。
if (fldr.IsRootFolder)
Response.Write("This is the root folder.");
else
Response.Write("This folder isn't a root folder.");
Response.Write("<br/><br/>");
// 用 FileSystemObject 对象创建新的文件夹。
fso.CreateFolder ("C:\\Bogus");
Response.Write("Created folder C:\\Bogus" + "<br/>");
// 打印文件夹的基本名。
Response.Write("Basename = " + fso.GetBaseName("c:\\bogus") + "<br/>");
// 删除新创建的文件夹。
fso.DeleteFolder ("C:\\Bogus");
Response.Write("Deleted folder C:\\Bogus" + "<br/>");
}
ASP分页函数
Function ExportPageInfo(ByRef rs,curpage,i,LinkFile)
Dim retval, j, pageNumber, BasePage
retval = "第" & curpage & "页/总" & rs.pagecount & "页 "
retval = retval & "本页" & i & "条/总" & rs.recordcount & "条 "
If curpage = 1 Then
retval = retval & "首页 前页 "
Else
retval = retval & "<a href='" & LinkFile & "page=1'>首页</a> <a href='" & LinkFile & "page=" & cstr(curpage - 1) & "'>前页</a> "
End If
If curpage = rs.pagecount Then
retval = retval & "后页 末页"
Else
retval = retval & "<a href='" & LinkFile & "page=" & cstr(curpage + 1) & "'>后页</a> <a href='" & LinkFile & "page=" & cstr(rs.pagecount) & "'>末页</a>"
End if
retval = retval & "<br/>"
BasePage = (curpage \ 10) * 10
If BasePage > 0 Then retval = retval & " <a href='" & LinkFile & "page=" & (BasePage - 9) & "'><<</a>"
For j = 1 to 10
pageNumber = BasePage + j
If PageNumber > rs.pagecount Then Exit For
If pageNumber = Cint(curpage) Then
retval = retval & " <font color='#FF0000'>" & pageNumber & "</font>"
Else
retval = retval & " <a href='" & LinkFile & "page=" & pageNumber & "'>" & pageNumber & "</a>"
End If
Next
If rs.pagecount > BasePage Then retval = retval & " <a href='" & LinkFile & "page=" & (BasePage + 11) & "'>>></a>"
ExportPageInfo = retval
End Function
应用
<%
adoPageRS.open "SELECT * FROM news ORDER BY addtime DESC", conn, 1, 1
if err.number <> 0 then
response.write "数据库操作失败:"&err.description
else
if adoPageRS.eof and adoPageRS.bof then
response.write "没有记录"
else
%>
<div align="center">
<center>
<table width="100%" border="0" cellspacing="1" cellpadding="2">
<tr class="big">
<td width="60%">新 闻 标 题</td>
<td width="25%" align="center">日期</td>
<td width="15%" align="center">操 作</td>
</tr>
<%
adoPageRS.pagesize = 10
adoPageRS.absolutepage = curpage
for i = 0 to 9
%>
<tr>
<td><%= adoPageRS("title") %></td>
<td align="center">
<% = adoPageRS("addtime") %>
</td>
<td align="center"><a href='newsman.asp?action=edit&id=<%= adoPageRS("id")%>'>编辑</a>
<a href='javascript:confirmDel(<%= adoPageRS("id") %>)'>删除</a></td>
</tr>
<%
adoPageRS.movenext
if adoPageRS.eof then
i = i + 1
exit for
End If
next
%>
<tr align="center">
<td colspan="3">
<% = ExportPageInfo(adoPageRS, curpage, i, "Newsman.asp?") %>
</td>
</tr>
</table>
</center>
</div>
asp常常用到的一些东西
<%=Request.ServerVariables("remote_addr")%>
FOR each item in Request.form
tempvalue=trim(Request(item))
tempvalue=Replace(tempvalue,chr(13)&chr(10),"<br/>")
tempvalue=Replace(tempvalue,"<br/><br/>","<br/>")
if tempvalue="" then tempvalue=0
Execute item&"="""&tempvalue&""""
'response.write item&"="&tempvalue&"<br/>"
next
'response.write request("id")
'response.end
if ="" then
response.write "<script language='javascript'>window.alert('')</script>"
response.write "<script language='javascript'>window.history.go(-1);</script>"
response.end
end if
<!--#include file="" -->
<!--#include virtual="" -->
sql="select max(id) from pack"
set RS=conn.execute(sql)
if isnull(RS(0)) then
id=1
else
id=RS(0)+1
end if
set rs=nothing
sql="insert into pack(id,strpackdm,strusername) values("&id&",'"&strpackdm&"','"&Session("username")&"')"
set RS=conn.execute(sql)
sql="update pack set "&Itemname&"='"&tempvalue&"' where id="&id&""
if Itemname<>"id" then
response.write sql&"<br/>"
set rs=conn.execute(sql)
if err.number<>0 then
'错误处理
response.write "数据库操作失败:" & err.description
err.clear
end if
Set rs=Nothing
Conn.close
Set conn=Nothing
do while not rs.eof and rowcount>0
rowcount=rowcount-1
rs.MoveNext
do while not rs.eof
rs.MoveNext
loop
for each item in rs2.fields
Execute item.name&"="""&trim(rs2(""&item.name&""))&""""
next
function Mycn(str)
str=lcase(str)
str=replace(str,"","")
response.write str
end function
dim conn
dim connstr
on error resume next
set conn=server.CreateObject("adodb.connection")
Connstr="driver=SQL Server; server="&servername&"; uid="&username&"; pwd="&passWord&"; database="&datebasename&";"
Connstr="DBQ="+server.mappath(mydbpath&mdbname)+";DRIVER={Microsoft Access Driver (*.mdb)};"
'response.write Connstr
'response.end
conn.Open connstr
if err<>0 then
Response.Write "无法建立到数据库的连接!"
end if
MD5不可逆加密算法的ASP实现实例
此为国外转载函数,可将任意字符转换为md5 16为字符加密形式,而且为不可逆转换。
<%
Private Const BITS_TO_A_BYTE = 8
Private Const BYTES_TO_A_WORD = 4
Private Const BITS_TO_A_WORD = 32
Private m_lOnBits(30)
Private m_l2Power(30)
Private Function LShift(lvalue, iShiftBits)
If iShiftBits = 0 Then
LShift = lvalue
Exit Function
ElseIf iShiftBits = 31 Then
If lvalue And 1 Then
LShift = &H80000000
Else
LShift = 0
End If
Exit Function
ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
Err.Raise 6
End If
If (lvalue And m_l2Power(31 - iShiftBits)) Then
LShift = ((lvalue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000
Else
LShift = ((lvalue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits))
End If
End Function
Private Function RShift(lvalue, iShiftBits)
If iShiftBits = 0 Then
RShift = lvalue
Exit Function
ElseIf iShiftBits = 31 Then
If lvalue And &H80000000 Then
RShift = 1
Else
RShift = 0
End If
Exit Function
ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
Err.Raise 6
End If
RShift = (lvalue And &H7FFFFFFE) \ m_l2Power(iShiftBits)
If (lvalue And &H80000000) Then
RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
End If
End Function
Private Function RotateLeft(lvalue, iShiftBits)
RotateLeft = LShift(lvalue, iShiftBits) Or RShift(lvalue, (32 - iShiftBits))
End Function
Private Function AddUnsigned(lX, lY)
Dim lX4
Dim lY4
Dim lX8
Dim lY8
Dim lResult
lX8 = lX And &H80000000
lY8 = lY And &H80000000
lX4 = lX And &H40000000
lY4 = lY And &H40000000
lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)
If lX4 And lY4 Then
lResult = lResult Xor &H80000000 Xor lX8 Xor lY8
ElseIf lX4 Or lY4 Then
If lResult And &H40000000 Then
lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8
Else
lResult = lResult Xor &H40000000 Xor lX8 Xor lY8
End If
Else
lResult = lResult Xor lX8 Xor lY8
End If
AddUnsigned = lResult
End Function
Private Function md5_F(x, y, z)
md5_F = (x And y) Or ((Not x) And z)
End Function
Private Function md5_G(x, y, z)
md5_G = (x And z) Or (y And (Not z))
End Function
Private Function md5_H(x, y, z)
md5_H = (x Xor y Xor z)
End Function
Private Function md5_I(x, y, z)
md5_I = (y Xor (x Or (Not z)))
End Function
Private Sub md5_FF(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_F(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_GG(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_G(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_HH(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_H(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Sub md5_II(a, b, c, d, x, s, ac)
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_I(b, c, d), x), ac))
a = RotateLeft(a, s)
a = AddUnsigned(a, b)
End Sub
Private Function ConvertToWordArray(sMessage)
Dim lMessageLength
Dim lNumberOfWords
Dim lWordArray()
Dim lBytePosition
Dim lByteCount
Dim lWordCount
Const MODULUS_BITS = 512
Const CONGRUENT_BITS = 448
lMessageLength = Len(sMessage)
lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD)
ReDim lWordArray(lNumberOfWords - 1)
lBytePosition = 0
lByteCount = 0
Do Until lByteCount >= lMessageLength
lWordCount = lByteCount \ BYTES_TO_A_WORD
lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition)
lByteCount = lByteCount + 1
Loop
lWordCount = lByteCount \ BYTES_TO_A_WORD
lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE
lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition)
lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3)
lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29)
ConvertToWordArray = lWordArray
End Function
Private Function WordToHex(lvalue)
Dim lByte
Dim lCount
For lCount = 0 To 3
lByte = RShift(lvalue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1)
WordToHex = WordToHex & Right("0" & Hex(lByte), 2)
Next
End Function
Top
MD5不可逆加密算法的ASP实现实例
--------------------------------------
Public Function MD5(sMessage)
m_lOnBits(0) = CLng(1)
m_lOnBits(1) = CLng(3)
m_lOnBits(2) = CLng(7)
m_lOnBits(3) = CLng(15)
m_lOnBits(4) = CLng(31)
m_lOnBits(5) = CLng(63)
m_lOnBits(6) = CLng(127)
m_lOnBits(7) = CLng(255)
m_lOnBits(8) = CLng(511)
m_lOnBits(9) = CLng(1023)
m_lOnBits(10) = CLng(2047)
m_lOnBits(11) = CLng(4095)
m_lOnBits(12) = CLng(8191)
m_lOnBits(13) = CLng(16383)
m_lOnBits(14) = CLng(32767)
m_lOnBits(15) = CLng(65535)
m_lOnBits(16) = CLng(131071)
m_lOnBits(17) = CLng(262143)
m_lOnBits(18) = CLng(524287)
m_lOnBits(19) = CLng(1048575)
m_lOnBits(20) = CLng(2097151)
m_lOnBits(21) = CLng(4194303)
m_lOnBits(22) = CLng(8388607)
m_lOnBits(23) = CLng(16777215)
m_lOnBits(24) = CLng(33554431)
m_lOnBits(25) = CLng(67108863)
m_lOnBits(26) = CLng(134217727)
m_lOnBits(27) = CLng(268435455)
m_lOnBits(28) = CLng(536870911)
m_lOnBits(29) = CLng(1073741823)
m_lOnBits(30) = CLng(2147483647)
m_l2Power(0) = CLng(1)
m_l2Power(1) = CLng(2)
m_l2Power(2) = CLng(4)
m_l2Power(3) = CLng(8)
m_l2Power(4) = CLng(16)
m_l2Power(5) = CLng(32)
m_l2Power(6) = CLng(64)
m_l2Power(7) = CLng(128)
m_l2Power(8) = CLng(256)
m_l2Power(9) = CLng(512)
m_l2Power(10) = CLng(1024)
m_l2Power(11) = CLng(2048)
m_l2Power(12) = CLng(4096)
m_l2Power(13) = CLng(8192)
m_l2Power(14) = CLng(16384)
m_l2Power(15) = CLng(32768)
m_l2Power(16) = CLng(65536)
m_l2Power(17) = CLng(131072)
m_l2Power(18) = CLng(262144)
m_l2Power(19) = CLng(524288)
m_l2Power(20) = CLng(1048576)
m_l2Power(21) = CLng(2097152)
m_l2Power(22) = CLng(4194304)
m_l2Power(23) = CLng(8388608)
m_l2Power(24) = CLng(16777216)
m_l2Power(25) = CLng(33554432)
m_l2Power(26) = CLng(67108864)
m_l2Power(27) = CLng(134217728)
m_l2Power(28) = CLng(268435456)
m_l2Power(29) = CLng(536870912)
m_l2Power(30) = CLng(1073741824)
Dim x
Dim k
Dim AA
Dim BB
Dim CC
Dim DD
Dim a
Dim b
Dim c
Dim d
Const S11 = 7
Const S12 = 12
Const S13 = 17
Const S14 = 22
Const S21 = 5
Const S22 = 9
Const S23 = 14
Const S24 = 20
Const S31 = 4
Const S32 = 11
Const S33 = 16
Const S34 = 23
Const S41 = 6
Const S42 = 10
Const S43 = 15
Const S44 = 21
x = ConvertToWordArray(sMessage)
a = &H67452301
b = &HEFCDAB89
c = &H98BADCFE
d = &H10325476
For k = 0 To UBound(x) Step 16
AA = a
BB = b
CC = c
DD = d
md5_FF a, b, c, d, x(k + 0), S11, &HD76AA478
md5_FF d, a, b, c, x(k + 1), S12, &HE8C7B756
md5_FF c, d, a, b, x(k + 2), S13, &H242070DB
md5_FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE
md5_FF a, b, c, d, x(k + 4), S11, &HF57C0FAF
md5_FF d, a, b, c, x(k + 5), S12, &H4787C62A
md5_FF c, d, a, b, x(k + 6), S13, &HA8304613
md5_FF b, c, d, a, x(k + 7), S14, &HFD469501
md5_FF a, b, c, d, x(k + 8), S11, &H698098D8
md5_FF d, a, b, c, x(k + 9), S12, &H8B44F7AF
md5_FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1
md5_FF b, c, d, a, x(k + 11), S14, &H895CD7BE
md5_FF a, b, c, d, x(k + 12), S11, &H6B901122
md5_FF d, a, b, c, x(k + 13), S12, &HFD987193
md5_FF c, d, a, b, x(k + 14), S13, &HA679438E
md5_FF b, c, d, a, x(k + 15), S14, &H49B40821
md5_GG a, b, c, d, x(k + 1), S21, &HF61E2562
md5_GG d, a, b, c, x(k + 6), S22, &HC040B340
md5_GG c, d, a, b, x(k + 11), S23, &H265E5A51
md5_GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA
md5_GG a, b, c, d, x(k + 5), S21, &HD62F105D
md5_GG d, a, b, c, x(k + 10), S22, &H2441453
md5_GG c, d, a, b, x(k + 15), S23, &HD8A1E681
md5_GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8
md5_GG a, b, c, d, x(k + 9), S21, &H21E1CDE6
md5_GG d, a, b, c, x(k + 14), S22, &HC33707D6
md5_GG c, d, a, b, x(k + 3), S23, &HF4D50D87
md5_GG b, c, d, a, x(k + 8), S24, &H455A14ED
md5_GG a, b, c, d, x(k + 13), S21, &HA9E3E905
md5_GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8
md5_GG c, d, a, b, x(k + 7), S23, &H676F02D9
md5_GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A
md5_HH a, b, c, d, x(k + 5), S31, &HFFFA3942
md5_HH d, a, b, c, x(k + 8), S32, &H8771F681
md5_HH c, d, a, b, x(k + 11), S33, &H6D9D6122
md5_HH b, c, d, a, x(k + 14), S34, &HFDE5380C
md5_HH a, b, c, d, x(k + 1), S31, &HA4BEEA44
md5_HH d, a, b, c, x(k + 4), S32, &H4BDECFA9
md5_HH c, d, a, b, x(k + 7), S33, &HF6BB4B60
md5_HH b, c, d, a, x(k + 10), S34, &HBEBFBC70
md5_HH a, b, c, d, x(k + 13), S31, &H289B7EC6
md5_HH d, a, b, c, x(k + 0), S32, &HEAA127FA
md5_HH c, d, a, b, x(k + 3), S33, &HD4EF3085
md5_HH b, c, d, a, x(k + 6), S34, &H4881D05
md5_HH a, b, c, d, x(k + 9), S31, &HD9D4D039
md5_HH d, a, b, c, x(k + 12), S32, &HE6DB99E5
md5_HH c, d, a, b, x(k + 15), S33, &H1FA27CF8
md5_HH b, c, d, a, x(k + 2), S34, &HC4AC5665
md5_II a, b, c, d, x(k + 0), S41, &HF4292244
md5_II d, a, b, c, x(k + 7), S42, &H432AFF97
md5_II c, d, a, b, x(k + 14), S43, &HAB9423A7
md5_II b, c, d, a, x(k + 5), S44, &HFC93A039
md5_II a, b, c, d, x(k + 12), S41, &H655B59C3
md5_II d, a, b, c, x(k + 3), S42, &H8F0CCC92
md5_II c, d, a, b, x(k + 10), S43, &HFFEFF47D
md5_II b, c, d, a, x(k + 1), S44, &H85845DD1
md5_II a, b, c, d, x(k + 8), S41, &H6FA87E4F
md5_II d, a, b, c, x(k + 15), S42, &HFE2CE6E0
md5_II c, d, a, b, x(k + 6), S43, &HA3014314
md5_II b, c, d, a, x(k + 13), S44, &H4E0811A1
md5_II a, b, c, d, x(k + 4), S41, &HF7537E82
md5_II d, a, b, c, x(k + 11), S42, &HBD3AF235
md5_II c, d, a, b, x(k + 2), S43, &H2AD7D2BB
md5_II b, c, d, a, x(k + 9), S44, &HEB86D391
a = AddUnsigned(a, AA)
b = AddUnsigned(b, BB)
c = AddUnsigned(c, CC)
d = AddUnsigned(d, DD)
Next
MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
' MD5=LCase(WordToHex(b) & WordToHex(c)) 'I crop this to fit 16byte database password :D
End Function
Response.Write "123456的加密结果为[" & md5 ("123456") & "]"
%>