风云中的神忍:Excel

来源:百度文库 编辑:九乡新闻网 时间:2024/04/20 09:48:36

Excel—“撤销工作表保护密码”的破解并获取原始密码

在日常工作中,您是否遇到过这样的情况:您用Excel编制的报表、表格、程序等,在单元格中设置了公式、函数等,为了防止其他人修改您的设置或者防止您自己无意中修改,您可能会使用Excel的工作表保护功能,但时间久了保护密码容易忘记,这该怎么办?有时您从网上下载的Excel格式的小程序,您想修改,但是作者加了工作表保护密码,怎么办?您只要按照以下步骤操作,Excel工作表保护密码瞬间即破!
    1、打开您需要破解保护密码的Excel文件;
    2、依次点击菜单栏上的工具---宏----录制新宏,输入宏名字如:aa;
    3、停止录制(这样得到一个空宏);
    4、依次点击菜单栏上的工具---宏----宏,选aa,点编辑按钮;
5、删除窗口中的所有字符(只有几个),替换为下面的内容;

从横线下开始复制
------------------------------------------------------------------------------------------
Option Explicit

Public Sub AllInternalPasswords()
' Breaks worksheet and workbook structure passwords. Bob McCormick
' probably originator of base code algorithm modified for coverage
' of workbook structure / windows passwords and for multiple passwords
'
' Norman Harker and JE McGimpsey 27-Dec-2002 (Version 1.1)
' Modified 2003-Apr-04 by JEM: All msgs to constants, and
' eliminate one Exit Sub (Version 1.1.1)
' Reveals hashed passwords NOT original passwords
Const DBLSPACE As String = vbNewLine & vbNewLine
Const AUTHORS As String = DBLSPACE & vbNewLine & _
"Adapted from Bob McCormick base code by" & _
"Norman Harker and JE McGimpsey"
Const HEADER As String = "AllInternalPasswords User Message"
Const VERSION As String = DBLSPACE & "Version 1.1.1 2003-Apr-04"
Const REPBACK As String = DBLSPACE & "Please report failure " & _
"to the microsoft.public.excel.programming newsgroup."
Const ALLCLEAR As String = DBLSPACE & "The workbook should " & _
"now be free of all password protection, so make sure you:" & _
DBLSPACE & "SAVE IT NOW!" & DBLSPACE & "and also" & _
DBLSPACE & "BACKUP!, BACKUP!!, BACKUP!!!" & _
DBLSPACE & "Also, remember that the password was " & _
"put there for a reason. Don't stuff up crucial formulas " & _
"or data." & DBLSPACE & "Access and use of some data " & _
"may be an offense. If in doubt, don't."
Const MSGNOPWORDS1 As String = "There were no passwords on " & _
"sheets, or workbook structure or windows." & AUTHORS & VERSION
Const MSGNOPWORDS2 As String = "There was no protection to " & _
"workbook structure or windows." & DBLSPACE & _
"Proceeding to unprotect sheets." & AUTHORS & VERSION
Const MSGTAKETIME As String = "After pressing OK button this " & _
"will take some time." & DBLSPACE & "Amount of time " & _
"depends on how many different passwords, the " & _
"passwords, and your computer's specification." & DBLSPACE & _
"Just be patient! Make me a coffee!" & AUTHORS & VERSION
Const MSGPWORDFOUND1 As String = "You had a Worksheet " & _
"Structure or Windows Password set." & DBLSPACE & _
"The password found was: " & DBLSPACE & "$$" & DBLSPACE & _
"Note it down for potential future use in other workbooks by " & _
"the same person who set this password." & DBLSPACE & _
"Now to check and clear other passwords." & AUTHORS & VERSION
Const MSGPWORDFOUND2 As String = "You had a Worksheet " & _
"password set." & DBLSPACE & "The password found was: " & _
DBLSPACE & "$$" & DBLSPACE & "Note it down for potential " & _
"future use in other workbooks by same person who " & _
"set this password." & DBLSPACE & "Now to check and clear " & _
"other passwords." & AUTHORS & VERSION
Const MSGONLYONE As String = "Only structure / windows " & _
"protected with the password that was just found." & _
ALLCLEAR & AUTHORS & VERSION & REPBACK
Dim w1 As Worksheet, w2 As Worksheet
Dim i As Integer, j As Integer, k As Integer, l As Integer
Dim m As Integer, n As Integer, i1 As Integer, i2 As Integer
Dim i3 As Integer, i4 As Integer, i5 As Integer, i6 As Integer
Dim PWord1 As String
Dim ShTag As Boolean, WinTag As Boolean

Application.ScreenUpdating = False
With ActiveWorkbook
WinTag = .ProtectStructure Or .ProtectWindows
End With
ShTag = False
For Each w1 In Worksheets
ShTag = ShTag Or w1.ProtectContents
Next w1
If Not ShTag And Not WinTag Then
MsgBox MSGNOPWORDS1, vbInformation, HEADER
Exit Sub
End If
MsgBox MSGTAKETIME, vbInformation, HEADER
If Not WinTag Then
MsgBox MSGNOPWORDS2, vbInformation, HEADER
Else
On Error Resume Next
Do 'dummy do loop
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
With ActiveWorkbook
.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If .ProtectStructure = False And _
.ProtectWindows = False Then
PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
MsgBox Application.Substitute(MSGPWORDFOUND1, _
"$$", PWord1), vbInformation, HEADER
Exit Do 'Bypass all for...nexts
End If
End With
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
Loop Until True
On Error GoTo 0
End If
If WinTag And Not ShTag Then
MsgBox MSGONLYONE, vbInformation, HEADER
Exit Sub
End If
On Error Resume Next
For Each w1 In Worksheets
'Attempt clearance with PWord1
w1.Unprotect PWord1
Next w1
On Error GoTo 0
ShTag = False
For Each w1 In Worksheets
'Checks for all clear ShTag triggered to 1 if not.
ShTag = ShTag Or w1.ProtectContents
Next w1
If ShTag Then
For Each w1 In Worksheets
With w1
If .ProtectContents Then
On Error Resume Next
Do 'Dummy do loop
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If Not .ProtectContents Then
PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
MsgBox Application.Substitute(MSGPWORDFOUND2, _
"$$", PWord1), vbInformation, HEADER
'leverage finding Pword by trying on other sheets
For Each w2 In Worksheets
w2.Unprotect PWord1
Next w2
Exit Do 'Bypass all for...nexts
End If
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next
Loop Until True
On Error GoTo 0
End If
End With
Next w1
End If
MsgBox ALLCLEAR & AUTHORS & VERSION & REPBACK, vbInformation, HEADER
End Sub
-----------------------------------------------------------------------------------------
复制到横线以上
    6、关闭编辑窗口;
    7、依次点击菜单栏上的工具---宏-----宏,选AllInternalPasswords,运行,确定两次;
    等一会,就会出现以下对话框:这就是Excel密码对应的原始密码(此密码和原先设置的密码都能打开此文档。如果是别人的文档,你又想恢复密码设置,就可以用此密码进行保护,他就能用他设置的密码打开,你可以试试,很有趣的。字母一定要大写):





再点击确定。Excel的原始密码就被清除了!!   哈哈...这是个老外做,在网上有久了.
以下是我做的汉化和注释,同大家分享一下:

用宏制作的解除工作表密码保护(转)

此为在网上找到的一个用宏来解除工作表密码保护的程序,是老外做的,自己加入了注释,消息框也用汉字显示了.



Public Sub AllInternalPasswords()
' Breaks worksheet and workbook structure passwords. Bob McCormick          破解Excel工作表和工作薄保护密码.
' probably originator of base code algorithm modified for coverage          Bob McCormick 基于覆盖工作薄窗口/所有工作表内容密码保护而制作
' of workbook structure / windows passwords and for multiple passwords
'
' Norman Harker and JE McGimpsey 27-Dec-2002 (Version 1.1)                  Norman Harker 与 JE McGimpsey 27-Dec-2002 (版本 1.1)
' Modified 2003-Apr-04 by JEM: All msgs to constants, and                   JEM 2003-Apr-04 改进:全部消息常数,和除去一个退出过程 (版本 1.1.1)
' eliminate one Exit Sub (Version 1.1.1)
' Reveals hashed passwords NOT original passwords                           程序展示的密码不是原始密码

Const DBLSPACE As String = vbNewLine & vbNewLine         '声明常量_字符型_vbNewLine=Chr(13)+Chr(10)指定的新行字符

'Const AUTHORS As String = DBLSPACE & vbNewLine & "Adapted from Bob McCormick base code by" & "Norman Harker and JE McGimpsey"         '声明常量_字符型
Const AUTHORS As String = DBLSPACE & vbNewLine & "制作程序: Bob McCormick 改进:Norman Harker and JE McGimpsey"         '声明常量_字符型

'Const HEADER As String = "AllInternalPasswords User Message"         '声明常量_字符型
Const HEADER As String = "密码使用消息框"         '声明常量_字符型

'Const VERSION As String = DBLSPACE & "Version 1.1.1 2003-Apr-04"         '声明常量_字符型
Const VERSION As String = DBLSPACE & "版本 1.1.1 2003-Apr-04"         '声明常量_字符型

'Const REPBACK As String = DBLSPACE & "Please report failure " & "to the microsoft.public.excel.programming newsgroup."        '声明常量_字符型
Const REPBACK As String = DBLSPACE & "请报告失败到 microsoft.public.excel.programming 工作组"        '声明常量_字符型

'Const ALLCLEAR As String = DBLSPACE & "The workbook should " & "now be free of all password protection, so make sure you:" & _
'   DBLSPACE & "SAVE IT NOW!" & DBLSPACE & "and also" & DBLSPACE & "BACKUP!, BACKUP!!, BACKUP!!!" & DBLSPACE & "Also, remember that the password was " & _
'   "put there for a reason. Don't stuff up crucial formulas " & "or data." & DBLSPACE & "Access and use of some data " & _
'   "may be an offense. If in doubt, don't."            '声明常量_字符型
Const ALLCLEAR As String = DBLSPACE & "工作薄现在将释放所有密码保护, 因而请你确认:" & _
   DBLSPACE & "现在释放!" & DBLSPACE & "和" & DBLSPACE & "备份!, 备份!!, 备份!!!" & DBLSPACE & "此外,请记住,密码是放在那里是有原因的.不要填充重要的公式和数据." _
   & DBLSPACE & "获取和使用的一些数据可能是一种犯罪行为. 如有疑问,不知道."            '声明常量_字符型

'Const MSGNOPWORDS1 As String = "There were no passwords on " & "sheets, or workbook structure or windows." & AUTHORS & VERSION            '声明常量_字符型
Const MSGNOPWORDS1 As String = "在工作薄/窗口/工作表上没有密码." & AUTHORS & VERSION            '声明常量_字符型

'Const MSGNOPWORDS2 As String = "There was no protection to " & "workbook structure or windows." & DBLSPACE & "Proceeding to unprotect sheets." & AUTHORS & VERSION              '声明常量_字符型
Const MSGNOPWORDS2 As String = "在工作薄/窗口/工作表上没有保护." & DBLSPACE & "着手解除表." & AUTHORS & VERSION              '声明常量_字符型

'Const MSGTAKETIME As String = "After pressing OK button this " & "will take some time." & DBLSPACE & "Amount of time " & _
'   "depends on how many different passwords, the " & "passwords, and your computer's specification." & DBLSPACE & _
'   "Just be patient! Make me a coffee!" & AUTHORS & VERSION         '声明常量_字符型
Const MSGTAKETIME As String = "在按下 OK 按钮后会等待少许时间." & DBLSPACE & "总时间依赖于密码的多少." & _
   "和你电脑的速度." & DBLSPACE & "只要有耐心!让我喝咖啡!" & AUTHORS & VERSION            '声明常量_字符型

'Const MSGPWORDFOUND1 As String = "You had a Worksheet " & "Structure or Windows Password set." & DBLSPACE & _
'   "The password found was: " & DBLSPACE & "$$" & DBLSPACE & "Note it down for potential future use in other workbooks by " & _
'   "the same person who set this password." & DBLSPACE & "Now to check and clear other passwords." & AUTHORS & VERSION            '声明常量_字符型
Const MSGPWORDFOUND1 As String = "你有工作表内容/窗口密码设置." & DBLSPACE & _
   "密码发现是: " & DBLSPACE & "$$" & DBLSPACE & "注意 它未来可能的其他工作簿中使用同一个人设置的密码." & _
    DBLSPACE & "现在检查和清除从前的密码." & AUTHORS & VERSION            '声明常量_字符型
   
'Const MSGPWORDFOUND2 As String = "You had a Worksheet " & "password set." & DBLSPACE & "The password found was: " & _
'   DBLSPACE & "$$" & DBLSPACE & "Note it down for potential " & "future use in other workbooks by same person who " & _
'   "set this password." & DBLSPACE & "Now to check and clear " & "other passwords." & AUTHORS & VERSION               '声明常量_字符型
Const MSGPWORDFOUND2 As String = "您有一个工作表密码设置." & DBLSPACE & "密码发现是: " & _
   DBLSPACE & "$$" & DBLSPACE & "注意 它未来可能的其他工作簿中使用同一个人设置的密码." & _
   DBLSPACE & "现在检查和清除从前的密码." & AUTHORS & VERSION               '声明常量_字符型

'Const MSGONLYONE As String = "Only structure / windows " & "protected with the password that was just found." & _
'   ALLCLEAR & AUTHORS & VERSION & REPBACK            '声明常量_字符型
Const MSGONLYONE As String = "只有结构/窗口发现有密码保护." & _
   ALLCLEAR & AUTHORS & VERSION & REPBACK            '声明常量_字符型
   
Dim w1 As Worksheet, w2 As Worksheet        '声明变量_对象型
Dim i As Integer, j As Integer, k As Integer, l As Integer        '声明变量_整数型
Dim m As Integer, n As Integer, i1 As Integer, i2 As Integer
Dim i3 As Integer, i4 As Integer, i5 As Integer, i6 As Integer
Dim PWord1 As String        '声明变量_字符型
Dim ShTag As Boolean, WinTag As Boolean        '声明变量_逻辑型

Application.ScreenUpdating = False     '屏幕更新_关闭
With ActiveWorkbook
   WinTag = .ProtectStructure Or .ProtectWindows        '进行逻辑或运算
   '逻辑变量赋值.ProtectStructure工作簿中工作表结构受保护,则该属性值为 True
   '逻辑变量赋值.ProtectWindows工作簿窗口受保护,则该属性值为 True
End With
ShTag = False       '逻辑变量赋值

For Each w1 In Worksheets
   ShTag = ShTag Or w1.ProtectContents        '进行逻辑或运算
   '逻辑变量赋值.ProtectContents工作表内容受保护,则该属性值为 True
Next w1

If Not ShTag And Not WinTag Then      '如果工作表内容与工作薄不受保护为真
   MsgBox MSGNOPWORDS1, vbInformation, HEADER
   Exit Sub       '退出本过程
End If

MsgBox MSGTAKETIME, vbInformation, HEADER

If Not WinTag Then       '如果工作簿不受保护为真
   MsgBox MSGNOPWORDS2, vbInformation, HEADER
Else
   On Error Resume Next     '发生错误时,转到紧接着发生错误语句之后的语句继续执行
   Do
   For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
   For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
   For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
   For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
   With ActiveWorkbook
      .Unprotect Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
      '取消工作簿的保护 语法 .Unprotect(Password)
      If .ProtectStructure = False And .ProtectWindows = False Then      '如果工作薄中工作表结构与窗口不受保护为真
         PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
         'PWord1= 拼写密码
         MsgBox Application.Substitute(MSGPWORDFOUND1, "$$", PWord1), vbInformation, HEADER
         '工作表函数Substitute(要替换字符的文本,要替换的字符,新字符)
         'vbInformation msgbox常数:显示 Information Message 图标
         'HEADER 在msgbox标题栏中显示的字符串
         Exit Do     '退出循环
      End If
   End With
   Next: Next: Next: Next: Next: Next
   Next: Next: Next: Next: Next: Next
   Loop Until True
   On Error GoTo 0     '停止在当前过程中处理错误
End If

If WinTag And Not ShTag Then      '如果工作薄受保护与工作表内容不受保护为真
   MsgBox MSGONLYONE, vbInformation, HEADER
   Exit Sub       '退出本过程
End If

On Error Resume Next     '发生错误时,转到紧接着发生错误语句之后的语句继续执行

For Each w1 In Worksheets
   w1.Unprotect PWord1     '取消工作表的保护 语法 .Unprotect(Password)
Next w1

On Error GoTo 0     '停止在当前过程中处理错误
ShTag = False

For Each w1 In Worksheets
   ShTag = ShTag Or w1.ProtectContents        '进行逻辑或运算
   '逻辑变量赋值.ProtectContents工作表内容受保护,则该属性值为 True
Next w1

If ShTag Then      '如果工作表内容受保护为真
   For Each w1 In Worksheets       '在每个工作表中循环
      With w1
         If .ProtectContents Then      '如果工作表内容受保护为真
            On Error Resume Next     '发生错误时,转到紧接着发生错误语句之后的语句继续执行
            Do
            For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
            For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
            For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
            For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
            .Unprotect Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
            '取消工作表的保护 语法 .Unprotect(Password)
            If Not .ProtectContents Then     '如果工作表内容不受保护为真
               PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
               'PWord1= 拼写密码
               MsgBox Application.Substitute(MSGPWORDFOUND2, "$$", PWord1), vbInformation, HEADER
               '工作表函数Substitute(要替换字符的文本,要替换的字符,新字符)
               'vbInformation msgbox常数:显示 Information Message 图标
               'HEADER 在msgbox标题栏中显示的字符串
               For Each w2 In Worksheets       '在每个工作表中循环
                  w2.Unprotect PWord1      '取消工作表的保护 语法 .Unprotect(Password)
               Next w2
               Exit Do     '退出循环
            End If
            Next: Next: Next: Next: Next: Next
            Next: Next: Next: Next: Next: Next
            Loop Until True
            On Error GoTo 0     '停止在当前过程中处理错误
         End If
      End With
   Next w1
End If
MsgBox ALLCLEAR & AUTHORS & VERSION & REPBACK, vbInformation, HEADER
End Sub