为什么此代码会禁用 sheet 保护?

Why does this code disable the sheet protection?

以下代码由 this website 提供,我用它来取消保护我不知道密码的工作表。

Sub DisableSheetProtection()
Dim password As String
On Error Resume Next
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 n = 65 To 66
For o = 65 To 66: For p = 65 To 66
For q = 65 To 66: For r = 65 To 66
For S = 65 To 66: For t = 32 To 126
ActiveSheet.Unprotect Chr(i) & _
Chr(j) & Chr(k) & Chr(l) & _
Chr(m) & Chr(n) & Chr(o) & _
Chr(p) & Chr(q) & Chr(r) & _
Chr(S) & Chr(t)

Next t: Next S: Next r: Next q
Next p: Next o: Next n: Next m
Next l: Next k: Next j: Next i
MsgBox "Sheet protection disabled."
End Sub

问题是:它是如何或为什么起作用的?Chr(65) 是 A,Chr(66) 是 B,Chr(32) 到 Chr(126) 是整个字母表和特殊字符。

基本上,如果您想尝试所有可能性,您需要这样做:

Sub DisableSheetProtection()
    Dim password As String
    On Error Resume Next
    For i = 32 To 126: For j = 32 To 126
    For k = 32 To 126: For l = 32 To 126
    For m = 32 To 126: For n = 32 To 126
    For o = 32 To 126: For p = 32 To 126
    For q = 32 To 126: For r = 32 To 126
    For S = 32 To 126: For t = 32 To 126
    ActiveSheet.Unprotect Chr(i) & _
    Chr(j) & Chr(k) & Chr(l) & _
    Chr(m) & Chr(n) & Chr(o) & _
    Chr(p) & Chr(q) & Chr(r) & _
    Chr(S) & Chr(t)

    Next t: Next S: Next r: Next q
    Next p: Next o: Next n: Next m
    Next l: Next k: Next j: Next i
    MsgBox "Sheet protection disabled."
    End Sub

但是代码工作正常,因为它用于取消保护,您无法打印出正确的密码。我认为这与 VBA unprotect/protect 方法有关。

Internal XL passwords are about as useful for security as tissue paper. The reason is that the passwords you enter (i.e., with Tools/Protect/Protect Worksheet or /Protect Workbook) are not used directly in protection. Instead they are hashed (mathematically transformed) into a much less secure code. Effectively, any password of any length is transformed into a string of 12 characters, the first 11 of which have one of only two possible values. The remaining character can have up to 95 possible values, leading to only

2^11 * 95 = 194,560 potential passwords.

This may seem like a lot, but it only takes a few seconds for a modern computer >to try them all. As a comparison, a 4-character password containing just the 26 lower case alphabet characters has 456,976 combinations, and a 3-character password consisting of lower case, upper case and the digits 0-9 will have 238,328 combinations.

Again, it doesn't matter what your original password is, one of those 194K strings will unlock your sheet or workbook.

更多阅读this