RC4算法及其實踐(Hello Password)

Hello Password

一個簡單的安全密碼管理工具。

Why? 因爲我無法信任網上的密碼管理軟件。另外,加密數據離開對應的密碼管理軟件無法解析,這個問題困擾着我。

所以,不如自己寫一個簡簡單單的密碼管理工具,所以就開始擼了這個小項目,( PyPi地址GitHub地址 ),通過以下方式來保證數據安全:

  • 雲端實時同步:把存儲密碼的文件放到OneDrive或百度網盤這些支持本地實時同步的雲端,這樣多臺電腦都可以訪問密碼數據。
  • 工具本身乾淨:只有使用python基礎庫,沒有使用其他第三方庫,300行左右的代碼,沒有任何聯網操作。
  • 加密數據可讀:使用json格式來存儲密碼文件,可以單獨打開,通過源碼裏的加密方法自己解析密碼數據。

實現思路

hpass 使用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 盒生成與明文長度相同的祕鑰流,使用祕鑰流加密明文。

在這裏插入圖片描述

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 許可的免費開源軟件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章