🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.programming
Article #217418

Python i klasy oraz dawka magii czyli niezrozumienie

#217418
From: Roman Tyczka
Date: Wed, 03 Mar 2021 11:57
75 lines
3515 bytes
Używam biblioteki Cryptography (https://github.com/pyca/cryptography) i 
ona oferuje dla szyfrów blokowych klasy, które obsługują tryby mieszania 
bloków. Użycie trybów z konkretnym algorytmem szyfrującym, dla tych 
samych danych wejściowych, zwraca inny wynik, i to jest oczekiwane 
zachowanie.

Ale nie mogę pojąć jak to jest zrobione od strony języka, bo gdy patrzę 
na klasy trybów to niektóre się niczym nie różnią, a wynik ich użycia 
daje inne efekty, np. takie trzy tryby OFB, CFB i CFB8:

class OFB(Mode, ModeWithInitializationVector):
     name = "OFB"

     def __init__(self, initialization_vector: bytes):
         utils._check_byteslike("initialization_vector", 
initialization_vector)
         self._initialization_vector = initialization_vector

     initialization_vector = 
utils.read_only_property("_initialization_vector")
     validate_for_algorithm = _check_iv_and_key_length


class CFB(Mode, ModeWithInitializationVector):
     name = "CFB"

     def __init__(self, initialization_vector: bytes):
         utils._check_byteslike("initialization_vector", 
initialization_vector)
         self._initialization_vector = initialization_vector

     initialization_vector = 
utils.read_only_property("_initialization_vector")
     validate_for_algorithm = _check_iv_and_key_length


class CFB8(Mode, ModeWithInitializationVector):
     name = "CFB8"

     def __init__(self, initialization_vector: bytes):
         utils._check_byteslike("initialization_vector", 
initialization_vector)
         self._initialization_vector = initialization_vector

     initialization_vector = 
utils.read_only_property("_initialization_vector")
     validate_for_algorithm = _check_iv_and_key_length

Poza polem "name" każda klasa jest identyczna, a jednak wynik jest inny. 
Jak to działa?

Kod powyższy jest w pliku:

https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/primitives/ciphers/modes.py


Przykład szyfrowania z algorytmem AES:

  import os
  from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, 
modes
  key = os.urandom(32)
  iv = os.urandom(16)
  cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
  encryptor = cipher.encryptor()
  ct = encryptor.update(b"a secret message") + encryptor.finalize()

Dokumentacja biblioteki:

https://cryptography.io/en/latest/hazmat/primitives/symmetric-encryption.html

-- 
pzdr
Roman
Technical Details
Message-ID: <603f79a0$0$514$65785112@news.neostrada.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!fu-berlin.de!newsreader4.netcologne.de!news.netcologne.de!peer04.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!newsfeed.neostrada.pl!unt-exc-01.news.neostrada.pl!unt-spo-a-01.news.neostrada.pl!news.neostrada.pl.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!fu-berlin.de!newsreader4.netcologne.de!news.netcologne.de!peer04.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!newsfeed.neostrada.pl!unt-exc-01.news.neostrada.pl!unt-spo-a-01.news.neostrada.pl!news.neostrada.pl.POSTED!not-for-mail
Newsgroups: pl.comp.programming
X-Mozilla-News-Host: news://news.nask.pl:119
From: Roman Tyczka <romantyczka@hate.you.spammer>
Subject: Python i klasy oraz dawka magii czyli niezrozumienie
Date: Wed, 3 Mar 2021 12:57:19 +0100
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0
MIME-Version: 1.0
Lines: 75
Message-ID: <603f79a0$0$514$65785112@news.neostrada.pl>
Organization: Telekomunikacja Polska
NNTP-Posting-Host: 194.49.104.179
X-Trace: 1614772640 unt-rea-a-01.news.neostrada.pl 514 194.49.104.179:42480
X-Complaints-To: abuse@news.neostrada.pl
X-Received-Bytes: 3415
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit