Misol sifatida AES shifrlash dasturini tuzib ko’rsataman. Buning uchun python dasturlash tilidan foydalanaman.
AES shifrlash algoritmini Python dasturida ishlatish uchun, pycryptodome moduli yuklab olingishi kerak. Bunday modulni pip yordamida o'rnatish mumkin:
pip install pycryptodome
from Crypto.Cipher import AES
import base64
def pad(s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
def encrypt(message, key):
message = pad(message)
iv = b'InitializationVe'
cipher = AES.new(key, AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(message))
def decrypt(ciphertext, key):
ciphertext = base64.b64decode(ciphertext)
iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
return plaintext.rstrip(b"\0")
key = b'0123456789abcdef' # 16 byte kalit
message = b'Salomatlik sifatida AES shifrlashni o\'rganaylik'
# Matn shifrlanadi
ciphertext = encrypt(message, key)
print(ciphertext)
# Matn qaytariladi
plaintext = decrypt(ciphertext, key)
print(plaintext)
Yuqoridagi kodda, pad funksiyasi, AES blok hajmini to'ldirish uchun qo'shimcha baytlar qo'shadi. encrypt va decrypt funksiyalari, mazmuni shifrlash va unshifrlashni amalga oshiradi. key bu AES shifrlash uchun kalit, o'lchami 16 byte (128 bit) bo'ladi. iv (initialization vector) shifrlashni boshlash uchun murakkabliki 16 baytli tartiblash (bu iv vektoriga qarang). Natijada, shifrlangan matn ciphertext ni qaytaradi. plaintext esa shifrlangan matnni unshifrlash jarayonida qaytaradi.
AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI SAMARQAND FILIALI
Do'stlaringiz bilan baham: |