-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc09.py
31 lines (21 loc) · 900 Bytes
/
c09.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def pad(data, size):
pad_size = size - len(data) % size
return data + bytes([pad_size] * pad_size)
def main():
want = b'YELLOW SUBMARINE\x04\x04\x04\x04'
got = pad(b'YELLOW SUBMARINE', 20)
assert(want == got)
if __name__ == '__main__':
main()
'''
Implement PKCS#7 padding
A block cipher transforms a fixed-sized block (usually 8 or 16 bytes) of plaintext into ciphertext.
But we almost never want to transform a single block; we encrypt irregularly-sized messages.
One way we account for irregularly-sized messages is by padding, creating a plaintext that
is an even multiple of the blocksize. The most popular padding scheme is called PKCS#7.
So: pad any block to a specific block length, by appending the number of bytes
of padding to the end of the block. For instance,
"YELLOW SUBMARINE"
... padded to 20 bytes would be:
"YELLOW SUBMARINE\x04\x04\x04\x04"
'''