This commit is contained in:
UrloMythus
2026-02-19 20:15:03 +01:00
parent 7785e8c604
commit cfc6bbabc9
181 changed files with 32141 additions and 4629 deletions

View File

@@ -8,7 +8,7 @@ from .aes import AES
from .rijndael import Rijndael
from .cryptomath import bytesToNumber, numberToByteArray
__all__ = ['new', 'Python_AES']
__all__ = ["new", "Python_AES"]
def new(key, mode, IV):
@@ -37,22 +37,21 @@ class Python_AES(AES):
plaintextBytes = bytearray(plaintext)
chainBytes = self.IV[:]
#CBC Mode: For each block...
for x in range(len(plaintextBytes)//16):
#XOR with the chaining block
blockBytes = plaintextBytes[x*16 : (x*16)+16]
# CBC Mode: For each block...
for x in range(len(plaintextBytes) // 16):
# XOR with the chaining block
blockBytes = plaintextBytes[x * 16 : (x * 16) + 16]
for y in range(16):
blockBytes[y] ^= chainBytes[y]
#Encrypt it
# Encrypt it
encryptedBytes = self.rijndael.encrypt(blockBytes)
#Overwrite the input with the output
# Overwrite the input with the output
for y in range(16):
plaintextBytes[(x*16)+y] = encryptedBytes[y]
plaintextBytes[(x * 16) + y] = encryptedBytes[y]
#Set the next chaining block
# Set the next chaining block
chainBytes = encryptedBytes
self.IV = chainBytes[:]
@@ -64,19 +63,18 @@ class Python_AES(AES):
ciphertextBytes = ciphertext[:]
chainBytes = self.IV[:]
#CBC Mode: For each block...
for x in range(len(ciphertextBytes)//16):
#Decrypt it
blockBytes = ciphertextBytes[x*16 : (x*16)+16]
# CBC Mode: For each block...
for x in range(len(ciphertextBytes) // 16):
# Decrypt it
blockBytes = ciphertextBytes[x * 16 : (x * 16) + 16]
decryptedBytes = self.rijndael.decrypt(blockBytes)
#XOR with the chaining block and overwrite the input with output
# XOR with the chaining block and overwrite the input with output
for y in range(16):
decryptedBytes[y] ^= chainBytes[y]
ciphertextBytes[(x*16)+y] = decryptedBytes[y]
ciphertextBytes[(x * 16) + y] = decryptedBytes[y]
#Set the next chaining block
# Set the next chaining block
chainBytes = blockBytes
self.IV = chainBytes[:]
@@ -89,7 +87,7 @@ class Python_AES_CTR(AES):
self.rijndael = Rijndael(key, 16)
self.IV = IV
self._counter_bytes = 16 - len(self.IV)
self._counter = self.IV + bytearray(b'\x00' * self._counter_bytes)
self._counter = self.IV + bytearray(b"\x00" * self._counter_bytes)
@property
def counter(self):
@@ -102,9 +100,9 @@ class Python_AES_CTR(AES):
def _counter_update(self):
counter_int = bytesToNumber(self._counter) + 1
self._counter = numberToByteArray(counter_int, 16)
if self._counter_bytes > 0 and \
self._counter[-self._counter_bytes:] == \
bytearray(b'\xff' * self._counter_bytes):
if self._counter_bytes > 0 and self._counter[-self._counter_bytes :] == bytearray(
b"\xff" * self._counter_bytes
):
raise OverflowError("CTR counter overflowed")
def encrypt(self, plaintext):