# aes-ecb加密
import base64
import hashlib
import json
from crypto.cipher import aes
# 秘鑰
secret =
'1111111111111111'
block_size =
16# bytes
# 補位,補齊16位
pad =
lambda s: s +
(block_size -
len(s)
% block_size)
* \ chr
(block_size -
len(s)
% block_size)
# 去除補位
unpad =
lambda s: s[:-
ord(s[
len(s)-1
:])]
# 使用sha1方法生成的隨機數,對key做處理
defget_sha1prng_key
(key)
: signature = hashlib.sha1(key.encode())
.digest(
) signature = hashlib.sha1(signature)
.digest(
)return
''.join(
['%02x'
% i for i in signature]
).upper()[
:32]# 判斷data是否為16的整數倍,不足的補"\0"
defadd_to_16
(text):if
len(text.encode(
'utf-8'))
%16: add =16-
(len
(text.encode(
'utf-8'))
%16)else
: add =
0 text = text +
('\0'
* add)
return text.encode(
'utf-8'
)# 加密函式
defencrypt
(text)
: mode = aes.mode_ecb
# 補位
text1 = pad(text)
# 不足16位補上"\0"
text2 = add_to_16(text1)
key2 = get_sha1prng_key(secret)
cryptos = aes.new(
bytes
.fromhex(key2)
, mode)
cipher_text = cryptos.encrypt(text2)
# 用base64轉成字串形式
encrypted_text =
str(base64.encodebytes(cipher_text)
, encoding=
'utf-8'
)# 執行加密並轉碼返回bytes
return encrypted_text
# 解密,要先去掉補足的空格用strip()去掉
defdecrypt
(text)
: mode = aes.mode_ecb
key2 = get_sha1prng_key(secret)
cryptor = aes.new(
bytes
.fromhex(key2)
, mode)
# 優先逆向解密base64成bytes
base64_decrypted = base64.decodebytes(text.encode(encoding=
'utf-8'))
# 執行解密密並轉碼返回str
decrypted_text =
str(cryptor.decrypt(base64_decrypted)
, encoding=
'utf-8'
).replace(
'\0',''
)# 去除補位
decrypted_text1 = unpad(decrypted_text)
return decrypted_text1
req_data =
# dict格式的可以用json.dumps序列化
# req_data = json.dumps(req_data)
content = encrypt(
str(req_data)
)decrypt_data = decrypt(content)
print
("明文:"
, decrypt_data)
print
("key:"
, secret)
print
("密文:"
, content)
#結果明文:
key:
1111111111111111
密文: yjgg1v9jyo4/ezgjw8yy3lm390mgkwdjhv1jxzuz+/8
=
用python實現SYN Flooding攻擊
作業裡的 直接這裡copy在這裡記錄一下。在tcp ip協議中,當客戶端試著與伺服器間建立tcp連線時,正常情況下客戶端與伺服器端進行tcp三次握手 1.客戶端通過傳送syn同步 synchronize 資訊到伺服器要求建立連線。2.伺服器通過響應客戶端syn ack以抄收 acknowledge ...
用python實現棧 Python實現棧的方法
usr bin env python 定義乙個列表來模擬棧 stack def pu 出棧,用到了pop 函式 def popit if len stack 0 print cannot pop from an empty stack else print removed stack.pop 編歷棧...
pypy 用python實現的python
pypy 分為兩部分 乙個 python 的實現 和 乙個編譯器 pypy provides infrastructure for building interpreters in r python.this infrastructure makes it much easier than star...