RC4算法及其实践(Hello Password)

2020-06-29  本文已影响0人  何小有

Hello Password

一个简单的安全密码管理工具。

Why? 因为我无法信任网上的密码管理软件。另外,加密数据离开对应的密码管理软件无法解析,这个问题困扰着我。

所以,不如自己写一个简简单单的密码管理工具,所以就开始撸了这个小项目,( PyPi地址GitHub地址 ),通过以下方式来保证数据安全:

实现思路

hpass 使用RC4算法进行数据加密存储。

RC4算法实现概括

使用 key 生成 S 盒

密钥调度算法(KSA)

KSA 算法初始化长度为 256 的 S 盒。

def rc4_init_s_box(key):
    # 将 0 到 255 的互不重复的元素装入 S 盒
    s_box = list(range(256))
    j = 0
    # 根据密钥打乱 S 盒
    for i in range(256):
        j_step_1 = j + s_box[i]
        j_step_2 = j_step_1 + ord(key[i % len(key)])
        j_step_3 = j_step_2 % 256
        j = j_step_3
        s_box[i], s_box[j] = s_box[j], s_box[i]
    return s_box

使用 S 盒生成密钥流

伪随机数生成算法(PRGA)

PRGA 算法根据 S 盒生成与明文长度相同的秘钥流,使用秘钥流加密明文。

伪随机数生成算法(PRGA)
def rc4_res_program(s_box, message):
    res = []
    i = j = 0
    # 遍历明文长度的每一个字节
    for s in message:
        # i 和 j 定位 S 盒中的一个元素
        i = (i + 1) % 256
        j = (j + s_box[i]) % 256
        # 与输入字节异或,同时,还改变了 S 盒
        s_box[i], s_box[j] = s_box[j], s_box[i]
        # 得到密文 k
        t = (s_box[i] + s_box[j]) % 256
        k = s_box[t]
        # 由于异或运算的特性,使得加密与解密过程一致
        res.append(chr(ord(s) ^ k))
    # 如果输入的是明文,输出的就是密文
    # 如果输入的是密文,输出的就是明文
    return res

快速开始

使用 hpass -h 查看详细命令

$ hpass -h
usage: hpass [-h] [-v] [-r PASSWORD_LENGTH] [-i] [-c]

Hello Password

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         View version information
  -r PASSWORD_LENGTH, --random_password PASSWORD_LENGTH
                        Randomly generate passwords containing uppercase and lowercase letters/numbers/symbols
  -i, --initialization  Create or specify a password storage file in the current directory
  -c, --cli             Start CLI Workbench
  -t, --transfer        Reset primary password (Change master password)

使用 hpass -i 在指定目录中初始化密码数据文件

$  hpass -i
Your primary password:
Enter your primary password again:
Find the password storage file in the current directory
Password storage file initialized successfully

使用 hpass 进入工作台

$ hpass
Your primary password:
H-Pass>

使用 random 生成随机密码

H-Pass> random 16
hiSVJ@77AEYFaZhu

使用 add 添加一个新帐户

H-Pass> add
The following is the information required for the new password :
Website = https://www.yeah.net/
Notes = 163 Yeah Mail
Username = xxxxxxx@yeah.net
Email =
Phone =
Password = hiSVJ@77AEYFaZhu
The new password has been successfully added!

使用 search 搜索帐户数据

H-Pass> search yeah
+----+-----------------------+--------------+
| ID |        Website        |    Notes     |
+----+-----------------------+--------------+
| 18 | https://www.yeah.net/ | 163 Yeah Mail |
+----+-----------------------+--------------+

使用 get 查看帐户密码

H-Pass> get 18
website = https://www.yeah.net/
notes = 163 Yeah Mail
username = xxxxxxx@yeah.net
email =
phone =
password = hiSVJ@77AEYFaZhu

使用 set 更改帐户数据

H-Pass> set 18 notes
Original notes = 163 Yeah Mail
Now notes = Yeah Mail
Password value modified successfully!

使用 help 查看工作台帮助信息

H-Pass> help
filepath           - Print the absolute path of the password storage file
all                - View the basic information of all password data
add                - Enter a new password data
search <keyword>   - Find password data by keyword
random <length>    - Generate a secure password of specified length
get <id>           - View the password data of the specified id
del <id>           - Delete the password data of the specified id
set <id> <key>     - Modify the key value of the password data of specified id

安装

像往常一样,最简单的方法是使用pip:

$ pip install hpass

或者,您可以下载 hpass-x.x.x.tar.gz 安装文件:

$ pip install hpass-x.x.x.tar.gz

Pip 将为您安装依赖项 (coloramaPrettyTable) 。或者,您可以克隆GitHub仓库:

$ git clone https://github.com/hekaiyou/hpass.git --recursive
$ cd hpass
$ python setup.py install

开源协议

hpass 是根据 MIT License 许可的免费开源软件。

上一篇 下一篇

猜你喜欢

热点阅读