Python密码安全分析-拒绝盗号(文末源代码)

请问小伙伴们有没有被盗号的经历呢?

 那么盗号的原因是什么呢?学网络安全的人可能脱口而出:“解析密码!”那么,黑客是怎么解析密码的呢?原来,是用的暴力破解,首先利用BUG绕过次数限制,之后使用工具破解,注意,这里会用到常见的密码而非简单的密码,比如“QWERTYUIOP”,“123456”,“000000”等。

所以密码越复杂,越不常见安全程度越高。那么,我用ChatGPT生成了一个密码安全分析,完美分析密码复杂度。

接下来,看一下ChatGPT对这个代码的介绍,顺便感受一下AI的魅力!(代码框中为AI所说)

我来逐一讲解这个源代码。

第一段:

```python
import math

def crack_time(password):
    """计算密码破解所需的时间"""

    # 整理密码信息:长度、字符类型、字符集合
    length = len(password)
    charsets = [['a', 'z'], ['A', 'Z'], ['0', '9'], ['!', '?', '@', '#', '$']]
    charset_sizes = [ord(c[-1]) - ord(c[0]) + 1 for c in charsets]
```

这段代码是用来计算密码破解所需的时间。`math` 模块被导入进来,是为了做一个指数计算。函数 `crack_time` 接受一个字符串变量 `password`,表示需要破解的密码。

接下来,代码用两个变量分别表示密码的长度和字符集合。字符集合包括小写字母、大写字母、数字、符号,每个字符集都被表示为一个由起始字符和终止字符组成的列表。然后,这些字符集的大小也被计算出来,以备后面的破解时间计算中使用。

第二段:

```python
    # 遍历字符集合计算破解时间
    num_combinations = 0
    for i in range(length):
        char = password[i]
        for j, charset in enumerate(charsets):
            if char in charset:
                num_combinations += (charset_sizes[j] ** (length - i - 1))
                break
```

这里是代码的主要计算部分。首先初始化一个变量 `num_combinations` 用来累计密码破解所需的尝试次数。

然后,代码遍历整个密码,对于每个字符,检查它属于哪个字符集合,并计算出该字符集合中可能的排列组合数。这个数值是字符集大小的幂次,乘以 4 个字符集合在密码中出现的位置中这个字符之后的字符数的乘积。

最后,所有字符的可能排列组合数被相加,也就是 `num_combinations`。

第三段:

```python
    # 计算破解时间
    combinations_per_second = 1000000000
    seconds = num_combinations / combinations_per_second
    minutes = seconds / 60
    hours = minutes / 60
    days = hours / 24
    years = days / 365

    return years, length
```

在这一段代码中,计算破解密码所需要的时间。首先,代码定义了每秒钟可以尝试的密码次数 `combinations_per_second`,这个值假设为 10 亿,也就是每秒尝试 10 亿个密码。

然后,代码把前面计算出的 `num_combinations` 除以每秒钟可以尝试的密码次数,得到一个表示尝试需要的秒数的值。

接下来的几行代码是将这个秒数转换成分钟、小时、天和年的时间单位,以便于理解。

最后,年数和密码长度一起返回。

第四段:

```python
def rate_password(password):
    """根据密码破解时间评估密码安全性和复杂度"""
    years, length = crack_time(password)
    if years < 0.01:
        rating = "非常不安全"
    elif years < 1:
        rating = "不安全"
    elif years < 10:
        rating = "安全"
    else:
        rating = "非常安全"

    # 判断密码复杂度
    has_lowercase = any(c.islower() for c in password)
    has_uppercase = any(c.isupper() for c in password)
    has_digit = any(c.isdigit() for c in password)
    has_symbol = any(c in '!?@#$' for c in password)

    if length >= 8 and has_lowercase and has_uppercase and has_digit and has_symbol:
        complexity = "强"
    elif length >= 6 and has_lowercase and has_uppercase and has_digit:
        complexity = "中"
    else:
        complexity = "弱"I

    return rating, complexity
```

这一段代码定义了一个函数 `rate_password`,用来根据密码破解所需的时间来评估密码的安全性和复杂度。函数接受一个密码字符串作为参数,并调用 `crack_time` 函数来获取密码破解所需的年数和密码长度。

接下来,代码根据破解所需年数来判断密码的安全性,将评分存储在变量 `rating` 中。如果破解年数小于 0.01,表示密码非常不安全;如果破解年数小于 1,表示密码不安全;如果破解年数小于 10,表示密码安全;否则表示密码非常安全。

接下来,代码通过检查密码中的字符类型和长度,来判断密码的复杂度。如果密码长度大于等于 8,同时包含小写字母、大写字母、数字和符号,那么密码复杂度被评定为 “强”;如果密码长度大于等于 6,同时包含小写字母、大写字母和数字,那么密码复杂度被评定为 “中”;否则密码复杂度被评定为 “弱”。

最后,函数返回一个元组,包括两个字符串变量,表示密码的安全性和复杂度。

第五段:

```python
if __name__ == '__main__':
    password = input("请输入你想测试安全性的密码:")
    rating, complexity = rate_password(password)
    print(f"你输入的密码需要 {crack_time(password)[0]:.2f} 年才能被破解,安全性评级为:{rating},复杂度为:{complexity}")
```

这一段就是整个代码的主函数了。首先程序会等待用户输入需要测试安全性的密码。然后,程序调用函数 `rate_password` 来评估密码的安全性和复杂度。最后,程序输出密码破解所需的年数、密码的安全性评级以及复杂度。

可以看到,现在ChatGPT还是有待改进,文章中频繁用到“接下来”,“然后”等词语,但是已经可以。

这个代码可以分析出破解时间和密码强度,一切均为线下计算,不连接互联网。并且没有第三方库,只用标准库。

接下来,放源码!

import math

def crack_time(password):
    """计算密码破解所需的时间"""

    # 整理密码信息:长度、字符类型、字符集合
    length = len(password)
    charsets = [['a', 'z'], ['A', 'Z'], ['0', '9'], ['!', '?', '@', '#', '$']]
    charset_sizes = [ord(c[-1]) - ord(c[0]) + 1 for c in charsets]

    # 遍历字符集合计算破解时间
    num_combinations = 0
    for i in range(length):
        char = password[i]
        for j, charset in enumerate(charsets):
            if char in charset:
                num_combinations += (charset_sizes[j] ** (length - i - 1))
                break

    # 计算破解时间
    combinations_per_second = 1000000000
    seconds = num_combinations / combinations_per_second
    minutes = seconds / 60
    hours = minutes / 60
    days = hours / 24
    years = days / 365

    return years, length


def rate_password(password):
    """根据密码破解时间评估密码安全性和复杂度"""
    years, length = crack_time(password)
    if years < 0.01:
        rating = "非常不安全"
    elif years < 1:
        rating = "不安全"
    elif years < 10:
        rating = "安全"
    else:
        rating = "非常安全"

    # 判断密码复杂度
    has_lowercase = any(c.islower() for c in password)
    has_uppercase = any(c.isupper() for c in password)
    has_digit = any(c.isdigit() for c in password)
    has_symbol = any(c in '!?@#$' for c in password)

    if length >= 8 and has_lowercase and has_uppercase and has_digit and has_symbol:
        complexity = "强"
    elif length >= 6 and has_lowercase and has_uppercase and has_digit:
        complexity = "中"
    else:
        complexity = "弱"

    return rating, complexity


if __name__ == '__main__':
    password = input("请输入你想测试安全性的密码:")
    rating, complexity = rate_password(password)
    print(f"你输入的密码需要 {crack_time(password)[0]:.2f} 年才能被破解,安全性评级为:{rating},复杂度为:{complexity}")

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值