from Crypto.Cipher import ARC4
cipher = ARC4.new(b'Key')
ct = cipher.encrypt(b'Plaintext')
ct
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[1], line 1 ----> 1 from Crypto.Cipher import ARC4 2 cipher = ARC4.new(b'Key') 3 ct = cipher.encrypt(b'Plaintext') ModuleNotFoundError: No module named 'Crypto'
!pip install pycryptodome
Defaulting to user installation because normal site-packages is not writeable Collecting pycryptodome Obtaining dependency information for pycryptodome from https://files.pythonhosted.org/packages/87/c4/c979db0914a23541d62c9e4b5e8a30f56a78c6dec8677db6a5327d306be5/pycryptodome-3.19.0-cp35-abi3-win_amd64.whl.metadata Downloading pycryptodome-3.19.0-cp35-abi3-win_amd64.whl.metadata (3.4 kB) Downloading pycryptodome-3.19.0-cp35-abi3-win_amd64.whl (1.7 MB) ---------------------------------------- 0.0/1.7 MB ? eta -:--:-- ---------- ----------------------------- 0.5/1.7 MB 9.4 MB/s eta 0:00:01 --------------------------------------- 1.7/1.7 MB 18.3 MB/s eta 0:00:01 ---------------------------------------- 1.7/1.7 MB 18.4 MB/s eta 0:00:00 Installing collected packages: pycryptodome Successfully installed pycryptodome-3.19.0
from Crypto.Cipher import ARC4
cipher = ARC4.new(b'Key')
ct = cipher.encrypt(b'Plaintext')
ct
b'\xbb\xf3\x16\xe8\xd9@\xaf\n\xd3'
cipher = ARC4.new(b'Key')
cipher.decrypt(ct)
b'Plaintext'
cipher.decrypt(ct)
b'W\xa7\xbd\xb8\xe7\xc0\x9d\x80\x11'
" ".join(f'{c:02X}' for c in ct)
'BB F3 16 E8 D9 40 AF 0A D3'
" ".join(f'{c:02d}' for c in ct)
'187 243 22 232 217 64 175 10 211'
print(*ct)
187 243 22 232 217 64 175 10 211
" ".join(f'{c:08b}' for c in ct)
'10111011 11110011 00010110 11101000 11011001 01000000 10101111 00001010 11010011'
b'\x01\x61\x41'
b'\x01aA'
bytes.fromhex('01 61 41')
b'\x01aA'
from Crypto.Cipher import ARC4
cipher = ARC4.new(b'\x01\x02\x03\x04\x05')
ct = cipher.encrypt(b'\x00'*32)
" ".join(f'{c:02x}' for c in ct)
'b2 39 63 05 f0 3d c0 27 cc c3 52 4a 0a 11 18 a8 69 82 94 4f 18 fc 82 d5 89 c4 03 a4 7a 0d 09 19'
bytes.fromhex('0102030405')
b'\x01\x02\x03\x04\x05'
bytes.fromhex('7C')
b'|'
Compute number of changed bits of ciphertext after changing one bit in plaintext
from Crypto.Cipher import ARC4
cipher = ARC4.new(b'\x01\x02\x03\x04\x05')
ct1 = cipher.encrypt(b'Hi\x00')
cipher = ARC4.new(b'\x01\x02\x03\x04\x05')
ct2 = cipher.encrypt(b'Hi\x40')
" ".join(f'{c:02x}' for c in b'Hi\x00')
'48 69 00'
" ".join(f'{c:02x}' for c in b'Hi\x40')
'48 69 40'
" ".join(f'{c:08b}' for c in b'Hi\x00')
'01001000 01101001 00000000'
" ".join(f'{c:08b}' for c in b'Hi\x40')
'01001000 01101001 01000000'
a = '01001000 01101001 00000000'
b = '01001000 01101001 01000000'
for i in range(len(a)):
if a[i] != b[i]:
print(i)
19
sum([1 for i in range(len(a)) if a[i]!=b[i]])
1
a = '01001000 01101001 00000000'
b = '01001000 01101110 01000000'
sum(1 for i in range(len(a)) if a[i]!=b[i]) #list [] can be omitted when it is parameter of function
4
[1 if a[i]!=b[i] else 0 for i in range(len(a))]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0]
sum([1 if a[i]!=b[i] else 0 for i in range(len(a))])
4
from Crypto.Cipher import DES
cipher = DES.new(b'12345678', DES.MODE_ECB)
ct = cipher.encrypt(b'HelloAll')
ct
b'\x11H\rM\x1a*l\xf9'
cipher.decrypt(ct)
b'HelloAll'
cipher.decrypt(ct)
b'HelloAll'
ct = cipher.encrypt(b'HelloAll12345678HelloAll')
ct
b'\x11H\rM\x1a*l\xf9\x96\xd0\x02\x88x\xd5\x8c\x89\x11H\rM\x1a*l\xf9'
ct = cipher.encrypt(b'Hello.')
ct
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[48], line 1 ----> 1 ct = cipher.encrypt(b'Hello.') 2 ct 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