16.加密解密基础

base64

import base64

def main():
    s = "haha123"
    bs = base64.b64encode(s.encode('utf-8'))
    print(bs)   //b'aGFoYTEyMw=='
    print(bs.decode('utf-8'))   # aGFoYTEyMw==

    s1 = "aGFoYTEyMw=="
    bs1 = str(base64.b64decode(s1),"utf-8")
    print(bs1)   # haha123

DES

加密需要key且必须为8个字节(8位),之后会选择模式,使用ECB模式(电子密本方式,JAVA封装的DES算法的默认模式)

由于8字节容易被穷举,所以使用3DES加密,3DES长度为168位

安装库

pip install pycryptodome
pip install pycryptodomex

加密

from Cryptodome.Cipher import DES
import binascii

def main():
		# 密钥,长度必须为8字节
    key = b'abcdefgh'
		# 创建一个DES对象
    des = DES.new(key, DES.MODE_ECB)
		
		# 密文
    text = "haha123 nihao"
		# 加密需要保证时8的倍数,不足用=补全
    text = text + (8 - (len(text) % 8)) * '='
    print(text)      # haha123 nihao===
		# 加密为十六进制
    encrypt_text = des.encrypt(text.encode())
    print(encrypt_text)    # b"\x97%\xdf\xd2\x87\x87aX\\\x8a\x05\xabW\x1fY'"
		# 通过binascii将十六进制转换ascii输出
    encrypt_res = binascii.b2a_hex(encrypt_text)
    print(encrypt_res)    # b'9725dfd2878761585c8a05ab571f5927'

解密

from Cryptodome.Cipher import DES
import binascii

def main():
    key = b'abcdefgh'
    des = DES.new(key, DES.MODE_ECB)
    encrypto_res = b'9725dfd2878761585c8a05ab571f5927'
		# 将ascii转换成十六进制
    encrypt_text = binascii.a2b_hex(encrypto_res)
		# 对十六进制数据进行解密
    decrypt_text = des.decrypt(encrypt_text)
    print(decrypt_text)  # haha123 nihao===

AES

key长度必须是8的倍数,被加密的数据也要是8的倍数

加密

from Cryptodome.Cipher import AES
import binascii

def main():
    key = b'abcdefghabcdefgh'
    text = 'haha123 wocao'
    text = text + (16 - (len(text) % 16)) * '='
    print(text)
    aes = AES.new(key, AES.MODE_ECB)
    encrypt_text = aes.encrypt(text.encode())
    encrypt_res = binascii.b2a_hex(encrypt_text)
    print(encrypt_res)

解密

from Cryptodome.Cipher import AES
import binascii

def main():
    key = b'abcdefghabcdefgh'

    encrypt_res = b'5ff27bccfc1aa3c0335f2f21bb9f17db'
    aes = AES.new(key, AES.MODE_ECB)
    encrypt_text = binascii.a2b_hex(encrypt_res)
    decrypt_text = aes.decrypt(encrypt_text)
    print(decrypt_text)

MD5

加密

from hashlib import md5

def main():
		# 创建字符串
    s = 'admin'
		# 创建md5对象
    new_md5 = md5()
		# 通过该对象中的函数进行utf-8编码变成md5并保存在hexdigest
    new_md5.update(s.encode('utf-8'))
		# 将保存在hexdigest的md5进行输出
    print(new_md5.hexdigest())

md5函数封装

from hashlib import md5

def md5_encrypt(input):
    s = input
    new_md5 = md5()
    new_md5.update(s.encode('utf-8'))
    return new_md5.hexdigest()

def main():
    a = md5_encrypt('admin')
    print(a)

字典生成器

import itertools

def main():
		# 包含数字
    words = '1234567890'
		# 将包含的数字按照复杂度2进行穷举
    temp = itertools.permutations(words, 2)
		# 打开一个叫dict.txt的文本并追加,将该操作命名为password
    passwords = open("dict.txt", "a")
    for i in temp:
				# 将temp中穷举的数字写入并换行
        passwords.write("".join(i))
        passwords.write("".join("\n"))
		# 关闭文件
    passwords.close()

image

ssh暴力破解

安装包

pip install Pexpect

python实现

from pexpect import pxssh

def Login(server, username, password):
    try:
        s = pxssh.pxssh()
        s.login(server, username, password)
        print("Success!")
    except:
        print("Failed")

def main():
    Login("192.168.223.131", "kali", "kali")

之后可以结合密码生成器对ssh进行穷举破解

posted @ 2022-04-22 12:03  icui4cu  阅读(143)  评论(0编辑  收藏  举报