from Crypto.Util.Padding import pad, unpad
from Crypto.Cipher import AES, DES
from Crypto.Random import get_random_bytes
from base64 import b64encode, b64decode
#encryption
data = b'TestUPJS'
key = get_random_bytes(8)
cipher = DES.new(key, DES.MODE_ECB)
ct = cipher.encrypt(data)
print(ct)
print(b64encode(ct).decode('utf-8'))
b'\xd3\xc0xz\xd2*%\xf4' 08B4etIqJfQ=
#decryption
decipher = DES.new(key, DES.MODE_ECB)
pt = decipher.decrypt(ct)
print(pt)
b'TestUPJS'
#encryption
data = b'short'
key = get_random_bytes(8)
cipher = DES.new(key, DES.MODE_ECB)
ct = cipher.encrypt(data)
print(ct)
print(b64encode(ct).decode('utf-8'))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[5], line 5 3 key = get_random_bytes(8) 4 cipher = DES.new(key, DES.MODE_ECB) ----> 5 ct = cipher.encrypt(data) 6 print(ct) 7 print(b64encode(ct).decode('utf-8')) File ~\AppData\Roaming\Python\Python311\site-packages\Crypto\Cipher\_mode_ecb.py:141, in EcbMode.encrypt(self, plaintext, output) 139 if result: 140 if result == 3: --> 141 raise ValueError("Data must be aligned to block boundary in ECB mode") 142 raise ValueError("Error %d while encrypting in ECB mode" % result) 144 if output is None: ValueError: Data must be aligned to block boundary in ECB mode
pad(b'short', 8) #pad to the given length
b'short\x03\x03\x03'
pad(b'short', 12)
b'short\x07\x07\x07\x07\x07\x07\x07'
#encryption
data = b'short'
key = get_random_bytes(8)
cipher = DES.new(key, DES.MODE_ECB)
ct = cipher.encrypt(pad(data, 8))
print(ct)
print(b64encode(ct).decode('utf-8'))
b'\x9b%o\xf8r\x8e\x1a%' myVv+HKOGiU=
#decryption
decipher = DES.new(key, DES.MODE_ECB)
pt = decipher.decrypt(ct)
print(pt)
unpad(pt, 8)
b'short\x03\x03\x03'
b'short'
blocksize=8
for s in ['pkcs7', 'iso7816', 'x923']:
print( '{:10s}'.format(s), pad(b'x', blocksize, style=s), pad(b'short', blocksize, style=s), pad(b'text1234', blocksize, style=s))
pkcs7 b'x\x07\x07\x07\x07\x07\x07\x07' b'short\x03\x03\x03' b'text1234\x08\x08\x08\x08\x08\x08\x08\x08' iso7816 b'x\x80\x00\x00\x00\x00\x00\x00' b'short\x80\x00\x00' b'text1234\x80\x00\x00\x00\x00\x00\x00\x00' x923 b'x\x00\x00\x00\x00\x00\x00\x07' b'short\x00\x00\x03' b'text1234\x00\x00\x00\x00\x00\x00\x00\x08'
#encryption
data = b'TestUPJSabcdefghTestUPJS'
key = get_random_bytes(8)
cipher = DES.new(key, DES.MODE_ECB)
ct = cipher.encrypt(data)
print(ct)
print(b64encode(ct).decode('utf-8'))
#decryption
decipher = DES.new(key, DES.MODE_ECB)
pt = decipher.decrypt(ct)
print(pt)
b'\x19\x1e\xf0\xb7\\/\x9f(\x93\xe0U\xd6\xce#\xb1\xdc\x19\x1e\xf0\xb7\\/\x9f(' GR7wt1wvnyiT4FXWziOx3Bke8LdcL58o b'TestUPJSabcdefghTestUPJS'
def wrap(text, size=8):
for i in range((len(text)+size-1) // size):
print(text[i*size:(i+1)*size])
wrap(pt)
b'TestUPJS' b'abcdefgh' b'TestUPJS'
wrap(ct)
b'\x19\x1e\xf0\xb7\\/\x9f(' b'\x93\xe0U\xd6\xce#\xb1\xdc' b'\x19\x1e\xf0\xb7\\/\x9f('
print(*ct)
25 30 240 183 92 47 159 40 147 224 85 214 206 35 177 220 25 30 240 183 92 47 159 40
ct[12]
206
ct[12] ^ 64
142
ct[12] = ct[12] ^ 64 #bytes are IMMUTABLE
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[42], line 1 ----> 1 ct[12] = ct[12] ^ 64 TypeError: 'bytes' object does not support item assignment
ct
b'\x19\x1e\xf0\xb7\\/\x9f(\x93\xe0U\xd6\xce#\xb1\xdc\x19\x1e\xf0\xb7\\/\x9f('
bytes(ct[i] for i in range(len(ct)))
b'\x19\x1e\xf0\xb7\\/\x9f(\x93\xe0U\xd6\xce#\xb1\xdc\x19\x1e\xf0\xb7\\/\x9f('
bytes(ct[i]^64 if i == 12 else ct[i] for i in range(len(ct)))
b'\x19\x1e\xf0\xb7\\/\x9f(\x93\xe0U\xd6\x8e#\xb1\xdc\x19\x1e\xf0\xb7\\/\x9f('
# changing one bit in the 13th byte
ct_changed = bytes(ct[i]^64 if i == 12 else ct[i] for i in range(len(ct)))
print(*ct)
print(*ct_changed)
25 30 240 183 92 47 159 40 147 224 85 214 206 35 177 220 25 30 240 183 92 47 159 40 25 30 240 183 92 47 159 40 147 224 85 214 142 35 177 220 25 30 240 183 92 47 159 40
decipher = DES.new(key, DES.MODE_ECB)
pt = decipher.decrypt(ct)
print(pt)
b'TestUPJSabcdefghTestUPJS'
decipher = DES.new(key, DES.MODE_ECB)
pt = decipher.decrypt(ct_changed)
pt
b'TestUPJS\xe1\xe4\xed\xfc(\x1e\xdfOTestUPJS'
wrap(data)
print('--------')
wrap(pt)
b'TestUPJS' b'abcdefgh' b'TestUPJS' -------- b'TestUPJS' b'\xe1\xe4\xed\xfc(\x1e\xdfO' b'TestUPJS'
#encryption
data = b'TestUPJSabcdefghTestUPJS12345678'
key = get_random_bytes(8)
cipher = DES.new(key, DES.MODE_CBC)
ct = cipher.encrypt(data)
print(ct)
print(b64encode(ct).decode('utf-8'))
#decryption
decipher = DES.new(key, DES.MODE_CBC)
pt = decipher.decrypt(ct)
print(pt) #WRONG RESULT (actually, only first block is wrong)
b'\x9e\xfe\xab\xe4x\xa4\t#d\x94I\xf2W_\xc7\x82\xeb\xb7\t\xf9\x04\x00_\x97\x87\xf7+.C\x0e\xbf\x82' nv6r5HikCSNklEnyV1/Hguu3CfkEAF+Xh/crLkMOv4I= b'\xcdW \xefI\x87V\xb6abcdefghTestUPJS12345678'
There is one more parameter, initilization vector. It is randomly chosen if not specified ...
#encryption
data = b'TestUPJSabcdefghTestUPJS12345678'
key = get_random_bytes(8)
cipher = DES.new(key, DES.MODE_CBC) #use random IV
ct = cipher.encrypt(data)
print(ct)
print(b64encode(ct).decode('utf-8'))
#decryption
decipher = DES.new(key, DES.MODE_CBC, iv=cipher.iv) # use IV from the encryption
pt = decipher.decrypt(ct)
print(pt) #WRONG RESULT (actually, only first block is wrong)
b'\xfd\x10\xf1\xb9\x7f\xfe)j\xae\xfc\x974\xceZZG9;#n?\xe1\xdc\xa0\xf8\x84\x9f\x83\x05\xc7\xe1r' /RDxuX/+KWqu/Jc0zlpaRzk7I24/4dyg+ISfgwXH4XI= b'TestUPJSabcdefghTestUPJS12345678'
wrap(data)
print('--------')
wrap(pt)
b'TestUPJS' b'abcdefgh' b'TestUPJS' b'12345678' -------- b'TestUPJS' b'abcdefgh' b'TestUPJS' b'12345678'
# changing one bit in the 13th byte
ct_changed = bytes(ct[i]^8 if i == 12 else ct[i] for i in range(len(ct)))
#decryption
decipher = DES.new(key, DES.MODE_CBC, iv=cipher.iv) # use IV from the encryption
pt = decipher.decrypt(ct_changed)
print(pt)
b'TestUPJS\xb5A\xd9v\xe2\x05\xd8RTest]PJS12345678'
wrap(data)
print('--------')
wrap(pt)
b'TestUPJS' b'abcdefgh' b'TestUPJS' b'12345678' -------- b'TestUPJS' b'\xb5A\xd9v\xe2\x05\xd8R' b'Test]PJS' b'12345678'
print(*b'U')
85
print(*b']')
93
85 ^ 93
8